From f19286efb0c4923588034a7a5abc7005fd631f1c Mon Sep 17 00:00:00 2001 From: FreneticScribbler Date: Sat, 25 Jan 2020 00:56:16 +0000 Subject: [PATCH] FEAT: Add a fifteen min cooldown between emails to admins Probably not the right way to go about it...but it does work! TODO: How to handle cooldown-emailing shared mailbox addresses? --- PyRIGS/settings.py | 4 +- RIGS/migrations/0038_profile_last_emailed.py | 18 ++++++++ RIGS/models.py | 1 + RIGS/signals.py | 45 ++++++++++++-------- 4 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 RIGS/migrations/0038_profile_last_emailed.py diff --git a/PyRIGS/settings.py b/PyRIGS/settings.py index 0793084d..40767592 100644 --- a/PyRIGS/settings.py +++ b/PyRIGS/settings.py @@ -45,8 +45,10 @@ if not DEBUG: INTERNAL_IPS = ['127.0.0.1'] ADMINS = ( - [('Tom Price', 'tomtom5152@gmail.com'), ('IT Manager', 'it@nottinghamtec.co.uk'), ('Technical Director', 'td@nottinghamtec.co.uk')] + [('Tom Price', 'tomtom5152@gmail.com'), ('IT Manager', 'it@nottinghamtec.co.uk'), ('Arona Jones', 'arona.jones@nottinghamtec.co.uk')] ) +if DEBUG: + ADMINS.append(('Testing Superuser', 'superuser@example.com')) # Application definition diff --git a/RIGS/migrations/0038_profile_last_emailed.py b/RIGS/migrations/0038_profile_last_emailed.py new file mode 100644 index 00000000..bca52b8a --- /dev/null +++ b/RIGS/migrations/0038_profile_last_emailed.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.13 on 2020-01-25 01:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('RIGS', '0037_auto_20200111_1829'), + ] + + operations = [ + migrations.AddField( + model_name='profile', + name='last_emailed', + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/RIGS/models.py b/RIGS/models.py index 2f1d9f1f..a6140c08 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -28,6 +28,7 @@ class Profile(AbstractUser): phone = models.CharField(max_length=13, null=True, blank=True) api_key = models.CharField(max_length=40, blank=True, editable=False, null=True) is_approved = models.BooleanField(default=False) + last_emailed = models.DateTimeField(blank=True, null=True) # Currently only populated by the admin approval email. TODO: Populate it each time we send any email, might need that... @classmethod def make_api_key(cls): diff --git a/RIGS/signals.py b/RIGS/signals.py index b16ac814..d64ceca3 100644 --- a/RIGS/signals.py +++ b/RIGS/signals.py @@ -1,3 +1,4 @@ +import datetime import re import urllib.request import urllib.error @@ -10,6 +11,7 @@ from django.conf import settings from django.contrib.staticfiles.storage import staticfiles_storage from django.core.mail import EmailMessage, EmailMultiAlternatives from django.template.loader import get_template +from django.utils import timezone from registration.signals import user_activated from premailer import Premailer from z3c.rml import rml2pdf @@ -106,25 +108,32 @@ post_save.connect(on_revision_commit, sender=models.EventAuthorisation) def send_admin_awaiting_approval_email(user, request, **kwargs): - for admin in settings.ADMINS: - context = { - 'request': request, - 'link_suffix': 'admin/RIGS/profile/?is_approved__exact=0', - 'number_of_users': models.Profile.users_awaiting_approval_count(models.Profile.objects), - 'to_name': admin[0] - } + # Bit more controlled than just emailing all superusers + for admin in models.Profile.objects.filter(email__in=[y for x in settings.ADMINS for y in x]): + # Check we've ever emailed them before and if so, if cooldown has passed. + if admin.last_emailed is None or admin.last_emailed + datetime.timedelta(minutes=15) <= timezone.now(): + context = { + 'request': request, + 'link_suffix': '/admin/RIGS/profile/?is_approved__exact=0', + 'number_of_users': models.Profile.users_awaiting_approval_count(models.Profile.objects), + 'to_name': admin.first_name + } - email = EmailMultiAlternatives( - "%s new users awaiting approval on RIGS" % (context['number_of_users']), - get_template("RIGS/admin_awaiting_approval.txt").render(context), - to=[admin[1]], - reply_to=[user.email], - ) - css = staticfiles_storage.path('css/email.css') - html = Premailer(get_template("RIGS/admin_awaiting_approval.html").render(context), - external_styles=css).transform() - email.attach_alternative(html, 'text/html') - email.send() + email = EmailMultiAlternatives( + "%s new users awaiting approval on RIGS" % (context['number_of_users']), + get_template("RIGS/admin_awaiting_approval.txt").render(context), + to=[admin.email], + reply_to=[user.email], + ) + css = staticfiles_storage.path('css/email.css') + html = Premailer(get_template("RIGS/admin_awaiting_approval.html").render(context), + external_styles=css).transform() + email.attach_alternative(html, 'text/html') + email.send() + + # Update last sent + admin.last_emailed = timezone.now() + admin.save() user_activated.connect(send_admin_awaiting_approval_email)