From d3d7c0b4a110a3d758d0c0c74d42adb238409e13 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 2 Jun 2018 18:03:18 +0200 Subject: [PATCH] Add mail send when confirming. Closes #19 --- manuels/context_processors.py | 4 ++-- manuels/models.py | 20 ++++++++++++++++++- .../templates/manuels/confirm_teacher.html | 2 +- .../manuels/emails_confirmation.html | 6 ++++++ .../manuels/{email.html => emails_link.html} | 0 manuels/views.py | 2 ++ manuels_collection/settings.py | 3 +++ 7 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 manuels/templates/manuels/emails_confirmation.html rename manuels/templates/manuels/{email.html => emails_link.html} (100%) diff --git a/manuels/context_processors.py b/manuels/context_processors.py index dc80c56..aa216f7 100644 --- a/manuels/context_processors.py +++ b/manuels/context_processors.py @@ -1,5 +1,5 @@ -import os +from django.conf import settings def authorized_mails(request): - return {'authorized_mails': os.getenv('AUTHORIZED_MAILS', '').split(',')} + return {'authorized_mails': settings.AUTHORIZED_MAILS} diff --git a/manuels/models.py b/manuels/models.py index 6722e1e..7b2acb7 100644 --- a/manuels/models.py +++ b/manuels/models.py @@ -79,7 +79,25 @@ class Teacher(BaseModel): if reply_to: msg.reply_to = reply_to msg.attach_alternative( - render_to_string('manuels/email.html', {'link': link, 'teacher': self}), "text/html" + render_to_string('manuels/emails_link.html', {'link': link, 'teacher': self}), "text/html" + ) + msg.send() + + def send_confirmation(self, request): + dest = settings.LIBRARIAN_EMAILS + link = request.build_absolute_uri(reverse('home_page')) + msg = EmailMultiAlternatives( + subject="Gestion des manuels scolaires - Confirmation d'un coordonnateur", + body=f'Bonjour,\n' + f'{self.first_name} a confirmé ses listes sur {link}', + from_email=settings.SERVER_EMAIL, + to=dest, + ) + reply_to = [os.getenv('REPLY_TO')] + if reply_to: + msg.reply_to = reply_to + msg.attach_alternative( + render_to_string('manuels/emails_confirmation.html', {'link': link, 'teacher': self}), "text/html" ) msg.send() diff --git a/manuels/templates/manuels/confirm_teacher.html b/manuels/templates/manuels/confirm_teacher.html index 3ac300b..c30ec93 100644 --- a/manuels/templates/manuels/confirm_teacher.html +++ b/manuels/templates/manuels/confirm_teacher.html @@ -15,7 +15,7 @@

Danger

Êtes-vous certain·e de vouloir confirmer vos listes ? - Cette action est définitive : vous ne pourrez plus les modifier ensuite. + Cette action est définitive : vous ne ne serez plus en mesure de les modifier ensuite.

diff --git a/manuels/templates/manuels/emails_confirmation.html b/manuels/templates/manuels/emails_confirmation.html new file mode 100644 index 0000000..eb3d5b2 --- /dev/null +++ b/manuels/templates/manuels/emails_confirmation.html @@ -0,0 +1,6 @@ + +

Bonjour,

+

+ {{ teacher.full_name }} a confirmé ses listes sur {{ link }} +

+ diff --git a/manuels/templates/manuels/email.html b/manuels/templates/manuels/emails_link.html similarity index 100% rename from manuels/templates/manuels/email.html rename to manuels/templates/manuels/emails_link.html diff --git a/manuels/views.py b/manuels/views.py index ff5fd13..eed4980 100644 --- a/manuels/views.py +++ b/manuels/views.py @@ -198,4 +198,6 @@ class ConfirmTeacherView(BaseTeacherView, UpdateView): response = super().form_valid(form) self.object.has_confirmed_list = True self.object.save() + self.object.send_confirmation(request=self.request) + messages.success(self.request, "Vos listes ont été validées. Votre documentaliste a été notifiée par email.") return response diff --git a/manuels_collection/settings.py b/manuels_collection/settings.py index 0cc1427..f182b36 100644 --- a/manuels_collection/settings.py +++ b/manuels_collection/settings.py @@ -41,6 +41,9 @@ ADMINS = [('Gabriel', os.getenv('ADMIN_EMAIL')), ] SERVER_EMAIL = os.getenv('SERVER_EMAIL') EMAIL_SUBJECT_PREFIX = '[Manuels] ' +AUTHORIZED_EMAILS = os.getenv('AUTHORIZED_EMAILS', '').split(',') +LIBRARIAN_EMAILS = os.getenv('LIBRARIAN_EMAILS', '').split(',') + # Application definition INSTALLED_APPS = [