From f9511ce0c3e482f7ccc1b575033cc4c0bd507ed0 Mon Sep 17 00:00:00 2001 From: Marco Acierno Date: Sun, 29 Sep 2024 20:18:55 +0200 Subject: [PATCH 1/2] Convert grant voucher to the new email template system --- backend/grants/tasks.py | 61 +++++-------------- backend/grants/tests/test_tasks.py | 21 +++---- .../0016_alter_emailtemplate_identifier.py | 18 ++++++ backend/notifications/models.py | 9 +++ 4 files changed, 50 insertions(+), 59 deletions(-) create mode 100644 backend/notifications/migrations/0016_alter_emailtemplate_identifier.py diff --git a/backend/grants/tasks.py b/backend/grants/tasks.py index 67047fd872..ba25eb6e8f 100644 --- a/backend/grants/tasks.py +++ b/backend/grants/tasks.py @@ -3,13 +3,11 @@ from django.conf import settings from django.utils import timezone -from notifications.models import EmailTemplateIdentifier -from notifications.templates import EmailTemplate +from notifications.models import EmailTemplate, EmailTemplateIdentifier from users.models import User from grants.models import Grant from integrations import slack -from notifications.emails import send_email import logging @@ -138,22 +136,21 @@ def send_grant_voucher_email(*, grant_id): user = grant.user voucher_code = grant.voucher_code + conference = grant.conference conference_name = grant.conference.name.localize("en") - subject_prefix = f"[{conference_name}]" - - send_email( - template=EmailTemplate.GRANT_VOUCHER_CODE, - to=user.email, - subject=f"{subject_prefix} Your Grant Voucher Code", - variables={ - "firstname": get_name(user, "there"), - "voucherCode": voucher_code, - "hasApprovedAccommodation": grant.has_approved_accommodation(), - "visaPageLink": urljoin(settings.FRONTEND_URL, "/visa"), + + email_template = EmailTemplate.objects.for_conference(conference).get_by_identifier( + EmailTemplateIdentifier.grant_voucher_code + ) + email_template.send_email( + recipient=user, + placeholders={ + "user_name": get_name(user, "there"), + "voucher_code": voucher_code, + "has_approved_accommodation": grant.has_approved_accommodation(), + "conference_name": conference_name, + "visa_page_link": urljoin(settings.FRONTEND_URL, "/visa"), }, - reply_to=[ - "grants@pycon.it", - ], ) grant.voucher_email_sent_at = timezone.now() @@ -202,33 +199,3 @@ def _new_send_grant_email( grant.applicant_reply_sent_at = timezone.now() grant.save() - - -def _send_grant_email(template: EmailTemplate, subject: str, grant: Grant, **kwargs): - try: - user = grant.user - - conference_name = grant.conference.name.localize("en") - subject_prefix = f"[{conference_name}]" - - send_email( - template=template, - to=user.email, - subject=f"{subject_prefix} {subject}", - variables={ - "firstname": get_name(user, "there"), - "conferenceName": conference_name, - **kwargs, - }, - reply_to=["grants@pycon.it"], - ) - - grant.applicant_reply_sent_at = timezone.now() - grant.save() - except Exception as e: - logger.error( - "Something went wrong while sending email Reply for Grant %s:\n%s", - grant.id, - e, - exc_info=True, - ) diff --git a/backend/grants/tests/test_tasks.py b/backend/grants/tests/test_tasks.py index be2f3ce7a7..7f8949b366 100644 --- a/backend/grants/tests/test_tasks.py +++ b/backend/grants/tests/test_tasks.py @@ -4,7 +4,6 @@ import pytest from users.tests.factories import UserFactory -from notifications.templates import EmailTemplate from grants.tests.factories import GrantFactory from grants.tasks import ( @@ -34,20 +33,18 @@ def test_send_grant_voucher_email(settings): approved_type=Grant.ApprovedType.ticket_only, ) - with patch("grants.tasks.send_email") as email_mock: + with patch("grants.tasks.EmailTemplate") as mock_email_template: send_grant_voucher_email(grant_id=grant.id) - email_mock.assert_called_once_with( - template=EmailTemplate.GRANT_VOUCHER_CODE, - to="marco@placeholder.it", - subject=f"[{grant.conference.name}] Your Grant Voucher Code", - variables={ - "firstname": "Marco Acierno", - "voucherCode": "ABC123", - "hasApprovedAccommodation": False, - "visaPageLink": "https://pycon.it/visa", + mock_email_template.objects.for_conference().get_by_identifier().send_email.assert_called_once_with( + recipient=user, + placeholders={ + "user_name": "Marco Acierno", + "voucher_code": "ABC123", + "has_approved_accommodation": False, + "visa_page_link": "https://pycon.it/visa", + "conference_name": grant.conference.name.localize("en"), }, - reply_to=["grants@pycon.it"], ) diff --git a/backend/notifications/migrations/0016_alter_emailtemplate_identifier.py b/backend/notifications/migrations/0016_alter_emailtemplate_identifier.py new file mode 100644 index 0000000000..4f40026f84 --- /dev/null +++ b/backend/notifications/migrations/0016_alter_emailtemplate_identifier.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.8 on 2024-09-29 18:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('notifications', '0015_alter_sentemail_conference'), + ] + + operations = [ + migrations.AlterField( + model_name='emailtemplate', + name='identifier', + field=models.CharField(choices=[('proposal_accepted', 'Proposal accepted'), ('proposal_rejected', 'Proposal rejected'), ('proposal_in_waiting_list', 'Proposal in waiting list'), ('proposal_scheduled_time_changed', 'Proposal scheduled time changed'), ('speaker_communication', 'Speaker communication'), ('voucher_code', 'Voucher code'), ('reset_password', '[System] Reset password'), ('grant_approved', 'Grant approved'), ('grant_rejected', 'Grant rejected'), ('grant_waiting_list', 'Grant waiting list'), ('grant_waiting_list_update', 'Grant waiting list update'), ('grant_voucher_code', 'Grant voucher code'), ('custom', 'Custom')], max_length=200, verbose_name='identifier'), + ), + ] diff --git a/backend/notifications/models.py b/backend/notifications/models.py index e4bc243da1..67c7b96297 100644 --- a/backend/notifications/models.py +++ b/backend/notifications/models.py @@ -36,6 +36,7 @@ class EmailTemplateIdentifier(models.TextChoices): "grant_waiting_list_update", _("Grant waiting list update"), ) + grant_voucher_code = "grant_voucher_code", _("Grant voucher code") custom = "custom", _("Custom") @@ -120,6 +121,14 @@ class EmailTemplate(TimeStampedModel): "body", "subject", ], + EmailTemplateIdentifier.grant_voucher_code: [ + *BASE_PLACEHOLDERS, + "conference_name", + "voucher_code", + "user_name", + "has_approved_accommodation", + "visa_page_link", + ], } conference = models.ForeignKey( From db5355a60c6090eefbe14f3079edf9bded122b98 Mon Sep 17 00:00:00 2001 From: Marco Acierno Date: Sun, 29 Sep 2024 20:19:36 +0200 Subject: [PATCH 2/2] remove field --- backend/notifications/templates.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/backend/notifications/templates.py b/backend/notifications/templates.py index 7f6ce9624a..f482e79779 100644 --- a/backend/notifications/templates.py +++ b/backend/notifications/templates.py @@ -2,9 +2,6 @@ class EmailTemplate(str, Enum): - # Grants - GRANT_VOUCHER_CODE = "grants-voucher-code" - # Users RESET_PASSWORD = "reset-password"