-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modification du contenu des emails de notifications (template HTML)
- Ajoute un template HTML avec style unifié pour tous les emails de notifications - Inclut les informations de l'expéditeur et le lien vers la fiche - Styles CSS appliqués directement dans les balises HTML pour maximiser la compatibilité avec les différents clients email (cf. score Mailtrap) - Dans les tests de notifications, renomme les variables pour éviter l'ambiguïté entre le contenu du message et l'objet Message lui-même : * 'message' en 'content' pour le contenu textuel * 'instance' en 'message_obj' pour l'objet Message - Ajout de tests pour notification AC et compte rendu Ticket Notion : https://www.notion.so/incubateur-masa/Mail-au-format-HTML-pour-respecter-les-maquettes-150de24614be80d0921bd8463ded61d8?pvs=4 PR : #551
- Loading branch information
Showing
2 changed files
with
159 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,65 @@ | ||
from post_office.mail import send | ||
from post_office.models import EmailTemplate | ||
|
||
from core.constants import MUS_STRUCTURE | ||
from core.models import Message, Contact | ||
from django.conf import settings | ||
|
||
|
||
def _send_message(recipients: list[str], copy: list[str], subject, message, instance): | ||
message += ( | ||
f"\n\n Pour voir la fiche concernée par cette notification, consultez SEVES : " | ||
f'<a href="{settings.ROOT_URL}{instance.content_object.get_absolute_url()}" target="_blank">{settings.ROOT_URL}{instance.content_object.get_absolute_url()}</a>' | ||
def _send_message(recipients: list[str], copy: list[str], subject: str, content: str, message_obj: Message): | ||
template, _ = EmailTemplate.objects.update_or_create( | ||
name="seves_email_template", | ||
defaults={ | ||
"subject": f"Sèves - {message_obj.content_object.numero} - {message_obj.message_type} - {subject}", | ||
"html_content": """ | ||
<!DOCTYPE html> | ||
<html> | ||
<div style="font-family: Arial, sans-serif;"> | ||
<p style="white-space: pre-wrap; line-height: 1.5;">{{ content }}</p> | ||
<p style="font-weight: bold; margin-top: 20px; margin-bottom: 0px;">{{ message_obj.sender.agent.prenom }} {{ message_obj.sender.agent.nom }}</p> | ||
<p style="margin-top: 0px;">{{ message_obj.sender.agent.structure }}</p> | ||
<p style="margin-top: 20px;">Consulter la fiche dans Sèves : <a href="{{ fiche_url }}">{{ fiche_url }}</a></p> | ||
</div> | ||
</html> | ||
""", | ||
}, | ||
) | ||
send( | ||
recipients=recipients, | ||
cc=copy, | ||
sender="no-reply@beta.gouv.fr", | ||
subject=f"SEVES - {subject}", | ||
html_message=message, | ||
template=template, | ||
context={ | ||
"message_obj": message_obj, | ||
"content": content, | ||
"fiche_url": f"{settings.ROOT_URL}{message_obj.content_object.get_absolute_url()}", | ||
}, | ||
) | ||
|
||
|
||
def notify_message(instance: Message): | ||
def notify_message(message_obj: Message): | ||
recipients, copy = [], [] | ||
message, subject = None, None | ||
if instance.message_type == Message.MESSAGE: | ||
subject = instance.title | ||
message = f"Bonjour,\n Vous avez reçu un message sur SEVES dont voici le contenu : \n {instance.content}" | ||
recipients = [r.email for r in instance.recipients.all()] | ||
copy = [r.email for r in instance.recipients_copy.all()] | ||
elif instance.message_type == Message.COMPTE_RENDU: | ||
subject = instance.title | ||
message = f"Bonjour,\n Vous avez reçu un compte rendu sur demande d'intervention sur SEVES dont voici le contenu : \n {instance.content}" | ||
recipients = [r.email for r in instance.recipients.all()] | ||
elif instance.message_type == Message.DEMANDE_INTERVENTION: | ||
subject = instance.title | ||
message = "Bonjour,\n Vous avez reçu un message sur SEVES." | ||
recipients = [r.email for r in instance.recipients.structures_only()] | ||
copy = [r.email for r in instance.recipients_copy.structures_only()] | ||
elif instance.message_type == Message.POINT_DE_SITUATION: | ||
subject = instance.title | ||
message = "Bonjour,\n Vous avez reçu un nouveau point de suivi sur SEVES." | ||
recipients = [c.email for c in instance.content_object.contacts.agents_only()] | ||
elif instance.message_type == Message.FIN_SUIVI: | ||
subject = instance.title | ||
message = "Bonjour,\n Vous avez reçu un nouveau point de suivi sur SEVES." | ||
recipients = instance.content_object.contacts.agents_only().filter(agent__structure__niveau2=MUS_STRUCTURE) | ||
recipients = [r.email for r in recipients] | ||
elif instance.message_type == Message.NOTIFICATION_AC: | ||
subject = instance.title | ||
message = ( | ||
f"Bonjour,\n une personne a déclarée la fiche {instance.content_object.numero} à l'administration centrale." | ||
) | ||
recipients = [Contact.objects.get_mus().email, Contact.objects.get_bsv().email] | ||
content = message_obj.content | ||
|
||
if recipients and message: | ||
_send_message(recipients, copy, subject=subject, message=message, instance=instance) | ||
match message_obj.message_type: | ||
case Message.MESSAGE: | ||
recipients = [r.email for r in message_obj.recipients.all()] | ||
copy = [r.email for r in message_obj.recipients_copy.all()] | ||
case Message.COMPTE_RENDU: | ||
recipients = [r.email for r in message_obj.recipients.all()] | ||
case Message.DEMANDE_INTERVENTION: | ||
recipients = [r.email for r in message_obj.recipients.structures_only()] | ||
copy = [r.email for r in message_obj.recipients_copy.structures_only()] | ||
case Message.POINT_DE_SITUATION: | ||
recipients = [c.email for c in message_obj.content_object.contacts.agents_only()] | ||
case Message.FIN_SUIVI: | ||
recipients = message_obj.content_object.contacts.agents_only().filter( | ||
agent__structure__niveau2=MUS_STRUCTURE | ||
) | ||
recipients = [r.email for r in recipients] | ||
case Message.NOTIFICATION_AC: | ||
content = f"Bonjour,\nLa fiche {message_obj.content_object.numero} vient d'être déclarée à l'administration centrale." | ||
recipients = [Contact.objects.get_mus().email, Contact.objects.get_bsv().email] | ||
|
||
if recipients and content: | ||
_send_message(recipients, copy, subject=message_obj.title, content=content, message_obj=message_obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters