From b03117a11908ac6c95ccbe893aa2347fe7782cbb Mon Sep 17 00:00:00 2001 From: Christophe Henry Date: Mon, 14 Mar 2022 17:28:00 +0100 Subject: [PATCH] Display a warning page when an issuer already exists with a given adress in DB --- aidants_connect/settings.py | 9 +++++ .../email/issuer_profile_reminder.html | 0 .../email/issuer_profile_reminder.txt | 0 .../issuer_already_exists_warning.html | 15 +++++++ .../tests/test_views.py | 29 +++++++++++++- aidants_connect_habilitation/views.py | 39 +++++++++++++++++-- 6 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 aidants_connect_habilitation/templates/email/issuer_profile_reminder.html create mode 100644 aidants_connect_habilitation/templates/email/issuer_profile_reminder.txt create mode 100644 aidants_connect_habilitation/templates/issuer_already_exists_warning.html diff --git a/aidants_connect/settings.py b/aidants_connect/settings.py index 8c05eaf50..14c9d5a32 100644 --- a/aidants_connect/settings.py +++ b/aidants_connect/settings.py @@ -553,3 +553,12 @@ def getenv_bool(key: str, default: Optional[bool] = None) -> bool: Je vous contacte car je ne reçois pas les emails de confirmation de mon adresse email.""", ) + +EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM = os.getenv( + "EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM", SUPPORT_EMAIL +) + +EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT = os.getenv( + "EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT", + "Rappel de votre profil demandeur", +) diff --git a/aidants_connect_habilitation/templates/email/issuer_profile_reminder.html b/aidants_connect_habilitation/templates/email/issuer_profile_reminder.html new file mode 100644 index 000000000..e69de29bb diff --git a/aidants_connect_habilitation/templates/email/issuer_profile_reminder.txt b/aidants_connect_habilitation/templates/email/issuer_profile_reminder.txt new file mode 100644 index 000000000..e69de29bb diff --git a/aidants_connect_habilitation/templates/issuer_already_exists_warning.html b/aidants_connect_habilitation/templates/issuer_already_exists_warning.html new file mode 100644 index 000000000..5efc949fc --- /dev/null +++ b/aidants_connect_habilitation/templates/issuer_already_exists_warning.html @@ -0,0 +1,15 @@ +{% extends 'layouts/main-habilitation.html' %} +{% load static form_extras %} + +{% block title %} + Aidants Connect - Un compte demandeur existe déjà +{% endblock %} + +{% block content %} +
+

+ Un profil avec cette adresse email existe déjà. Nous venons d'envoyer un email à cette adresse contenant un lien + où vous pourrez retrouver toutes les demandes faites avec ce profil. +

+
+{% endblock %} diff --git a/aidants_connect_habilitation/tests/test_views.py b/aidants_connect_habilitation/tests/test_views.py index 019934ad9..15df62173 100644 --- a/aidants_connect_habilitation/tests/test_views.py +++ b/aidants_connect_habilitation/tests/test_views.py @@ -1,5 +1,5 @@ from datetime import timedelta -from unittest.mock import patch +from unittest.mock import ANY, Mock, patch from uuid import UUID, uuid4 from django.forms import model_to_dict @@ -97,6 +97,33 @@ def test_redirect_valid_post_creates_an_email_confirmation(self): "Request should have created an instance of IssuerEmailConfirmation" ) + @patch("aidants_connect_habilitation.views.send_mail") + def test_send_email_when_issuer_already_exists(self, send_mail_mock: Mock): + issuer: Issuer = IssuerFactory() + + data = utils.get_form(IssuerForm).clean() + data["email"] = issuer.email + + self.client.post(reverse(self.pattern_name), data) + + send_mail_mock.assert_called_with( + from_email=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM, + recipient_list=[issuer.email], + subject=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT, + message=ANY, + html_message=ANY, + ) + + def test_render_warning_when_issuer_already_exists(self): + issuer: Issuer = IssuerFactory() + + data = utils.get_form(IssuerForm).clean() + data["email"] = issuer.email + + response = self.client.post(reverse(self.pattern_name), data) + + self.assertTemplateUsed(response, "issuer_already_exists_warning.html") + @tag("habilitation") class IssuerEmailConfirmationWaitingViewTests(TestCase): diff --git a/aidants_connect_habilitation/views.py b/aidants_connect_habilitation/views.py index 2ca951923..e9eefe67e 100644 --- a/aidants_connect_habilitation/views.py +++ b/aidants_connect_habilitation/views.py @@ -1,5 +1,7 @@ from django.conf import settings -from django.shortcuts import get_object_or_404, redirect +from django.core.mail import send_mail +from django.shortcuts import get_object_or_404, redirect, render +from django.template import loader from django.urls import reverse from django.views.generic import FormView, RedirectView, TemplateView, View from django.views.generic.base import ContextMixin @@ -113,14 +115,23 @@ class NewHabilitationView(RedirectView): class NewIssuerFormView(HabilitationStepMixin, FormView): + template_name = "issuer_form.html" + form_class = IssuerForm + @property def step(self) -> HabilitationFormStep: return HabilitationFormStep.ISSUER - template_name = "issuer_form.html" - form_class = IssuerForm + def form_invalid(self, form: IssuerForm): + # Gets the error code of all the errors for email, if any + # See https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.error_messages # noqa + error_codes = [error.code for error in form.errors["email"].as_data()] + if "unique" in error_codes: + self.send_mail(form) + return render(self.request, "issuer_already_exists_warning.html") + return super().form_invalid(form) - def form_valid(self, form): + def form_valid(self, form: IssuerForm): self.saved_model: Issuer = form.save() IssuerEmailConfirmation.for_issuer(self.saved_model).send(self.request) return super().form_valid(form) @@ -134,6 +145,26 @@ def get_success_url(self): def get_context_data(self, **kwargs): return {**super().get_context_data(**kwargs), "step": self.step} + def get_email_context_data(self, **kwargs): + return {**self.get_context_data(), **kwargs} + + def send_mail(self, form: IssuerForm): + email = form.data["email"] + context = self.get_email_context_data(email=email) + text_message = loader.render_to_string( + "email/issuer_profile_reminder.txt", context + ) + html_message = loader.render_to_string( + "email/issuer_profile_reminder.html", context + ) + send_mail( + from_email=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_FROM, + recipient_list=[email], + subject=settings.EMAIL_HABILITATION_ISSUER_EMAIL_ALREADY_EXISTS_SUBJECT, + message=text_message, + html_message=html_message, + ) + class IssuerEmailConfirmationWaitingView( HabilitationStepMixin, CheckIssuerMixin, TemplateView