diff --git a/backend/tilavarauspalvelu/admin/email_template/forms.py b/backend/tilavarauspalvelu/admin/email_template/forms.py index 04678b2d6..928bcf804 100644 --- a/backend/tilavarauspalvelu/admin/email_template/forms.py +++ b/backend/tilavarauspalvelu/admin/email_template/forms.py @@ -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 @@ -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), @@ -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 diff --git a/backend/tilavarauspalvelu/admin/email_template/tester.py b/backend/tilavarauspalvelu/admin/email_template/tester.py index 09307e33e..b72e2b147 100644 --- a/backend/tilavarauspalvelu/admin/email_template/tester.py +++ b/backend/tilavarauspalvelu/admin/email_template/tester.py @@ -9,7 +9,7 @@ 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 @@ -17,7 +17,7 @@ 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 @@ -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() @@ -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 = { @@ -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)