Skip to content

Commit

Permalink
update booking schema adding booking_code field. the booking_code wil…
Browse files Browse the repository at this point in the history
…l be generated on booking creation
  • Loading branch information
luca-bellenghi committed Sep 5, 2023
1 parent bf1a824 commit 35ad8ad
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 231 deletions.
4 changes: 2 additions & 2 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

# use this extend one of the buildout configuration:
extends =
# test-6.0.x.cfg
test-5.2.x.cfg
test-6.0.x.cfg
# test-5.2.x.cfg
3 changes: 2 additions & 1 deletion src/redturtle/prenotazioni/browser/prenotazione_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def fields(self):
fields["booking_type"].widgetFactory = CustomRadioFieldWidget

# omit some fields
fields = fields.omit("gate").omit("booking_expiration_date").omit("staff_notes")
fields = fields.omit("gate").omit("booking_expiration_date")
fields = fields.omit("staff_notes").omit("booking_code")

# move title on top (after the type)
ids = [x for x in fields.keys()]
Expand Down
11 changes: 8 additions & 3 deletions src/redturtle/prenotazioni/content/prenotazione.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from zope.interface import implementer
from zope.schema import ValidationError

import hashlib
import re
import six

Expand Down Expand Up @@ -174,6 +173,13 @@ class IPrenotazione(model.Schema):
title=_("Expiration date booking"), required=True
)

directives.mode(booking_code="display")
booking_code = schema.TextLine(
title=_("Booking code"),
description=_("Codice univoco della prenotazione"),
required=False
)

staff_notes = schema.Text(
required=False, title=_("label_booking_staff_notes", "Staff notes")
)
Expand Down Expand Up @@ -255,8 +261,7 @@ def Date(self):
return DateTime(self.getBooking_date())

def getBookingCode(self):
hash_obj = hashlib.blake2b(bytes(self.UID(), encoding="utf8"), digest_size=3)
return hash_obj.hexdigest().upper()
return self.booking_code

def canAccessBooking(self):
creator = self.Creator()
Expand Down
6 changes: 6 additions & 0 deletions src/redturtle/prenotazioni/events/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
handler=".events_logger.on_modify"
/>

<subscriber
for="redturtle.prenotazioni.content.prenotazione.IPrenotazione
zope.lifecycleevent.IObjectAddedEvent"
handler=".prenotazione.set_booking_code"
/>

<subscriber
for="redturtle.prenotazioni.content.prenotazione.IPrenotazione
zope.lifecycleevent.IObjectAddedEvent"
Expand Down
16 changes: 13 additions & 3 deletions src/redturtle/prenotazioni/events/prenotazione.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
from plone import api
from plone.app.event.base import default_timezone
from plone.registry.interfaces import IRegistry
from plone.stringinterp.interfaces import IStringSubstitution
from Products.CMFPlone.interfaces.controlpanel import IMailSchema
from redturtle.prenotazioni import _
from redturtle.prenotazioni.utils import is_migration
from redturtle.prenotazioni.adapters.booker import IBooker
from redturtle.prenotazioni.interfaces import IPrenotazioneEmailMessage
from redturtle.prenotazioni.utils import is_migration
from zope.component import getAdapter
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.i18n import translate
from plone.stringinterp.interfaces import IStringSubstitution
from zope.component import getAdapter

import hashlib


logger = getLogger(__name__)

Expand Down Expand Up @@ -183,3 +186,10 @@ def send_email_to_managers(booking, event):
msg_type="text/html",
immediate=True,
)


def set_booking_code(booking, event):
hash_obj = hashlib.blake2b(bytes(booking.UID(), encoding="utf8"), digest_size=3)
hash_value = hash_obj.hexdigest().upper()
setattr(booking, "booking_code", hash_value)
return
Loading

0 comments on commit 35ad8ad

Please sign in to comment.