From 3b2aa02ae5dc2737537f99e9c0b2b4ae780b77ae Mon Sep 17 00:00:00 2001 From: Tom Price Date: Mon, 10 Apr 2017 19:16:45 +0100 Subject: [PATCH] Add success notification emails. Enable RevisionMixin for EventAuthorisation. Add signal receivers for RIGS. Expand RIGS into an explicitly defined app to support signals. --- RIGS/__init__.py | 1 + RIGS/apps.py | 8 +++++ RIGS/models.py | 2 +- RIGS/rigboard.py | 5 ++- RIGS/signals.py | 34 +++++++++++++++++++ .../eventauthorisation_client_success.txt | 11 ++++++ .../RIGS/eventauthorisation_mic_success.txt | 5 +++ RIGS/test_models.py | 18 ++++++---- 8 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 RIGS/apps.py create mode 100644 RIGS/signals.py create mode 100644 RIGS/templates/RIGS/eventauthorisation_client_success.txt create mode 100644 RIGS/templates/RIGS/eventauthorisation_mic_success.txt diff --git a/RIGS/__init__.py b/RIGS/__init__.py index e69de29b..f6f847cc 100644 --- a/RIGS/__init__.py +++ b/RIGS/__init__.py @@ -0,0 +1 @@ +default_app_config = 'RIGS.apps.RIGSAppConfig' diff --git a/RIGS/apps.py b/RIGS/apps.py new file mode 100644 index 00000000..a0dc3724 --- /dev/null +++ b/RIGS/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + + +class RIGSAppConfig(AppConfig): + name = 'RIGS' + + def ready(self): + import RIGS.signals diff --git a/RIGS/models.py b/RIGS/models.py index 37225697..d1f20446 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -506,7 +506,7 @@ class EventCrew(models.Model): @reversion.register -class EventAuthorisation(models.Model): +class EventAuthorisation(models.Model, RevisionMixin): event = models.OneToOneField('Event', related_name='authorisation') email = models.EmailField() name = models.CharField(max_length=255) diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index 2d98e80e..5584184d 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -3,6 +3,9 @@ import cStringIO as StringIO from io import BytesIO import urllib2 +import reversion +from django.core.mail import EmailMessage +from django.db import transaction from django.views import generic from django.core.urlresolvers import reverse_lazy from django.shortcuts import get_object_or_404 @@ -232,7 +235,7 @@ class EventAuthorise(generic.UpdateView): def form_valid(self, form): self.object = form.save() - # TODO: send email confirmation + self.template_name = self.success_template messages.add_message(self.request, messages.SUCCESS, 'Success! Your event has been authorised. You will also receive email confirmation.') diff --git a/RIGS/signals.py b/RIGS/signals.py new file mode 100644 index 00000000..869dbdae --- /dev/null +++ b/RIGS/signals.py @@ -0,0 +1,34 @@ +import reversion + +from django.core.mail import EmailMessage +from django.template.loader import get_template + +from RIGS import models + + +def send_eventauthorisation_success_email(instance): + context = { + 'object': instance, + } + client_email = EmailMessage( + "N%05d | %s - Event Authorised".format(instance.event.pk, instance.event.name), + get_template("RIGS/eventauthorisation_client_success.txt").render(context), + to=[instance.email] + ) + mic_email = EmailMessage( + "N%05d | %s - Event Authorised".format(instance.event.pk, instance.event.name), + get_template("RIGS/eventauthorisation_mic_success.txt").render(context), + to=[instance.event.mic.email] + ) + + client_email.send() + mic_email.send() + + +def on_revision_commit(instances, **kwargs): + for instance in instances: + if isinstance(instance, models.EventAuthorisation): + send_eventauthorisation_success_email(instance) + + +reversion.post_revision_commit.connect(on_revision_commit) diff --git a/RIGS/templates/RIGS/eventauthorisation_client_success.txt b/RIGS/templates/RIGS/eventauthorisation_client_success.txt new file mode 100644 index 00000000..23e05786 --- /dev/null +++ b/RIGS/templates/RIGS/eventauthorisation_client_success.txt @@ -0,0 +1,11 @@ +Hi there, + +Just to let you know your event N{{object.event.pk|stringformat:"05d"}} has been successfully authorised for {{object.amount}} by {{object.name}} as of {{object.last_edited_at}}. + +{% if object.event.organisation and object.event.organisation.union_account %}{# internal #} +Your event is now fully booked and payment will be processed by the finance department automatically. +{% else %}{# external #} +Your event is now fully booked and our finance department will be contact to arrange payment. +{% endif %} + +The TEC Rig Information Gathering System diff --git a/RIGS/templates/RIGS/eventauthorisation_mic_success.txt b/RIGS/templates/RIGS/eventauthorisation_mic_success.txt new file mode 100644 index 00000000..f5577ced --- /dev/null +++ b/RIGS/templates/RIGS/eventauthorisation_mic_success.txt @@ -0,0 +1,5 @@ +Hi {{object.event.mic.name}}, + +Just to let you know your event N{{object.event.pk|stringformat:"05d"}} has been successfully authorised for {{object.amount}} by {{object.name}} as of {{object.last_edited_at}}. + +The TEC Rig Information Gathering System diff --git a/RIGS/test_models.py b/RIGS/test_models.py index a858187d..08f1dd69 100644 --- a/RIGS/test_models.py +++ b/RIGS/test_models.py @@ -1,4 +1,5 @@ import pytz +import reversion from django.conf import settings from django.core.exceptions import ValidationError from django.test import TestCase @@ -331,14 +332,13 @@ class EventPricingTestCase(TestCase): class EventAuthorisationTestCase(TestCase): - @classmethod - def setUpTestData(cls): - cls.person = models.Person.objects.create(name='Authorisation Test Person') - cls.organisation = models.Organisation.objects.create(name='Authorisation Test Organisation') - cls.event = models.Event.objects.create(name="AuthorisationTestCase", person=cls.person, + def setUp(self): + self.person = models.Person.objects.create(name='Authorisation Test Person') + self.organisation = models.Organisation.objects.create(name='Authorisation Test Organisation') + self.event = models.Event.objects.create(name="AuthorisationTestCase", person=self.person, start_date=date.today()) # Add some items - models.EventItem.objects.create(event=cls.event, name="Authorisation test item", quantity=2, cost=123.45, + models.EventItem.objects.create(event=self.event, name="Authorisation test item", quantity=2, cost=123.45, order=1) def test_event_property(self): @@ -348,3 +348,9 @@ class EventAuthorisationTestCase(TestCase): auth1.amount = self.event.total auth1.save() self.assertTrue(self.event.authorised) + + def test_last_edited(self): + with reversion.create_revision(): + auth = models.EventAuthorisation.objects.create(event=self.event, email="authroisation@model.test.case", + name="Test Auth", amount=self.event.total) + self.assertIsNotNone(auth.last_edited_at)