Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add series pindora info to admin panel #1559

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions backend/tilavarauspalvelu/admin/reservation/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tilavarauspalvelu.enums import AccessType
from tilavarauspalvelu.integrations.keyless_entry import PindoraClient
from tilavarauspalvelu.models import Reservation
from utils.date_utils import DEFAULT_TIMEZONE


class ReservationAdminForm(forms.ModelForm):
Expand Down Expand Up @@ -148,18 +149,39 @@ class Meta:
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

if (
self.instance
and self.instance.recurring_reservation is None
and self.instance.access_type == AccessType.ACCESS_CODE
):
if self.instance and self.instance.access_type == AccessType.ACCESS_CODE:
pindora_field = self.fields["pindora_response"]
pindora_field.initial = self.get_pindora_response(self.instance)
pindora_field.widget.attrs.update({"cols": "100", "rows": "20"})

def get_pindora_response(self, obj: Reservation) -> str | None:
if obj.access_type != AccessType.ACCESS_CODE:
return None
if self.instance.recurring_reservation is None:
response = PindoraClient.get_reservation(reservation=self.instance)
pindora_field.initial = json.dumps(response, default=str, indent=2)

response = PindoraClient.get_reservation(reservation=obj)
return json.dumps(response, default=str, indent=2)
elif self.instance.recurring_reservation.allocated_time_slot is None:
response = PindoraClient.get_reservation_series(series=self.instance.recurring_reservation)

# Only show the validity for this reservation
response["reservation_unit_code_validity"] = [
item
for item in response["reservation_unit_code_validity"]
if item["begin"] == self.instance.begin.astimezone(DEFAULT_TIMEZONE)
and item["end"] == self.instance.end.astimezone(DEFAULT_TIMEZONE)
]

pindora_field.initial = json.dumps(response, default=str, indent=2)

else:
allocation = self.instance.recurring_reservation.allocated_time_slot
section = allocation.reservation_unit_option.application_section
Comment on lines +172 to +173
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have this helper: self.instance.actions.get_application_section()

response = PindoraClient.get_seasonal_booking(section=section)

# Only show the validity for this reservation
response["reservation_unit_code_validity"] = [
item
for item in response["reservation_unit_code_validity"]
if item["begin"] == self.instance.begin.astimezone(DEFAULT_TIMEZONE)
and item["end"] == self.instance.end.astimezone(DEFAULT_TIMEZONE)
and item["reservation_unit_id"] == self.instance.recurring_reservation.reservation_unit.uuid
]

pindora_field.initial = json.dumps(response, default=str, indent=2)