Skip to content

Commit

Permalink
feat: use MJML to generate HTML emails (HL-999) (#2586)
Browse files Browse the repository at this point in the history
* feat: add mjml and city of helsinki email templates

* feat: capability to send HTML emails

* feat: new HTML email templates
  • Loading branch information
sirtawast authored Dec 12, 2023
1 parent 4369883 commit 7939f5e
Show file tree
Hide file tree
Showing 46 changed files with 8,703 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

from django.core.management.base import BaseCommand
from django.utils import timezone
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _

from applications.enums import ApplicationStatus
from applications.models import Application
from messages.automatic_messages import send_email_to_applicant
from messages.automatic_messages import (
get_email_template_context,
render_email_template,
send_email_to_applicant,
)

APPLICATION_ABOUT_TO_BE_DELETED_MESSAGE = _(
"Your application {id} will be deleted soon. If you want to continue the application process, please do so by "
Expand Down Expand Up @@ -62,17 +65,21 @@ def notify_applications(days_to_deletion: int, days_to_keep: int) -> int:
return applications_to_notify.count()


def get_draft_notice_email_notification_subject():
return str(_("Your Helsinki benefit draft application will expire"))


def _send_notification_mail(application: Application, days_to_deletion: int) -> int:
"""Send a notification mail to the applicant about the upcoming application deletion"""

subject = _("Your application is about to be deleted")
application_deletion_date = (
application.modified_at + timedelta(days=days_to_deletion)
).strftime("%d.%m.%Y")
context = get_email_template_context(application)
context["application_deletion_date"] = application_deletion_date

message = format_lazy(
APPLICATION_ABOUT_TO_BE_DELETED_MESSAGE,
id=application.application_number,
application_deletion_date=(
application.modified_at + timedelta(days=days_to_deletion)
).strftime("%d.%m.%Y"),
)
subject = get_draft_notice_email_notification_subject()
message = render_email_template(context, "draft-notice", "txt")
html_message = render_email_template(context, "draft-notice", "html")

return send_email_to_applicant(application, subject, message)
return send_email_to_applicant(application, subject, message, html_message)
8 changes: 7 additions & 1 deletion backend/benefit/applications/tests/test_applications_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
from companies.tests.factories import CompanyFactory
from helsinkibenefit.settings import MAX_UPLOAD_SIZE
from helsinkibenefit.tests.conftest import * # noqa
from messages.automatic_messages import (
get_additional_information_email_notification_subject,
)
from shared.audit_log import models as audit_models
from shared.service_bus.enums import YtjOrganizationCode
from terms.models import TermsOfServiceApproval
Expand Down Expand Up @@ -1282,7 +1285,10 @@ def test_application_status_change_as_handler(
in application.messages.first().content
)
assert len(mailoutbox) == 1
assert "You have received a new message" in mailoutbox[0].subject
assert (
get_additional_information_email_notification_subject()
in mailoutbox[0].subject
)

if to_status in [
ApplicationStatus.CANCELLED,
Expand Down
7 changes: 6 additions & 1 deletion backend/benefit/helsinkibenefit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,15 @@
"simple_history.middleware.HistoryRequestMiddleware",
]

MJML_COMPILED_EMAIL_TEMPLATE_PATHS = [
os.path.join(BASE_DIR, "helsinkibenefit/templates/emails/mjml-generated/html"),
os.path.join(BASE_DIR, "helsinkibenefit/templates/emails/mjml-generated/txt"),
]

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"DIRS": [] + MJML_COMPILED_EMAIL_TEMPLATE_PATHS,
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down
Loading

0 comments on commit 7939f5e

Please sign in to comment.