Skip to content

Commit

Permalink
Add Pindora API response to admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Jan 31, 2025
1 parent d13d1b9 commit ca11831
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions locale/fi/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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ä."
Expand Down Expand Up @@ -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ä"
Expand Down Expand Up @@ -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ä"
Expand Down
12 changes: 12 additions & 0 deletions locale/sv/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand Down
8 changes: 8 additions & 0 deletions tilavarauspalvelu/admin/reservation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ class ReservationAdmin(admin.ModelAdmin):
],
},
],
[
_("Pindora information"),
{
"fields": [
"pindora_response",
],
},
],
[
_("Additional information"),
{
Expand Down
33 changes: 33 additions & 0 deletions tilavarauspalvelu/admin/reservation/form.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand All @@ -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)

0 comments on commit ca11831

Please sign in to comment.