Skip to content

Commit

Permalink
Get seasonal email recipient name from application, not reservation
Browse files Browse the repository at this point in the history
  • Loading branch information
ranta committed Dec 13, 2024
1 parent 4cb26a5 commit 32ddc13
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -182,22 +180,17 @@ 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),
"reservation_url": get_staff_reservations_ext_link(
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 2 additions & 10 deletions tilavarauspalvelu/models/application/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
9 changes: 8 additions & 1 deletion tilavarauspalvelu/models/application_section/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

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


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,
Expand Down

0 comments on commit 32ddc13

Please sign in to comment.