From 77df37f2838bfbded8f552a2c79ed05205a24437 Mon Sep 17 00:00:00 2001 From: DuP-491 Date: Sat, 5 Feb 2022 20:59:31 +0530 Subject: [PATCH 1/3] 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') + From 1d1df4a5cbb26a917cf3e97f60ac1d589737bb51 Mon Sep 17 00:00:00 2001 From: DuP-491 Date: Sat, 5 Feb 2022 21:05:13 +0530 Subject: [PATCH 2/3] bug fix --- news/migrations/0001_initial.py | 26 +++++++++++++++++++ news/migrations/0002_remove_news_broadcast.py | 17 ++++++++++++ news/migrations/0003_news_broadcast.py | 20 ++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 news/migrations/0001_initial.py create mode 100644 news/migrations/0002_remove_news_broadcast.py create mode 100644 news/migrations/0003_news_broadcast.py diff --git a/news/migrations/0001_initial.py b/news/migrations/0001_initial.py new file mode 100644 index 00000000..714a301b --- /dev/null +++ b/news/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 3.1.5 on 2022-02-05 11:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='News', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=256)), + ('content', models.TextField()), + ('link', models.URLField(blank=True, null=True)), + ('date', models.DateField(blank=True)), + ('is_open', models.BooleanField(default=False)), + ('broadcast', models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/news/migrations/0002_remove_news_broadcast.py b/news/migrations/0002_remove_news_broadcast.py new file mode 100644 index 00000000..58b57b13 --- /dev/null +++ b/news/migrations/0002_remove_news_broadcast.py @@ -0,0 +1,17 @@ +# Generated by Django 3.1.5 on 2022-02-05 15:04 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='news', + name='broadcast', + ), + ] diff --git a/news/migrations/0003_news_broadcast.py b/news/migrations/0003_news_broadcast.py new file mode 100644 index 00000000..c6d1e9a2 --- /dev/null +++ b/news/migrations/0003_news_broadcast.py @@ -0,0 +1,20 @@ +# Generated by Django 3.1.5 on 2022-02-05 15:06 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0002_remove_news_broadcast'), + ] + + operations = [ + migrations.AddField( + model_name='news', + name='broadcast', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + ] From 0f4f8f11c617926a49e2a5dabe1a2dd0fe162fd9 Mon Sep 17 00:00:00 2001 From: DuP-491 Date: Sat, 5 Feb 2022 21:17:42 +0530 Subject: [PATCH 3/3] unrolling spam detect --- news/migrations/0001_initial.py | 3 +-- news/migrations/0002_remove_news_broadcast.py | 17 ---------------- news/migrations/0003_news_broadcast.py | 20 ------------------- news/models.py | 16 +++++++-------- news/views.py | 10 +++++----- 5 files changed, 14 insertions(+), 52 deletions(-) delete mode 100644 news/migrations/0002_remove_news_broadcast.py delete mode 100644 news/migrations/0003_news_broadcast.py diff --git a/news/migrations/0001_initial.py b/news/migrations/0001_initial.py index 714a301b..ad07cf21 100644 --- a/news/migrations/0001_initial.py +++ b/news/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.1.5 on 2022-02-05 11:58 +# Generated by Django 3.1.5 on 2022-02-05 15:47 from django.db import migrations, models @@ -20,7 +20,6 @@ class Migration(migrations.Migration): ('link', models.URLField(blank=True, null=True)), ('date', models.DateField(blank=True)), ('is_open', models.BooleanField(default=False)), - ('broadcast', models.DateTimeField(auto_now_add=True)), ], ), ] diff --git a/news/migrations/0002_remove_news_broadcast.py b/news/migrations/0002_remove_news_broadcast.py deleted file mode 100644 index 58b57b13..00000000 --- a/news/migrations/0002_remove_news_broadcast.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 3.1.5 on 2022-02-05 15:04 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('news', '0001_initial'), - ] - - operations = [ - migrations.RemoveField( - model_name='news', - name='broadcast', - ), - ] diff --git a/news/migrations/0003_news_broadcast.py b/news/migrations/0003_news_broadcast.py deleted file mode 100644 index c6d1e9a2..00000000 --- a/news/migrations/0003_news_broadcast.py +++ /dev/null @@ -1,20 +0,0 @@ -# Generated by Django 3.1.5 on 2022-02-05 15:06 - -from django.db import migrations, models -import django.utils.timezone - - -class Migration(migrations.Migration): - - dependencies = [ - ('news', '0002_remove_news_broadcast'), - ] - - operations = [ - migrations.AddField( - model_name='news', - name='broadcast', - field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), - preserve_default=False, - ), - ] diff --git a/news/models.py b/news/models.py index 44cd7173..874eb496 100644 --- a/news/models.py +++ b/news/models.py @@ -12,7 +12,7 @@ class News(models.Model): 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) + # broadcast = models.DateTimeField(auto_now_add=timezone.now(),blank=True) def __str__(self): return f'{self.title}' @@ -22,10 +22,10 @@ def save(self, *args,**kwargs): 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 + 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 0107bd29..da588428 100644 --- a/news/views.py +++ b/news/views.py @@ -54,9 +54,9 @@ 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') + # 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 notice_body = render_to_string('news/notice_email.html', context={'body': news.content}) to_users = [] @@ -67,8 +67,8 @@ def broadCastNews(request,pk): 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() + # news.broadcast=timezone.now() + # news.save() except: messages.success(request, f'Notice was not broad casted') return redirect('news:news_page')