diff --git a/tilavarauspalvelu/integrations/email/template_context/application.py b/tilavarauspalvelu/integrations/email/template_context/application.py index 3d3fe66d1..ce40ac821 100644 --- a/tilavarauspalvelu/integrations/email/template_context/application.py +++ b/tilavarauspalvelu/integrations/email/template_context/application.py @@ -4,7 +4,6 @@ from django.utils.translation import pgettext -from tilavarauspalvelu.models import RecurringReservation from tilavarauspalvelu.translation import get_attr_by_language, get_translated from .common import ( @@ -131,9 +130,8 @@ def get_context_for_application_section_cancelled( ) -> EmailContext: if application_section is not None: reservation = application_section.actions.get_last_reservation() - data: dict[str, Any] = { - "email_recipient_name": reservation.actions.get_email_reservee_name(), + "email_recipient_name": application_section.application.applicant, "cancel_reason": get_attr_by_language(reservation.cancel_reason, "reason", language), **params_for_application_section_info(application_section=application_section, language=language), } @@ -182,12 +180,6 @@ def get_context_for_staff_notification_application_section_cancelled( **data: Any, ) -> EmailContext: if application_section is not None: - reservation = application_section.actions.get_last_reservation() - - reservation_series = RecurringReservation.objects.filter( - allocated_time_slot__reservation_unit_option__application_section=application_section - ).prefetch_related("reservations") - reservation_series_data = [ { **params_for_reservation_series_info(reservation_series=series), @@ -195,9 +187,10 @@ def get_context_for_staff_notification_application_section_cancelled( reservation_id=series.reservations.values_list("pk").last() ), } - for series in reservation_series + for series in application_section.actions.get_reservation_series() ] + reservation = application_section.actions.get_last_reservation() data: dict[str, Any] = { "cancel_reason": get_attr_by_language(reservation.cancel_reason, "reason", language), "cancelled_reservation_series": reservation_series_data, diff --git a/tilavarauspalvelu/integrations/email/template_context/reservation.py b/tilavarauspalvelu/integrations/email/template_context/reservation.py index c24a7b349..26e39d130 100644 --- a/tilavarauspalvelu/integrations/email/template_context/reservation.py +++ b/tilavarauspalvelu/integrations/email/template_context/reservation.py @@ -635,7 +635,7 @@ def get_context_for_seasonal_reservation_modified_series( section = reservation_series.actions.get_application_section() data: dict[str, Any] = { - "email_recipient_name": reservation_series.actions.get_email_reservee_name(), + "email_recipient_name": section.application.applicant, **params_for_reservation_series_info(reservation_series=reservation_series), **params_for_application_section_info(application_section=section, language=language), } @@ -743,7 +743,7 @@ def get_context_for_seasonal_reservation_rejected_series( reservation = reservation_series.reservations.first() data: dict[str, Any] = { - "email_recipient_name": reservation_series.actions.get_email_reservee_name(), + "email_recipient_name": section.application.applicant, "rejection_reason": get_attr_by_language(reservation.deny_reason, "reason", language), **params_for_reservation_series_info(reservation_series=reservation_series), **params_for_application_section_info(application_section=section, language=language), diff --git a/tilavarauspalvelu/models/application/model.py b/tilavarauspalvelu/models/application/model.py index 90141f799..6e1ac21d7 100644 --- a/tilavarauspalvelu/models/application/model.py +++ b/tilavarauspalvelu/models/application/model.py @@ -234,19 +234,11 @@ def applicant() -> str: ), models.When( models.Q(contact_person__isnull=False), - then=Concat( - "contact_person__first_name", - models.Value(" "), - "contact_person__last_name", - ), + then=Concat("contact_person__first_name", models.Value(" "), "contact_person__last_name"), ), models.When( models.Q(user__isnull=False), - then=Concat( - "user__first_name", - models.Value(" "), - "user__last_name", - ), + then=Concat("user__first_name", models.Value(" "), "user__last_name"), ), default=models.Value(""), output_field=models.CharField(), diff --git a/tilavarauspalvelu/models/application_section/actions.py b/tilavarauspalvelu/models/application_section/actions.py index d8aa463d9..319ec2d9b 100644 --- a/tilavarauspalvelu/models/application_section/actions.py +++ b/tilavarauspalvelu/models/application_section/actions.py @@ -2,9 +2,11 @@ from typing import TYPE_CHECKING -from tilavarauspalvelu.models import Reservation +from tilavarauspalvelu.models import RecurringReservation, Reservation if TYPE_CHECKING: + from django.db.models import QuerySet + from tilavarauspalvelu.models import ApplicationSection @@ -12,6 +14,11 @@ class ApplicationSectionActions: def __init__(self, application_section: ApplicationSection) -> None: self.application_section = application_section + def get_reservation_series(self) -> QuerySet[RecurringReservation]: + return RecurringReservation.objects.filter( + allocated_time_slot__reservation_unit_option__application_section=self.application_section + ).prefetch_related("reservations") + def get_last_reservation(self) -> Reservation | None: return Reservation.objects.filter( user=self.application_section.application.user,