diff --git a/src/redturtle/prenotazioni/content/prenotazione_type.py b/src/redturtle/prenotazioni/content/prenotazione_type.py index 52ccb122..393fa36c 100644 --- a/src/redturtle/prenotazioni/content/prenotazione_type.py +++ b/src/redturtle/prenotazioni/content/prenotazione_type.py @@ -28,11 +28,6 @@ class IBookingAdditionalFieldsSchema(model.Schema): required=True, default="", ) - description = schema.TextLine( - title=_("booking_additional_field_description", default="Descrizione"), - required=False, - default="", - ) type = schema.Choice( title=_("booking_additional_field_type", default="Tipo"), required=True, @@ -43,6 +38,11 @@ class IBookingAdditionalFieldsSchema(model.Schema): required=False, default=False, ) + description = schema.TextLine( + title=_("booking_additional_field_description", default="Descrizione"), + required=False, + default="", + ) class IPrenotazioneType(model.Schema): @@ -104,6 +104,14 @@ class IPrenotazioneType(model.Schema): }, ) + model.fieldset( + "additional_fields", + label=_("booking_additional_fields_schema_title"), + fields=[ + "booking_additional_fields_schema", + ], + ) + @implementer(IPrenotazioneType) class PrenotazioneType(Item): diff --git a/src/redturtle/prenotazioni/restapi/services/booking/update.py b/src/redturtle/prenotazioni/restapi/services/booking/update.py index 583877df..cd704297 100644 --- a/src/redturtle/prenotazioni/restapi/services/booking/update.py +++ b/src/redturtle/prenotazioni/restapi/services/booking/update.py @@ -41,48 +41,34 @@ def handle_additional_fields(self): booking_type = self.context.get_booking_type() + # validate additional fields + # TODO: refactor this code to a separate method to avoid code duplication for field_schema in booking_type.booking_additional_fields_schema or []: - field = list( - filter( - lambda i: i.get("name") == field_schema.get("name"), - additional_fields, - ) - ) - - if not field and field_schema.get("required", False): - raise BadRequest( - api.portal.translate( - _( - "Additional field '${additional_field_name}' is missing.", - mapping=dict( - additional_field_name=field_schema.get("name") - ), - ) - ) - ) - elif not field: - continue - - field = field[0] - - try: - if not field.get("value"): + field = None + for additional_field in additional_fields: + if additional_field.get("name") == field_schema.get("name"): + field = additional_field + break + + if field_schema.get("required", False): + if not field or not field.get("value"): raise BadRequest( api.portal.translate( _( - "Additional field '${additional_field_name}' value is missing.", + "Additional field '${additional_field_name}' is missing.", mapping=dict( additional_field_name=field_schema.get("name") ), ) ) ) + elif not field: + continue - # Validation + try: field_types_validators.get(field_schema.get("type"))( field.get("value") ) - except ValidationError as e: raise BadRequest( api.portal.translate( diff --git a/src/redturtle/prenotazioni/vocabularies/voc_booking_additional_fiels_types.py b/src/redturtle/prenotazioni/vocabularies/voc_booking_additional_fiels_types.py index 138c9fe0..c7e501ca 100644 --- a/src/redturtle/prenotazioni/vocabularies/voc_booking_additional_fiels_types.py +++ b/src/redturtle/prenotazioni/vocabularies/voc_booking_additional_fiels_types.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from zope.i18n import translate from zope.interface import implementer from zope.schema import TextLine from zope.schema.interfaces import IVocabularyFactory @@ -30,7 +29,7 @@ def __call__(self, context): SimpleTermFieldType( "text", "text", - translate( + context.translate( _( "label_booking_additional_field_textline", default="Text line",