-
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
12 changed files
with
346 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from django.conf import settings | ||
from django.core.exceptions import ValidationError | ||
from post_office.models import Email, EmailTemplate, PRIORITY | ||
from post_office.settings import ( | ||
get_available_backends, | ||
get_log_level, | ||
) | ||
from post_office.signals import email_queued | ||
from post_office.utils import ( | ||
create_attachments, | ||
get_email_template, | ||
parse_emails, | ||
parse_priority, | ||
) | ||
from post_office.mail import create | ||
|
||
|
||
def send( | ||
recipients=None, | ||
sender=None, | ||
template=None, | ||
context=None, | ||
subject="", | ||
message="", | ||
html_message="", | ||
scheduled_time=None, | ||
expires_at=None, | ||
headers=None, | ||
priority=None, | ||
attachments=None, | ||
render_on_delivery=False, | ||
log_level=None, | ||
commit=True, | ||
cc=None, | ||
bcc=None, | ||
language="", | ||
backend="", | ||
): | ||
""" | ||
Taken directly from Django post office in order to add the pk of the email in the headers | ||
""" | ||
try: | ||
recipients = parse_emails(recipients) | ||
except ValidationError as e: | ||
raise ValidationError("recipients: %s" % e.message) | ||
|
||
try: | ||
cc = parse_emails(cc) | ||
except ValidationError as e: | ||
raise ValidationError("c: %s" % e.message) | ||
|
||
try: | ||
bcc = parse_emails(bcc) | ||
except ValidationError as e: | ||
raise ValidationError("bcc: %s" % e.message) | ||
|
||
if sender is None: | ||
sender = settings.DEFAULT_FROM_EMAIL | ||
|
||
priority = parse_priority(priority) | ||
|
||
if log_level is None: | ||
log_level = get_log_level() | ||
|
||
if not commit: | ||
if priority == PRIORITY.now: | ||
raise ValueError("send_many() can't be used with priority = 'now'") | ||
if attachments: | ||
raise ValueError("Can't add attachments with send_many()") | ||
|
||
if template: | ||
if subject: | ||
raise ValueError('You can\'t specify both "template" and "subject" arguments') | ||
if message: | ||
raise ValueError('You can\'t specify both "template" and "message" arguments') | ||
if html_message: | ||
raise ValueError('You can\'t specify both "template" and "html_message" arguments') | ||
|
||
# template can be an EmailTemplate instance or name | ||
if isinstance(template, EmailTemplate): | ||
template = template | ||
# If language is specified, ensure template uses the right language | ||
if language and template.language != language: | ||
template = template.translated_templates.get(language=language) | ||
else: | ||
template = get_email_template(template, language) | ||
|
||
if backend and backend not in get_available_backends().keys(): | ||
raise ValueError("%s is not a valid backend alias" % backend) | ||
|
||
email = create( | ||
sender, | ||
recipients, | ||
cc, | ||
bcc, | ||
subject, | ||
message, | ||
html_message, | ||
context, | ||
scheduled_time, | ||
expires_at, | ||
headers, | ||
template, | ||
priority, | ||
render_on_delivery, | ||
commit=commit, | ||
backend=backend, | ||
) | ||
|
||
if attachments: | ||
attachments = create_attachments(attachments) | ||
email.attachments.add(*attachments) | ||
|
||
if email.headers: | ||
email.headers["X-Mailin-Tag"] = email.pk | ||
else: | ||
email.headers = {"X-Mailin-Tag": email.pk} | ||
email.save() | ||
|
||
if priority == PRIORITY.now: | ||
email.dispatch(log_level=log_level) | ||
elif commit: | ||
email_queued.send(sender=Email, emails=[email]) | ||
|
||
return email |
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() | ||
|
||
|
Oops, something went wrong.