Skip to content

Commit

Permalink
fix update validation additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico authored and folix-01 committed Sep 27, 2024
1 parent 12c1ce7 commit a89faa0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
18 changes: 13 additions & 5 deletions src/redturtle/prenotazioni/content/prenotazione_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
42 changes: 14 additions & 28 deletions src/redturtle/prenotazioni/restapi/services/booking/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -30,7 +29,7 @@ def __call__(self, context):
SimpleTermFieldType(
"text",
"text",
translate(
context.translate(
_(
"label_booking_additional_field_textline",
default="Text line",
Expand Down

0 comments on commit a89faa0

Please sign in to comment.