From 77df37f2838bfbded8f552a2c79ed05205a24437 Mon Sep 17 00:00:00 2001 From: DuP-491 Date: Sat, 5 Feb 2022 20:59:31 +0530 Subject: [PATCH] email threading and spam prevention --- news/migrations/__init__.py | 0 news/models.py | 15 ++++++++++++- news/views.py | 42 ++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 news/migrations/__init__.py diff --git a/news/migrations/__init__.py b/news/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/news/models.py b/news/models.py index c6970a29..44cd7173 100644 --- a/news/models.py +++ b/news/models.py @@ -1,14 +1,18 @@ +import datetime from django.db import models from django.utils import timezone +from django.utils.timezone import utc from django.urls import reverse # Create your models here. class News(models.Model): + title = models.CharField(blank=False,unique=False,max_length=256) - content =models.TextField() + content = models.TextField() link = models.URLField(blank=True,null=True) date = models.DateField(blank=True,unique=False) is_open = models.BooleanField(default=False) + broadcast = models.DateTimeField(auto_now_add=timezone.now(),blank=True) def __str__(self): return f'{self.title}' @@ -16,3 +20,12 @@ def __str__(self): def save(self, *args,**kwargs): self.date=timezone.now() super().save(args,kwargs) + + def allow_broadcast(self): + # return True + now = datetime.datetime.utcnow().replace(tzinfo=utc) + time_difference = now-self.broadcast + if time_difference.total_seconds() > 300: + return True + else: + return False diff --git a/news/views.py b/news/views.py index bf282f3d..0107bd29 100644 --- a/news/views.py +++ b/news/views.py @@ -9,8 +9,22 @@ from django.contrib.auth.models import User from django.core.mail import EmailMessage,send_mass_mail from django.conf.global_settings import EMAIL_HOST_USER +from django.utils import timezone from django.http import JsonResponse +import threading +from threading import Thread # Create your views here. +class EmailThread(threading.Thread): + def __init__(self, subject, html_content, recipient_list): + self.subject = subject + self.recipient_list = recipient_list + self.html_content = html_content + threading.Thread.__init__(self) + + def run (self): + msg = EmailMessage(self.subject, self.html_content, EMAIL_HOST_USER, self.recipient_list) + msg.content_subtype = "html" + msg.send() def news(request): context={} @@ -40,24 +54,23 @@ def createNews(request): def broadCastNews(request,pk): # news_id=request.POST.get('id') news = News.objects.get(id=pk) + if not news.allow_broadcast(): + messages.success(request, f"Broadcast for this post has been recently done,try again after 5 minutes") + return redirect('news:news_page') mail_subject = news.title - message = render_to_string('news/notice_email.html', context={'body': news.content}) + notice_body = render_to_string('news/notice_email.html', context={'body': news.content}) to_users = [] for user in User.objects.all(): - try: - if user.is_active and user.email is not EMAIL_HOST_USER: - to_users.append(user.email) - except: - pass + if user.is_active and user.email is not EMAIL_HOST_USER: + to_users.append(user.email) try: - email = EmailMessage( - subject=mail_subject, body=message, to=to_users, - ) - email.content_subtype = "html" - email.send() + for i in range(0,len(to_users),100): + EmailThread(mail_subject, notice_body, to_users[i:i+100]).start() + messages.success(request, f'Notice has been broadcast to all users') + news.broadcast=timezone.now() + news.save() except: - pass - messages.success(request, f'Notice has been broadcast to all users') + messages.success(request, f'Notice was not broad casted') return redirect('news:news_page') @has_role_head_or_coordinator @@ -82,4 +95,5 @@ def updateNews(request,pk): return redirect('news:news_page') else: messages.info(request,"Sorry you dont have permission") - return redirect('news:news_page') \ No newline at end of file + return redirect('news:news_page') +