Skip to content

Commit

Permalink
Merge branch 'master' into booking_serializer_extend
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Jan 10, 2024
2 parents 58bbacb + e9f5d22 commit 16b75e0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/redturtle/prenotazioni/adapters/booker.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ def book(self, data, force_gate=None, duration=-1):
if not getattr(self.context, "auto_confirm", False) and getattr(
self.context, "auto_confirm_manager", False
):
# if self.context.auto_confirm is True, auto confirm is made in event handler for all
api.content.transition(obj=obj, transition="confirm")
if api.content.get_state(obj) != "confirmed":
api.content.transition(obj, transition="confirm")
return obj

def move(self, booking, data):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import json

from plone import api
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.interfaces import ISerializeToJson
Expand Down Expand Up @@ -138,6 +140,31 @@ def __call__(self, *args, **kwargs):
)()
except ComponentLookupError:
requirements = ""
prenotazioni_folder = self.prenotazione.getPrenotazioniFolder()
data["booking_folder"] = {
"@id": prenotazioni_folder.absolute_url(),
"uid": prenotazioni_folder.UID(),
"title": prenotazioni_folder.Title(),
"orario_di_apertura": getattr(
prenotazioni_folder, "orario_di_apertura", None
),
"description_agenda": json_compatible(
prenotazioni_folder.descriptionAgenda,
prenotazioni_folder,
),
# BBB
"address": {},
}
for other in ["booking_address", "booking_office"]:
if getattr(self.prenotazione, other, None):
try:
data[other] = json.loads(getattr(self.prenotazione, other))
except Exception:
logger.warning(
"%s field is not JSON serializable: %s",
other,
getattr(self.prenotazione, other),
)
data["requirements"] = requirements
data["cosa_serve"] = requirements # BBB
return data
45 changes: 45 additions & 0 deletions src/redturtle/prenotazioni/restapi/services/booking/add.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# -*- coding: utf-8 -*-
import json
from urllib.parse import urlparse

from plone import api
from plone.protect.interfaces import IDisableCSRFProtection
from plone.restapi.deserializer import json_body
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.interfaces import ISerializeToJsonSummary
from zExceptions import BadRequest
from zope.component import getMultiAdapter
from zope.component import queryMultiAdapter
from zope.interface import alsoProvides

from redturtle.prenotazioni import _
from redturtle.prenotazioni import logger
from redturtle.prenotazioni.adapters.booker import BookerException
from redturtle.prenotazioni.adapters.booker import IBooker
from redturtle.prenotazioni.content.prenotazione import VACATION_TYPE
Expand Down Expand Up @@ -59,9 +65,48 @@ def reply(self):
)
raise BadRequest(msg)

# other_fields: {
# booking_address: prenotazioneObj.booking_folder?.address?.['@id'],
# booking_office: prenotazioneObj.booking_office?.['url'],
# booking_contact_info: (prenotazioneObj.booking_office?.contact_info || []).map((c) => c['@id']),
# },
if "other_fields" in data:
# XXX: i dati arrivano da un utente, eventualmente anche anonimo non possiamo
# permetterci di salvare i dati raw che arrivano, piuttosto salviamo
# solo la url dell'uffico di riferimento da cui poi recuperare i dati
# in fase di visualizzazione o salviamo i dati serializzati ora a partire dalla url
# passata dall'utente. Per ora la seconda opzione è più conservativa nel caso
# in cui l'ufficio cambi i dati dopo la prenotazione o venga eliminato.
other = data["other_fields"]
if other.get("booking_office", None):
# XXX: aggiungere 'booking_office' nello schema di booking ?
self.save_other_field(obj, "booking_office", other["booking_office"])
if other.get("booking_address", None):
self.save_other_field(obj, "booking_address", other["booking_address"])
# XXX: da valutare
# if other.get("booking_contact_info", None):

serializer = queryMultiAdapter((obj, self.request), ISerializeToJson)
return serializer()

def save_other_field(self, obj, field, value):
try:
# TODO: usare volto_frontend_url
path = urlparse(value).path
other = api.content.get(path)
if other:
setattr(
obj,
field,
json.dumps(
getMultiAdapter(
(other, self.request), ISerializeToJsonSummary
)()
),
)
except Exception:
logger.exception("Error while saving %s field %s for %s", value, field, obj)

def validate(self):
data = json_body(self.request)
data_fields = {field["name"]: field["value"] for field in data["fields"]}
Expand Down
3 changes: 2 additions & 1 deletion src/redturtle/prenotazioni/tests/test_available_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def setUp(self):
def tearDown(self):
self.api_session.close()

@freeze_time(date(date.today().year, date.today().month, 2))
# XXX: freezgun doesn't work with self.api_session
# @freeze_time(date(date.today().year, date.today().month, 2))
def test_month_slots_called_without_params_return_all_available_slots_of_current_month(
self,
):
Expand Down

0 comments on commit 16b75e0

Please sign in to comment.