Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert grant voucher to the new email template system #4085

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 14 additions & 47 deletions backend/grants/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
)
21 changes: 9 additions & 12 deletions backend/grants/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"],
)


Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
),
]
9 changes: 9 additions & 0 deletions backend/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions backend/notifications/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@


class EmailTemplate(str, Enum):
# Grants
GRANT_VOUCHER_CODE = "grants-voucher-code"

# Users
RESET_PASSWORD = "reset-password"

Expand Down