-
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.
Ajouter le code de base pour les notifications
Ajout de Django Post office pour la gestion des files d'attentes des mails. Ajout de la logique métier en fonction de chaque type de message.
- Loading branch information
Showing
11 changed files
with
222 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MUS_STRUCTURE = "MUS" | ||
BSV_STRUCTURE = "SAS/SDSPV/BSV" |
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
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from post_office.mail import send | ||
|
||
from core.constants import MUS_STRUCTURE | ||
from core.models import Message | ||
|
||
|
||
def _send_message(recipients: list[str], copy: list[str], subject, message): | ||
send( | ||
recipients=recipients, | ||
cc=copy, | ||
sender="[email protected]", | ||
subject=f"SEVES - {subject}", | ||
message=message, | ||
) | ||
|
||
|
||
def notify_message(instance: 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.has_structure()] | ||
copy = [r.email for r in instance.recipients_copy.has_structure()] | ||
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.has_agent()] | ||
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.has_agent().filter(agent__structure__niveau2=MUS_STRUCTURE) | ||
recipients = [r.email for r in recipients] | ||
|
||
if recipients and message: | ||
_send_message(recipients, copy, subject=subject, message=message) |
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
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
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
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
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
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 |
---|---|---|
|
@@ -203,9 +203,10 @@ def test_can_add_and_see_note_without_document(live_server, page: Page, fiche_de | |
def test_can_add_and_see_compte_rendu(live_server, page: Page, fiche_detection: FicheDetection): | ||
page.goto(f"{live_server.url}{fiche_detection.get_absolute_url()}") | ||
|
||
Contact.objects.create(structure=Structure.objects.create(niveau1="MUS", niveau2="MUS", libelle="MUS")) | ||
structure = Structure.objects.create(niveau1="MUS", niveau2="MUS", libelle="MUS") | ||
Contact.objects.create(structure=structure, email="[email protected]") | ||
structure = Structure.objects.create(niveau1="SAS/SDSPV/BSV", niveau2="SAS/SDSPV/BSV", libelle="BSV") | ||
Contact.objects.create(structure=structure) | ||
Contact.objects.create(structure=structure, email="[email protected]") | ||
page.get_by_test_id("element-actions").click() | ||
page.get_by_test_id("fildesuivi-actions-compte-rendu").click() | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import pytest | ||
from model_bakery import baker | ||
|
||
from core.models import Structure, Agent, Contact, Message | ||
from core.notifications import notify_message | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_notification_message(mailoutbox, fiche_detection): | ||
sender = baker.make(Contact, agent=baker.make(Agent)) | ||
contact_1 = baker.make(Contact, agent=baker.make(Agent)) | ||
contact_2 = baker.make(Contact, structure=baker.make(Structure)) | ||
contact_3 = baker.make(Contact, structure=baker.make(Structure)) | ||
contact_4 = baker.make(Contact, agent=baker.make(Agent)) | ||
_contact_5 = baker.make(Contact, structure=baker.make(Structure)) | ||
_contact_6 = baker.make(Contact, agent=baker.make(Agent)) | ||
|
||
message = Message.objects.create( | ||
title="TITLE", | ||
content="My message \n Thanks", | ||
sender=sender, | ||
message_type=Message.MESSAGE, | ||
content_object=fiche_detection, | ||
) | ||
message.recipients.set([contact_1, contact_2]) | ||
message.recipients_copy.set([contact_3, contact_4]) | ||
|
||
notify_message(message) | ||
|
||
assert len(mailoutbox) == 1 | ||
message = mailoutbox[0] | ||
assert message.subject == "SEVES - TITLE" | ||
assert ( | ||
message.body == "Bonjour,\n Vous avez reçu un message sur SEVES dont voici le contenu : \n My message \n Thanks" | ||
) | ||
assert message.from_email == "[email protected]" | ||
assert set(message.to) == {contact_1.email, contact_2.email} | ||
assert set(message.cc) == {contact_3.email, contact_4.email} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_notification_demande_intervention(mailoutbox, fiche_detection): | ||
sender = baker.make(Contact, agent=baker.make(Agent)) | ||
agent_1 = baker.make(Contact, agent=baker.make(Agent)) | ||
agent_2 = baker.make(Contact, agent=baker.make(Agent)) | ||
_agent_3 = baker.make(Contact, agent=baker.make(Agent)) | ||
structure_1 = baker.make(Contact, structure=baker.make(Structure)) | ||
structure_2 = baker.make(Contact, structure=baker.make(Structure)) | ||
_structure_3 = baker.make(Contact, structure=baker.make(Structure)) | ||
|
||
message = Message.objects.create( | ||
title="TITLE", | ||
content="My message \n Thanks", | ||
sender=sender, | ||
message_type=Message.DEMANDE_INTERVENTION, | ||
content_object=fiche_detection, | ||
) | ||
message.recipients.set([agent_1, structure_1]) | ||
message.recipients_copy.set([agent_2, structure_2]) | ||
|
||
notify_message(message) | ||
|
||
assert len(mailoutbox) == 1 | ||
message = mailoutbox[0] | ||
assert message.subject == "SEVES - TITLE" | ||
assert message.body == "Bonjour,\n Vous avez reçu un message sur SEVES." | ||
assert message.from_email == "[email protected]" | ||
assert set(message.to) == {structure_1.email} | ||
assert set(message.cc) == {structure_2.email} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_notification_point_de_situation(mailoutbox, fiche_detection): | ||
sender = baker.make(Contact, agent=baker.make(Agent)) | ||
agent_1 = baker.make(Contact, agent=baker.make(Agent)) | ||
_agent_2 = baker.make(Contact, agent=baker.make(Agent)) | ||
structure_1 = baker.make(Contact, structure=baker.make(Structure)) | ||
_structure_2 = baker.make(Contact, structure=baker.make(Structure)) | ||
|
||
message = Message.objects.create( | ||
title="TITLE", | ||
content="My message \n Thanks", | ||
sender=sender, | ||
message_type=Message.POINT_DE_SITUATION, | ||
content_object=fiche_detection, | ||
) | ||
fiche_detection.contacts.set([agent_1, structure_1]) | ||
|
||
notify_message(message) | ||
|
||
assert len(mailoutbox) == 1 | ||
message = mailoutbox[0] | ||
assert message.subject == "SEVES - TITLE" | ||
assert message.body == "Bonjour,\n Vous avez reçu un nouveau point de suivi sur SEVES." | ||
assert message.from_email == "[email protected]" | ||
assert set(message.to) == {agent_1.email} | ||
assert set(message.cc) == set() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_notification_fin_de_suivi(mailoutbox, fiche_detection): | ||
sender = baker.make(Contact, agent=baker.make(Agent)) | ||
agent_1 = baker.make(Contact, agent=baker.make(Agent, structure__niveau2="MUS")) | ||
agent_2 = baker.make(Contact, agent=baker.make(Agent, structure__niveau2="FOO")) | ||
structure_1 = baker.make(Contact, structure=baker.make(Structure)) | ||
|
||
message = Message.objects.create( | ||
title="TITLE", | ||
content="My message \n Thanks", | ||
sender=sender, | ||
message_type=Message.FIN_SUIVI, | ||
content_object=fiche_detection, | ||
) | ||
fiche_detection.contacts.set([agent_1, agent_2, structure_1]) | ||
|
||
notify_message(message) | ||
|
||
assert len(mailoutbox) == 1 | ||
message = mailoutbox[0] | ||
assert message.subject == "SEVES - TITLE" | ||
assert message.body == "Bonjour,\n Vous avez reçu un nouveau point de suivi sur SEVES." | ||
assert message.from_email == "[email protected]" | ||
assert set(message.to) == {agent_1.email} | ||
assert set(message.cc) == set() |