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 16, 2024
1 parent 9595d8f commit e2c9814
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 31 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 @@ -632,10 +632,10 @@ def get_context_for_seasonal_reservation_modified_series(
**data: Any,
) -> EmailContext:
if reservation_series is not None:
section = reservation_series.actions.get_application_section()
section = reservation_series.allocated_time_slot.reservation_unit_option.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 @@ -739,11 +739,11 @@ def get_context_for_seasonal_reservation_rejected_series(
**data: Any,
) -> EmailContext:
if reservation_series is not None:
section = reservation_series.actions.get_application_section()
section = reservation_series.allocated_time_slot.reservation_unit_option.application_section
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
7 changes: 1 addition & 6 deletions tilavarauspalvelu/models/recurring_reservation/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING, Any, TypedDict

from tilavarauspalvelu.enums import RejectionReadinessChoice
from tilavarauspalvelu.models import AffectingTimeSpan, ApplicationSection, RejectedOccurrence, Reservation
from tilavarauspalvelu.models import AffectingTimeSpan, RejectedOccurrence, Reservation
from tilavarauspalvelu.utils.opening_hours.time_span_element import TimeSpanElement
from utils.date_utils import DEFAULT_TIMEZONE, combine, get_periods_between

Expand Down Expand Up @@ -328,11 +328,6 @@ def bulk_create_rejected_occurrences_for_periods(

return RejectedOccurrence.objects.bulk_create(occurrences)

def get_application_section(self) -> ApplicationSection | None:
return ApplicationSection.objects.filter(
reservation_unit_options__allocated_time_slots__recurring_reservation=self.recurring_reservation
).first()

def get_email_reservee_name(self) -> str:
reservation: Reservation | None = self.recurring_reservation.reservations.last()

Expand Down

0 comments on commit e2c9814

Please sign in to comment.