Skip to content

Commit

Permalink
email threading and spam prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
DuP-491 committed Feb 5, 2022
1 parent 0b32c3c commit 77df37f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
Empty file added news/migrations/__init__.py
Empty file.
15 changes: 14 additions & 1 deletion news/models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
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}'

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
42 changes: 28 additions & 14 deletions news/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={}
Expand Down Expand Up @@ -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
Expand All @@ -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')
return redirect('news:news_page')

0 comments on commit 77df37f

Please sign in to comment.