Skip to content

Commit

Permalink
Validate required fields in add
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Aug 9, 2023
1 parent 0a6b2ed commit b6370e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/redturtle/prenotazioni/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
"description",
]
DEFAULT_VISIBLE_BOOKING_FIELDS = ["email", "phone", "description"]
STATIC_REQUIRED_FIELDS = ["title"]
69 changes: 36 additions & 33 deletions src/redturtle/prenotazioni/restapi/services/booking/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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),
)
)
Expand All @@ -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()
19 changes: 11 additions & 8 deletions src/redturtle/prenotazioni/restapi/services/booking_schema/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = ""
Expand Down

0 comments on commit b6370e4

Please sign in to comment.