Skip to content

Commit

Permalink
Email: Simplify getting EmailTesterForm
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta committed Feb 17, 2025
1 parent f056934 commit 70cc3f4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
17 changes: 6 additions & 11 deletions backend/tilavarauspalvelu/admin/email_template/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@
from tilavarauspalvelu.typing import EmailContext, Lang

__all__ = [
"select_tester_form",
"EmailTesterForm",
]


def select_tester_form(*, email_type: EmailTemplateType) -> type[EmailTesterForm]:
"""Select email tester form based on email type."""
email_form_class = EmailTesterForm
email_form_class.email_type = email_type
return email_form_class


class EmailTesterForm(forms.BaseForm):
email_type: EmailTemplateType

def __init__(self, **kwargs: Any) -> None:
def __init__(self, email_type: EmailTemplateType, **kwargs: Any) -> None:
self.email_type = email_type

email_tester_fields = get_email_tester_form_fields()

# Select only the fields required for the email type
Expand All @@ -46,7 +41,7 @@ def to_context(self) -> EmailContext:
return self.email_type.get_email_context(**context_params)

@classmethod
def from_reservation_unit(cls, instance: ReservationUnit, *, language: Lang) -> Self:
def from_reservation_unit(cls, email_type: EmailTemplateType, instance: ReservationUnit, *, language: Lang) -> Self:
"""Initialise the form with data form from reservation unit information."""
initial = {
"reservation_unit_name": get_attr_by_language(instance, "name", language),
Expand All @@ -63,7 +58,7 @@ def from_reservation_unit(cls, instance: ReservationUnit, *, language: Lang) ->
"access_code_validity_period": "",
})

return cls(initial=initial)
return cls(email_type=email_type, initial=initial)


WIDTH = 50
Expand Down
21 changes: 10 additions & 11 deletions backend/tilavarauspalvelu/admin/email_template/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND
from rest_framework.status import HTTP_400_BAD_REQUEST

from tilavarauspalvelu.enums import Language
from tilavarauspalvelu.integrations.email.sending import send_emails_in_batches_task
from tilavarauspalvelu.integrations.email.typing import EmailData, EmailType
from tilavarauspalvelu.models import ReservationUnit
from utils.utils import safe_getattr

from .forms import select_tester_form
from .forms import EmailTesterForm

if TYPE_CHECKING:
from tilavarauspalvelu.typing import WSGIRequest
Expand Down Expand Up @@ -69,17 +69,12 @@ def email_tester_admin_view(request: WSGIRequest, email_type: str) -> HttpRespon
except ValueError:
return HttpResponse(f"Invalid email type: {email_type}", HTTP_400_BAD_REQUEST)

tester_form_class = select_tester_form(email_type=email_type_class)
if tester_form_class is None:
return HttpResponse(f"No form defined for email type: {email_type_class}", status=HTTP_404_NOT_FOUND)

reservation_unit_pk: str | None = request.GET.get("reservation_unit", None)

reservation_unit_form = ReservationUnitSelectForm()
template_switcher_form = TemplateSwitcherForm(initial={"email_type": email_type})

if request.method == "POST":
tester_form = tester_form_class(data=request.POST)
tester_form = EmailTesterForm(email_type=email_type_class, data=request.POST)
if tester_form.is_valid():
recipients = [tester_form.cleaned_data["send_to"]]
context = tester_form.to_context()
Expand All @@ -93,11 +88,15 @@ def email_tester_admin_view(request: WSGIRequest, email_type: str) -> HttpRespon
)
elif reservation_unit_pk:
reservation_unit = ReservationUnit.objects.get(id=int(reservation_unit_pk))
tester_form = tester_form_class.from_reservation_unit(reservation_unit, language=Language.FI.value)
tester_form = EmailTesterForm.from_reservation_unit(
email_type=email_type_class,
instance=reservation_unit,
language=Language.FI.value,
)
tester_form.initial["send_to"] = request.user.email or ""
reservation_unit_form.initial["reservation_unit"] = reservation_unit.pk
else:
tester_form = tester_form_class()
tester_form = EmailTesterForm(email_type=email_type_class)
tester_form.initial["send_to"] = request.user.email or ""

context = {
Expand All @@ -107,7 +106,7 @@ def email_tester_admin_view(request: WSGIRequest, email_type: str) -> HttpRespon
"app_view_name": admin_data_settings.NAME, # For breadcrumbs
"form": tester_form,
"reservation_unit_form": reservation_unit_form,
"template_switcher_form": template_switcher_form,
"template_switcher_form": TemplateSwitcherForm(initial={"email_type": email_type}),
}

return TemplateResponse(request, "email/email_tester.html", context=context)

0 comments on commit 70cc3f4

Please sign in to comment.