Skip to content

Commit

Permalink
FEAT #513: Implement email reminders to complete risk assessments (#514)
Browse files Browse the repository at this point in the history
* FEAT #513: Implement email reminders to complete risk assessments

* Add missing f-string tag
  • Loading branch information
FreneticScribbler committed Nov 19, 2022
1 parent 37101d3 commit 0117091
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 15 deletions.
5 changes: 3 additions & 2 deletions PyRIGS/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@

INTERNAL_IPS = ['127.0.0.1']

ADMINS = [('Tom Price', 'tomtom5152@gmail.com'), ('IT Manager', 'it@nottinghamtec.co.uk'),
('Arona Jones', 'arona.jones@nottinghamtec.co.uk')]
DOMAIN = env('DOMAIN', default='example.com')

ADMINS = [('IT Manager', f'it@{DOMAIN}'), ('Arona Jones', f'arona.jones@{DOMAIN}')]
if DEBUG:
ADMINS.append(('Testing Superuser', 'superuser@example.com'))

Expand Down
38 changes: 38 additions & 0 deletions RIGS/management/commands/send_reminders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import premailer
import datetime

from django.template.loader import get_template
from django.contrib.staticfiles import finders
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.core.mail import EmailMultiAlternatives
from django.utils import timezone
from django.urls import reverse

from RIGS import models


class Command(BaseCommand):
help = 'Sends email reminders as required. Triggered daily through heroku-scheduler in production.'

def handle(self, *args, **options):
events = models.Event.objects.current_events().select_related('riskassessment')
for event in events:
earliest_time = event.earliest_time if isinstance(event.earliest_time, datetime.datetime) else timezone.make_aware(datetime.datetime.combine(event.earliest_time, datetime.time(00, 00)))
# 48 hours = 172800 seconds
if not event.cancelled and not event.dry_hire and (earliest_time - timezone.now()).total_seconds() <= 172800 and not hasattr(event, 'riskassessment'):
context = {
"event": event,
"url": "https://" + settings.DOMAIN + reverse('event_ra', kwargs={'pk': event.pk})
}
target = event.mic.email if event.mic else f"productions@{settings.DOMAIN}"
msg = EmailMultiAlternatives(
f"{event} - Risk Assessment Incomplete",
get_template("email/ra_reminder.txt").render(context),
to=[target],
reply_to=[f"h.s.manager@{settings.DOMAIN}"],
)
css = finders.find('css/email.css')
html = premailer.Premailer(get_template("email/ra_reminder.html").render(context), external_styles=css).transform()
msg.attach_alternative(html, 'text/html')
msg.send()
10 changes: 5 additions & 5 deletions RIGS/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def send_eventauthorisation_success_email(instance):

client_email = EmailMultiAlternatives(
subject,
get_template("eventauthorisation_client_success.txt").render(context),
get_template("email/eventauthorisation_client_success.txt").render(context),
to=[instance.email],
reply_to=[settings.AUTHORISATION_NOTIFICATION_ADDRESS],
)

css = finders.find('css/email.css')
html = Premailer(get_template("eventauthorisation_client_success.html").render(context),
html = Premailer(get_template("email/eventauthorisation_client_success.html").render(context),
external_styles=css).transform()
client_email.attach_alternative(html, 'text/html')

Expand All @@ -82,7 +82,7 @@ def send_eventauthorisation_success_email(instance):

mic_email = EmailMessage(
subject,
get_template("eventauthorisation_mic_success.txt").render(context),
get_template("email/eventauthorisation_mic_success.txt").render(context),
to=[mic_email_address]
)

Expand Down Expand Up @@ -117,12 +117,12 @@ def send_admin_awaiting_approval_email(user, request, **kwargs):

email = EmailMultiAlternatives(
f"{context['number_of_users']} new users awaiting approval on RIGS",
get_template("admin_awaiting_approval.txt").render(context),
get_template("email/admin_awaiting_approval.txt").render(context),
to=[admin.email],
reply_to=[user.email],
)
css = finders.find('css/email.css')
html = Premailer(get_template("admin_awaiting_approval.html").render(context),
html = Premailer(get_template("email/admin_awaiting_approval.html").render(context),
external_styles=css).transform()
email.attach_alternative(html, 'text/html')
email.send()
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions RIGS/templates/email/eventauthorisation_mic_success.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hi {{object.event.mic.get_full_name|default_if_none:"somebody"}},

Just to let you know your event N{{object.eventdisplay_id}} has been successfully authorised for £{{object.amount}} by {{object.name}} as of {{object.event.last_edited_at}}.

The TEC Rig Information Gathering System
16 changes: 16 additions & 0 deletions RIGS/templates/email/ra_reminder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'base_client_email.html' %}

{% block content %}
<p>Hi {{event.mic.get_full_name|default_if_none:"Productions Manager"}},</p>

{% if event.mic %}
<p>Just to let you know your event {{event.display_id}} <em>requires<em> a pre-event risk assessment completing prior to the event. Please do so as soon as possible.</p>
{% else %}
<p>This is a reminder that event {{event.display_id}} requires a MIC assigning and a risk assessment completing.</p>
{% endif %}

<p>Fill it out here:</p>
<a href="{{url}}" class="btn btn-info"><span class="fas fa-paperclip"></span> Create Risk Assessment</a>

<p>TEC PA &amp; Lighting</p>
{% endblock %}
9 changes: 9 additions & 0 deletions RIGS/templates/email/ra_reminder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Hi {{event.mic.get_full_name|default_if_none:"Productions Manager"}},

{% if event.mic %}
Just to let you know your event {{event.display_id}} requires a risk assessment completing prior to the event. Please do so as soon as possible.
{% else %}
This is a reminder that event {{event.display_id}} requires a MIC assigning and a risk assessment completing.
{% endif %}

The TEC Rig Information Gathering System
5 changes: 0 additions & 5 deletions RIGS/templates/eventauthorisation_mic_success.txt

This file was deleted.

6 changes: 3 additions & 3 deletions RIGS/views/rigboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ def form_valid(self, form):

msg = EmailMultiAlternatives(
f"{self.object.display_id} | {self.object.name} - Event Authorisation Request",
get_template("eventauthorisation_client_request.txt").render(context),
get_template("email/eventauthorisation_client_request.txt").render(context),
to=[email],
reply_to=[self.request.user.email],
)
css = finders.find('css/email.css')
html = premailer.Premailer(get_template("eventauthorisation_client_request.html").render(context),
html = premailer.Premailer(get_template("email/eventauthorisation_client_request.html").render(context),
external_styles=css).transform()
msg.attach_alternative(html, 'text/html')

Expand All @@ -357,7 +357,7 @@ def form_valid(self, form):


class EventAuthoriseRequestEmailPreview(generic.DetailView):
template_name = "eventauthorisation_client_request.html"
template_name = "email/eventauthorisation_client_request.html"
model = models.Event

def render_to_response(self, context, **response_kwargs):
Expand Down

0 comments on commit 0117091

Please sign in to comment.