diff --git a/PyRIGS/settings.py b/PyRIGS/settings.py index 7813e1be..6d2fbbd6 100644 --- a/PyRIGS/settings.py +++ b/PyRIGS/settings.py @@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/1.7/ref/settings/ import os import raven import secrets +import datetime BASE_DIR = os.path.dirname(os.path.dirname(__file__)) @@ -44,13 +45,11 @@ if not DEBUG: INTERNAL_IPS = ['127.0.0.1'] -# TODO This will conflict with merging the auth refactor -ADMINS = [ - ('Tom Price', 'tomtom5152@gmail.com') -] +ADMINS = [('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 - INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', @@ -185,6 +184,8 @@ if not DEBUG or EMAILER_TEST: else: EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' +EMAIL_COOLDOWN = datetime.timedelta(minutes=15) + # Internationalization # https://docs.djangoproject.com/en/1.7/topics/i18n/ diff --git a/RIGS/admin.py b/RIGS/admin.py index b3bc16eb..65442968 100644 --- a/RIGS/admin.py +++ b/RIGS/admin.py @@ -22,13 +22,22 @@ admin.site.register(models.Invoice) admin.site.register(models.Payment) +def approve_user(modeladmin, request, queryset): + queryset.update(is_approved=True) + + +approve_user.short_description = "Approve selected users" + + @admin.register(models.Profile) class ProfileAdmin(UserAdmin): + # Don't know how to add 'is_approved' whilst preserving the default list... + list_filter = ('is_approved', 'is_active', 'is_staff', 'is_superuser', 'groups') fieldsets = ( (None, {'fields': ('username', 'password')}), (_('Personal info'), { 'fields': ('first_name', 'last_name', 'email', 'initials', 'phone')}), - (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', + (_('Permissions'), {'fields': ('is_approved', 'is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}), (_('Important dates'), { 'fields': ('last_login', 'date_joined')}), @@ -41,6 +50,7 @@ class ProfileAdmin(UserAdmin): ) form = forms.ProfileChangeForm add_form = forms.ProfileCreationForm + actions = [approve_user] class AssociateAdmin(VersionAdmin): diff --git a/RIGS/finance.py b/RIGS/finance.py index a7c357fb..061c81a8 100644 --- a/RIGS/finance.py +++ b/RIGS/finance.py @@ -11,6 +11,7 @@ from django.template.loader import get_template from django.views import generic from django.db.models import Q from z3c.rml import rml2pdf +from django.db.models import Q from RIGS import models @@ -122,6 +123,34 @@ class InvoiceArchive(generic.ListView): template_name = 'RIGS/invoice_list_archive.html' paginate_by = 25 + def get_queryset(self): + q = self.request.GET.get('q', "") + + filter = Q(event__name__icontains=q) + + # try and parse an int + try: + val = int(q) + filter = filter | Q(pk=val) + filter = filter | Q(event__pk=val) + except: # noqa + # not an integer + pass + + try: + if q[0] == "N": + val = int(q[1:]) + filter = Q(event__pk=val) # If string is Nxxxxx then filter by event number + elif q[0] == "#": + val = int(q[1:]) + filter = Q(pk=val) # If string is #xxxxx then filter by invoice number + except: # noqa + pass + + object_list = self.model.objects.filter(filter).order_by('-invoice_date') + + return object_list + class InvoiceWaiting(generic.ListView): model = models.Event diff --git a/RIGS/forms.py b/RIGS/forms.py index 47b0f062..a006bc87 100644 --- a/RIGS/forms.py +++ b/RIGS/forms.py @@ -2,8 +2,10 @@ 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 from captcha.fields import ReCaptchaField import simplejson @@ -33,8 +35,16 @@ class ProfileRegistrationFormUniqueEmail(RegistrationFormUniqueEmail): return self.cleaned_data['initials'] +class CheckApprovedForm(AuthenticationForm): + def confirm_login_allowed(self, user): + if user.is_approved or user.is_superuser: + return AuthenticationForm.confirm_login_allowed(self, user) + else: + raise forms.ValidationError("Your account hasn't been approved by an administrator yet. Please check back in a few minutes!") + + # Embedded Login form - remove the autofocus -class EmbeddedAuthenticationForm(AuthenticationForm): +class EmbeddedAuthenticationForm(CheckApprovedForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['username'].widget.attrs.pop('autofocus', None) diff --git a/RIGS/migrations/0036_profile_is_approved.py b/RIGS/migrations/0036_profile_is_approved.py new file mode 100644 index 00000000..3fdb5af7 --- /dev/null +++ b/RIGS/migrations/0036_profile_is_approved.py @@ -0,0 +1,23 @@ +# Generated by Django 2.0.13 on 2020-01-10 14:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('RIGS', '0035_auto_20191124_1319'), + ] + + operations = [ + migrations.AddField( + model_name='profile', + name='is_approved', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='profile', + name='last_emailed', + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/RIGS/migrations/0037_approve_legacy.py b/RIGS/migrations/0037_approve_legacy.py new file mode 100644 index 00000000..72348b3f --- /dev/null +++ b/RIGS/migrations/0037_approve_legacy.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.13 on 2020-01-11 18:29 +# This migration ensures that legacy Profiles from before approvals were implemented are automatically approved +from django.db import migrations + +def approve_legacy(apps, schema_editor): + Profile = apps.get_model('RIGS', 'Profile') + for person in Profile.objects.all(): + person.is_approved = True + person.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('RIGS', '0036_profile_is_approved'), + ] + + operations = [ + migrations.RunPython(approve_legacy) + ] diff --git a/RIGS/migrations/0036_auto_20200208_1342.py b/RIGS/migrations/0038_auto_20200306_2000.py similarity index 89% rename from RIGS/migrations/0036_auto_20200208_1342.py rename to RIGS/migrations/0038_auto_20200306_2000.py index 4c86d115..f1f893e0 100644 --- a/RIGS/migrations/0036_auto_20200208_1342.py +++ b/RIGS/migrations/0038_auto_20200306_2000.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.3 on 2020-02-08 13:42 +# Generated by Django 2.0.13 on 2020-03-06 20:00 from django.db import migrations @@ -6,7 +6,7 @@ from django.db import migrations class Migration(migrations.Migration): dependencies = [ - ('RIGS', '0035_auto_20191124_1319'), + ('RIGS', '0037_approve_legacy'), ] operations = [ diff --git a/RIGS/models.py b/RIGS/models.py index f68ecbcd..2b22da05 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -25,6 +25,8 @@ class Profile(AbstractUser): initials = models.CharField(max_length=5, unique=True, null=True, blank=False) 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): @@ -51,6 +53,14 @@ class Profile(AbstractUser): def latest_events(self): return self.event_mic.order_by('-start_date').select_related('person', 'organisation', 'venue', 'mic') + @classmethod + def admins(cls): + return Profile.objects.filter(email__in=[y for x in settings.ADMINS for y in x]) + + @classmethod + def users_awaiting_approval_count(cls): + return Profile.objects.filter(models.Q(is_approved=False)).count() + def __str__(self): return self.name diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index 664cbe56..6aeff551 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -225,10 +225,18 @@ class EventPrint(generic.View): return response -class EventArchive(generic.ArchiveIndexView): +class EventArchive(generic.ListView): model = models.Event - date_field = "start_date" paginate_by = 25 + template_name = "RIGS/event_archive.html" + + def get_context_data(self, **kwargs): + # get super context + context = super(EventArchive, self).get_context_data(**kwargs) + + context['start'] = self.request.GET.get('start', None) + context['end'] = self.request.GET.get('end', datetime.date.today().strftime('%Y-%m-%d')) + return context def get_queryset(self): start = self.request.GET.get('start', None) @@ -240,19 +248,34 @@ class EventArchive(generic.ArchiveIndexView): "Muppet! Check the dates, it has been fixed for you.") start, end = end, start # Stop the impending fail - filter = False + filter = Q() if end != "": - filter = Q(start_date__lte=end) + filter &= Q(start_date__lte=end) if start: - if filter: - filter = filter & Q(start_date__gte=start) - else: - filter = Q(start_date__gte=start) + filter &= Q(start_date__gte=start) - if filter: - qs = self.model.objects.filter(filter).order_by('-start_date') - else: - qs = self.model.objects.all().order_by('-start_date') + q = self.request.GET.get('q', "") + + if q is not "": + qfilter = Q(name__icontains=q) | Q(description__icontains=q) | Q(notes__icontains=q) + + # try and parse an int + try: + val = int(q) + qfilter = qfilter | Q(pk=val) + except: # noqa not an integer + pass + + try: + if q[0] == "N": + val = int(q[1:]) + qfilter = Q(pk=val) # If string is N###### then do a simple PK filter + except: # noqa + pass + + filter &= qfilter + + qs = self.model.objects.filter(filter).order_by('-start_date') # Preselect related for efficiency qs.select_related('person', 'organisation', 'venue', 'mic') diff --git a/RIGS/signals.py b/RIGS/signals.py index f0e8308b..e1772e07 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,9 @@ 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.urls import reverse +from django.utils import timezone +from registration.signals import user_activated from premailer import Premailer from z3c.rml import rml2pdf @@ -102,3 +106,35 @@ 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): + # Bit more controlled than just emailing all superusers + for admin in models.Profile.admins(): + # Check we've ever emailed them before and if so, if cooldown has passed. + if admin.last_emailed is None or admin.last_emailed + settings.EMAIL_COOLDOWN <= timezone.now(): + context = { + 'request': request, + 'link_suffix': reverse("admin:RIGS_profile_changelist") + '?is_approved__exact=0', + 'number_of_users': models.Profile.users_awaiting_approval_count(), + '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.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) 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 diff --git a/RIGS/templates/RIGS/event_archive.html b/RIGS/templates/RIGS/event_archive.html index 4fc5642e..18459073 100644 --- a/RIGS/templates/RIGS/event_archive.html +++ b/RIGS/templates/RIGS/event_archive.html @@ -5,34 +5,49 @@ {% block content %}
-

Event Archive

- -