From b6370e416f5d9621de628bc712d78309582ebea0 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Wed, 9 Aug 2023 11:54:11 +0200 Subject: [PATCH] Validate required fields in add --- src/redturtle/prenotazioni/config.py | 1 + .../restapi/services/booking/add.py | 69 ++++++++++--------- .../restapi/services/booking_schema/get.py | 19 ++--- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/redturtle/prenotazioni/config.py b/src/redturtle/prenotazioni/config.py index 6b5f01b1..e2bf7c8f 100644 --- a/src/redturtle/prenotazioni/config.py +++ b/src/redturtle/prenotazioni/config.py @@ -15,3 +15,4 @@ "description", ] DEFAULT_VISIBLE_BOOKING_FIELDS = ["email", "phone", "description"] +STATIC_REQUIRED_FIELDS = ["title"] diff --git a/src/redturtle/prenotazioni/restapi/services/booking/add.py b/src/redturtle/prenotazioni/restapi/services/booking/add.py index 6d0c6b14..3fc8d294 100644 --- a/src/redturtle/prenotazioni/restapi/services/booking/add.py +++ b/src/redturtle/prenotazioni/restapi/services/booking/add.py @@ -2,17 +2,17 @@ from plone.protect.interfaces import IDisableCSRFProtection from plone.restapi.deserializer import json_body from plone.restapi.interfaces import ISerializeToJson -from plone.restapi.services import Service from redturtle.prenotazioni import _ from redturtle.prenotazioni.adapters.booker import IBooker +from redturtle.prenotazioni.restapi.services.booking_schema.get import BookingSchema +from zExceptions import BadRequest from zope.component import queryMultiAdapter from zope.interface import alsoProvides -from zExceptions import BadRequest # src/redturtle/prenotazioni/browser/prenotazione_add.py -class AddBooking(Service): +class AddBooking(BookingSchema): """ Add a new booking """ @@ -21,26 +21,51 @@ def reply(self): data = json_body(self.request) data_fields = {field["name"]: field["value"] for field in data["fields"]} - required = self.context.required_booking_fields or [] + self.validate() + + alsoProvides(self.request, IDisableCSRFProtection) + + booker = IBooker(self.context.aq_inner) + + book_data = { + "booking_date": data["booking_date"], + "booking_type": data["booking_type"], + } + for field in data_fields: + book_data[field] = data_fields[field] + + obj = booker.book(data=book_data) - # la tipologia di una prenotazione deve essere sempre obbligatoria ticket: 19131 + if not obj: + msg = self.context.translate( + _("Sorry, this slot is not available anymore.") + ) + raise BadRequest(msg) + + serializer = queryMultiAdapter((obj, self.request), ISerializeToJson) + return serializer() + + def validate(self): + data = json_body(self.request) + data_fields = {field["name"]: field["value"] for field in data["fields"]} + + missing_str = "Required input '${field}' is missing." + + # campi che non sono nei data_fields for field in ("booking_date", "booking_type"): if not data.get(field): msg = self.context.translate( _( - "Required input '${field}' is missing.", + missing_str, mapping=dict(field=field), ) ) raise BadRequest(msg) - - for field in required: - if field in ("booking_date", "booking_type"): - continue + for field in self.required_fields: if not data_fields.get(field): msg = self.context.translate( _( - "Required input '${field}' is missing.", + missing_str, mapping=dict(field=field), ) ) @@ -56,25 +81,3 @@ def reply(self): ) ) raise BadRequest(msg) - - alsoProvides(self.request, IDisableCSRFProtection) - - booker = IBooker(self.context.aq_inner) - - book_data = { - "booking_date": data["booking_date"], - "booking_type": data["booking_type"], - } - for field in data_fields: - book_data[field] = data_fields[field] - - obj = booker.book(data=book_data) - - if not obj: - msg = self.context.translate( - _("Sorry, this slot is not available anymore.") - ) - raise BadRequest(msg) - - serializer = queryMultiAdapter((obj, self.request), ISerializeToJson) - return serializer() diff --git a/src/redturtle/prenotazioni/restapi/services/booking_schema/get.py b/src/redturtle/prenotazioni/restapi/services/booking_schema/get.py index 486d4721..7f0a2b2f 100644 --- a/src/redturtle/prenotazioni/restapi/services/booking_schema/get.py +++ b/src/redturtle/prenotazioni/restapi/services/booking_schema/get.py @@ -5,10 +5,10 @@ from redturtle.prenotazioni import _ from redturtle.prenotazioni import datetime_with_tz from zExceptions import BadRequest +from redturtle.prenotazioni.config import STATIC_REQUIRED_FIELDS class BookingSchema(Service): - static_required_fields = ["title"] field_type_mapping = {"description": "textarea"} # TODO: rendere traudcibili label e descrizione @@ -42,13 +42,16 @@ class BookingSchema(Service): @property @memoize def required_fields(self): - required_booking_fields = getattr( - self.context, - "required_booking_fields", - [], + required_booking_fields = ( + getattr( + self.context, + "required_booking_fields", + [], + ) + or [] ) fields = [x for x in required_booking_fields] - for field in self.static_required_fields: + for field in STATIC_REQUIRED_FIELDS: if field not in fields: fields.append(field) return fields @@ -75,11 +78,11 @@ def reply(self): current_user = api.user.get_current() fields_list = [] - fields = [x for x in self.static_required_fields] + fields = [x for x in STATIC_REQUIRED_FIELDS] fields += [ x for x in self.context.visible_booking_fields - if x not in self.static_required_fields + if x not in STATIC_REQUIRED_FIELDS ] for field in fields: value = ""