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?
This commit is contained in:
2020-01-25 00:56:16 +00:00
parent c049874e5f
commit f19286efb0
4 changed files with 49 additions and 19 deletions

View File

@@ -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

View File

@@ -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),
),
]

View File

@@ -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):

View File

@@ -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)