From ca11831d0704da97ae94bcee992b0037ab64fdde Mon Sep 17 00:00:00 2001 From: Matti Lamppu Date: Thu, 30 Jan 2025 16:19:05 +0200 Subject: [PATCH] Add Pindora API response to admin panel --- locale/fi/LC_MESSAGES/django.po | 12 +++++++ locale/sv/LC_MESSAGES/django.po | 12 +++++++ tilavarauspalvelu/admin/reservation/admin.py | 8 +++++ tilavarauspalvelu/admin/reservation/form.py | 33 ++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/locale/fi/LC_MESSAGES/django.po b/locale/fi/LC_MESSAGES/django.po index 22ce40684..a2fa472d0 100644 --- a/locale/fi/LC_MESSAGES/django.po +++ b/locale/fi/LC_MESSAGES/django.po @@ -1278,6 +1278,10 @@ msgstr "Varaajan tiedot" msgid "Billing information" msgstr "Laskutustiedot" +#: tilavarauspalvelu/admin/reservation/admin.py +msgid "Pindora information" +msgstr "Pindora tiedot" + #: tilavarauspalvelu/admin/reservation/admin.py msgid "None of the selected reservations can be denied." msgstr "Yhtään valittua varausta ei voida hylätä." @@ -1466,6 +1470,10 @@ msgstr "Varauksen maksajan postitoimipaikka" msgid "Billing address zip code" msgstr "Varauksen maksajan postinumero" +#: tilavarauspalvelu/admin/reservation/form.py +msgid "Pindora API response" +msgstr "Pindoran rajapinnan vastaus" + #: tilavarauspalvelu/admin/reservation/form.py msgid "User" msgstr "Käyttäjä" @@ -1616,6 +1624,10 @@ msgstr "Varaajan postinumero" msgid "Reservee's preferred language" msgstr "Varaajan suosima kieli" +#: tilavarauspalvelu/admin/reservation/form.py +msgid "Response from Pindora API" +msgstr "Vastaus Pindoran rajapinnasta" + #: tilavarauspalvelu/admin/reservation/form.py msgid "User who made the reservation" msgstr "Varauksen tehnyt käyttäjä" diff --git a/locale/sv/LC_MESSAGES/django.po b/locale/sv/LC_MESSAGES/django.po index afaf1acfe..cafb35c6b 100644 --- a/locale/sv/LC_MESSAGES/django.po +++ b/locale/sv/LC_MESSAGES/django.po @@ -1234,6 +1234,10 @@ msgstr "" msgid "Billing information" msgstr "" +#: tilavarauspalvelu/admin/reservation/admin.py +msgid "Pindora information" +msgstr "" + #: tilavarauspalvelu/admin/reservation/admin.py msgid "None of the selected reservations can be denied." msgstr "" @@ -1422,6 +1426,10 @@ msgstr "" msgid "Billing address zip code" msgstr "" +#: tilavarauspalvelu/admin/reservation/form.py +msgid "Pindora API response" +msgstr "" + #: tilavarauspalvelu/admin/reservation/form.py msgid "User" msgstr "" @@ -1572,6 +1580,10 @@ msgstr "" msgid "Reservee's preferred language" msgstr "" +#: tilavarauspalvelu/admin/reservation/form.py +msgid "Response from Pindora API" +msgstr "" + #: tilavarauspalvelu/admin/reservation/form.py msgid "User who made the reservation" msgstr "" diff --git a/tilavarauspalvelu/admin/reservation/admin.py b/tilavarauspalvelu/admin/reservation/admin.py index 6d5ca290d..b5c84396d 100644 --- a/tilavarauspalvelu/admin/reservation/admin.py +++ b/tilavarauspalvelu/admin/reservation/admin.py @@ -178,6 +178,14 @@ class ReservationAdmin(admin.ModelAdmin): ], }, ], + [ + _("Pindora information"), + { + "fields": [ + "pindora_response", + ], + }, + ], [ _("Additional information"), { diff --git a/tilavarauspalvelu/admin/reservation/form.py b/tilavarauspalvelu/admin/reservation/form.py index e83f1b9fa..7ced3ea4d 100644 --- a/tilavarauspalvelu/admin/reservation/form.py +++ b/tilavarauspalvelu/admin/reservation/form.py @@ -1,12 +1,19 @@ from __future__ import annotations +import json +from typing import Any + from django import forms from django.utils.translation import gettext_lazy as _ +from tilavarauspalvelu.enums import AccessType +from tilavarauspalvelu.integrations.keyless_entry import PindoraClient from tilavarauspalvelu.models import Reservation class ReservationAdminForm(forms.ModelForm): + pindora_response = forms.CharField(widget=forms.Textarea(attrs={"disabled": True, "cols": "40", "rows": "1"})) + class Meta: model = Reservation fields = [] # Use fields from ModelAdmin @@ -62,6 +69,8 @@ class Meta: "billing_address_city": _("Billing address city"), "billing_address_zip": _("Billing address zip code"), # + "pindora_response": _("Pindora API response"), + # "user": _("User"), "recurring_reservation": _("Recurring reservation"), "deny_reason": _("Reason for deny"), @@ -122,6 +131,8 @@ class Meta: "billing_address_city": _("Billing address city"), "billing_address_zip": _("Billing address zip code"), # + "pindora_response": _("Response from Pindora API"), + # "user": _("User who made the reservation"), "recurring_reservation": _("Recurring reservation"), "deny_reason": _("Reason for denying the reservation"), @@ -130,3 +141,25 @@ class Meta: "home_city": _("Reservee's home city"), "age_group": _("Age group of the group or association"), } + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + + 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 + + response = PindoraClient.get_cached_reservation_response(ext_uuid=obj.ext_uuid) + if response is None: + response = PindoraClient.get_reservation(reservation=obj) + PindoraClient.cache_reservation_response(response, ext_uuid=obj.ext_uuid) + + if response is None: + return None + + return json.dumps(response, default=str, indent=2)