From 806b8a3f00329e69dfd1ece9dbbc1cb7935b1202 Mon Sep 17 00:00:00 2001 From: FreneticScribbler Date: Fri, 24 Jan 2020 00:56:44 +0000 Subject: [PATCH] FEAT: Add admin email notif when an account is activated and awaiting approval No async or time-since shenanigans yet! --- PyRIGS/settings.py | 2 +- RIGS/forms.py | 2 +- RIGS/models.py | 3 +++ RIGS/signals.py | 26 +++++++++++++++++++ .../RIGS/admin_awaiting_approval.html | 9 +++++++ .../RIGS/admin_awaiting_approval.txt | 5 ++++ 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 RIGS/templates/RIGS/admin_awaiting_approval.html create mode 100644 RIGS/templates/RIGS/admin_awaiting_approval.txt diff --git a/PyRIGS/settings.py b/PyRIGS/settings.py index 5877ad74..0793084d 100644 --- a/PyRIGS/settings.py +++ b/PyRIGS/settings.py @@ -45,7 +45,7 @@ if not DEBUG: INTERNAL_IPS = ['127.0.0.1'] ADMINS = ( - ('Tom Price', 'tomtom5152@gmail.com') + [('Tom Price', 'tomtom5152@gmail.com'), ('IT Manager', 'it@nottinghamtec.co.uk'), ('Technical Director', 'td@nottinghamtec.co.uk')] ) # Application definition diff --git a/RIGS/forms.py b/RIGS/forms.py index efc2111b..bb176d61 100644 --- a/RIGS/forms.py +++ b/RIGS/forms.py @@ -2,6 +2,7 @@ from django import forms from django.utils import formats from django.conf import settings from django.core import serializers +from django.core.mail import EmailMessage, EmailMultiAlternatives from django.contrib.auth.forms import UserCreationForm, UserChangeForm, AuthenticationForm, PasswordResetForm from registration.forms import RegistrationFormUniqueEmail from django.contrib.auth.forms import AuthenticationForm @@ -33,7 +34,6 @@ class ProfileRegistrationFormUniqueEmail(RegistrationFormUniqueEmail): raise forms.ValidationError("These initials are already in use. Please supply different initials.") return self.cleaned_data['initials'] - class CheckApprovedForm(AuthenticationForm): def confirm_login_allowed(self, user): if user.is_approved or user.is_superuser: diff --git a/RIGS/models.py b/RIGS/models.py index 62b068c9..2f1d9f1f 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -54,6 +54,9 @@ class Profile(AbstractUser): def latest_events(self): return self.event_mic.order_by('-start_date').select_related('person', 'organisation', 'venue', 'mic') + def users_awaiting_approval_count(self): + return self.filter(models.Q(is_approved=False)).count() + def __str__(self): return self.name diff --git a/RIGS/signals.py b/RIGS/signals.py index ea0395d7..b16ac814 100644 --- a/RIGS/signals.py +++ b/RIGS/signals.py @@ -10,6 +10,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 registration.signals import user_activated from premailer import Premailer from z3c.rml import rml2pdf @@ -102,3 +103,28 @@ def on_revision_commit(sender, instance, created, **kwargs): 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] + } + + 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() + + +user_activated.connect(send_admin_awaiting_approval_email) diff --git a/RIGS/templates/RIGS/admin_awaiting_approval.html b/RIGS/templates/RIGS/admin_awaiting_approval.html new file mode 100644 index 00000000..8db5433c --- /dev/null +++ b/RIGS/templates/RIGS/admin_awaiting_approval.html @@ -0,0 +1,9 @@ +{% extends 'base_client_email.html' %} + +{% block content %} +

Hi {{ to_name|default_if_none:"Administrator" }},

+ +

{{ number_of_users|default_if_none:"Some" }} new users are awaiting administrator approval on RIGS. Click here to approve them.

+ +

TEC PA & Lighting

+{% endblock %} diff --git a/RIGS/templates/RIGS/admin_awaiting_approval.txt b/RIGS/templates/RIGS/admin_awaiting_approval.txt new file mode 100644 index 00000000..e89785ee --- /dev/null +++ b/RIGS/templates/RIGS/admin_awaiting_approval.txt @@ -0,0 +1,5 @@ +Hi {{ to_name|default_if_none:"Administrator" }}, + +{{ number_of_users|default_if_none:"Some" }} new users are awaiting administrator approval on RIGS. Use this link to approve them: {{ request.scheme }}://{{ request.get_host }}/{{ link_suffix }} + +TEC PA & Lighting