mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-27 10:22:17 +00:00
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:
@@ -45,8 +45,10 @@ if not DEBUG:
|
|||||||
INTERNAL_IPS = ['127.0.0.1']
|
INTERNAL_IPS = ['127.0.0.1']
|
||||||
|
|
||||||
ADMINS = (
|
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
|
# Application definition
|
||||||
|
|
||||||
|
|||||||
18
RIGS/migrations/0038_profile_last_emailed.py
Normal file
18
RIGS/migrations/0038_profile_last_emailed.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -28,6 +28,7 @@ class Profile(AbstractUser):
|
|||||||
phone = models.CharField(max_length=13, null=True, blank=True)
|
phone = models.CharField(max_length=13, null=True, blank=True)
|
||||||
api_key = models.CharField(max_length=40, blank=True, editable=False, null=True)
|
api_key = models.CharField(max_length=40, blank=True, editable=False, null=True)
|
||||||
is_approved = models.BooleanField(default=False)
|
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
|
@classmethod
|
||||||
def make_api_key(cls):
|
def make_api_key(cls):
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import datetime
|
||||||
import re
|
import re
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import urllib.error
|
import urllib.error
|
||||||
@@ -10,6 +11,7 @@ from django.conf import settings
|
|||||||
from django.contrib.staticfiles.storage import staticfiles_storage
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
||||||
from django.core.mail import EmailMessage, EmailMultiAlternatives
|
from django.core.mail import EmailMessage, EmailMultiAlternatives
|
||||||
from django.template.loader import get_template
|
from django.template.loader import get_template
|
||||||
|
from django.utils import timezone
|
||||||
from registration.signals import user_activated
|
from registration.signals import user_activated
|
||||||
from premailer import Premailer
|
from premailer import Premailer
|
||||||
from z3c.rml import rml2pdf
|
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):
|
def send_admin_awaiting_approval_email(user, request, **kwargs):
|
||||||
for admin in settings.ADMINS:
|
# Bit more controlled than just emailing all superusers
|
||||||
context = {
|
for admin in models.Profile.objects.filter(email__in=[y for x in settings.ADMINS for y in x]):
|
||||||
'request': request,
|
# Check we've ever emailed them before and if so, if cooldown has passed.
|
||||||
'link_suffix': 'admin/RIGS/profile/?is_approved__exact=0',
|
if admin.last_emailed is None or admin.last_emailed + datetime.timedelta(minutes=15) <= timezone.now():
|
||||||
'number_of_users': models.Profile.users_awaiting_approval_count(models.Profile.objects),
|
context = {
|
||||||
'to_name': admin[0]
|
'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(
|
email = EmailMultiAlternatives(
|
||||||
"%s new users awaiting approval on RIGS" % (context['number_of_users']),
|
"%s new users awaiting approval on RIGS" % (context['number_of_users']),
|
||||||
get_template("RIGS/admin_awaiting_approval.txt").render(context),
|
get_template("RIGS/admin_awaiting_approval.txt").render(context),
|
||||||
to=[admin[1]],
|
to=[admin.email],
|
||||||
reply_to=[user.email],
|
reply_to=[user.email],
|
||||||
)
|
)
|
||||||
css = staticfiles_storage.path('css/email.css')
|
css = staticfiles_storage.path('css/email.css')
|
||||||
html = Premailer(get_template("RIGS/admin_awaiting_approval.html").render(context),
|
html = Premailer(get_template("RIGS/admin_awaiting_approval.html").render(context),
|
||||||
external_styles=css).transform()
|
external_styles=css).transform()
|
||||||
email.attach_alternative(html, 'text/html')
|
email.attach_alternative(html, 'text/html')
|
||||||
email.send()
|
email.send()
|
||||||
|
|
||||||
|
# Update last sent
|
||||||
|
admin.last_emailed = timezone.now()
|
||||||
|
admin.save()
|
||||||
|
|
||||||
|
|
||||||
user_activated.connect(send_admin_awaiting_approval_email)
|
user_activated.connect(send_admin_awaiting_approval_email)
|
||||||
|
|||||||
Reference in New Issue
Block a user