Skip to content

Commit

Permalink
Merge branch 'customizable_from_address' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Jan 8, 2024
2 parents fee6f4d + 3e0dead commit 842e33e
Show file tree
Hide file tree
Showing 12 changed files with 1,494 additions and 1,288 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Changelog
- Add booking reminder emails.
[folix-01]

- Move code generation to adapter, to be more customizable.
[cekk]

- Add customizable email from in PrenotazioniFolder contents.
[cekk]

2.3.2 (2024-01-03)
------------------
Expand Down
29 changes: 28 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,33 @@ bookings within a given time interval.
You can also filter the results specifying a searchable text,
a gate or a review state.

Booking code generation
-----------------------

Every booking has an unique code generated on creation.

By default this code is based on its UID.
If you need to change this logic, you can do it registering a more specific adapter::

<adapter factory=".my_new_code.MyNewBookingCodeGenerator" />


And the adapter should be something like this::

from redturtle.prenotazioni.adapters.booking_code import BookingCodeGenerator
from redturtle.prenotazioni.adapters.booking_code import IBookingCodeGenerator
from redturtle.prenotazioni.content.prenotazione import IPrenotazione
from my.package.interfaces import IMyPackageLayer
from zope.component import adapter
from zope.interface import implementer


@implementer(IBookingCodeGenerator)
@adapter(IPrenotazione, IMyPackageLayer)
class MyNewBookingCodeGenerator(BookingCodeGenerator):
def __call__(self, *args, **kwargs):
return "XXXXX"


Rest API
========
Expand Down Expand Up @@ -463,7 +490,7 @@ Response::
}

@booking-schema
--------------------
---------------

Endpoint that need to be called on a PrenotazioniFolder.
It returns the list of all fields to fill in for the booking.
Expand Down
28 changes: 28 additions & 0 deletions src/redturtle/prenotazioni/adapters/booking_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from redturtle.prenotazioni.content.prenotazione import IPrenotazione
from zope.component import adapter
from zope.component import Interface
from zope.interface import implementer

import hashlib


class IBookingCodeGenerator(Interface):
"""
Adapter interface to generate a code
"""


@implementer(IBookingCodeGenerator)
@adapter(IPrenotazione, Interface)
class BookingCodeGenerator:
def __init__(self, context, request):
self.context = context
self.request = request

def __call__(self, *args, **kwargs):
hash_obj = hashlib.blake2b(
bytes(self.context.UID(), encoding="utf8"), digest_size=3
)

return hash_obj.hexdigest().upper()
3 changes: 3 additions & 0 deletions src/redturtle/prenotazioni/adapters/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,7 @@
/>


<!-- code generator -->
<adapter factory=".booking_code.BookingCodeGenerator" />

</configure>
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ class INotificationEmail(model.Schema):
default=True,
required=False,
)
email_from = schema.TextLine(
title=_("Email from"),
description=_(
'Insert an email address used as "from" in email notifications. '
"Leave empty to use the default from set in the site configuration."
),
required=False,
default="",
)
notify_on_submit_subject = schema.TextLine(
title=_(
"notify_on_submit_subject",
Expand Down Expand Up @@ -260,6 +269,7 @@ class INotificationEmail(model.Schema):
),
fields=[
"notifications_email_enabled",
"email_from",
"notify_on_submit_subject",
"notify_on_submit_message",
"notify_on_confirm_subject",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
class PrenotazioneEmailMessage:
prenotazione = None
event = None
error_msg = "Could not send notification email due to: {message}"

def __init__(self, prenotazione, event):
self.prenotazione = prenotazione
Expand All @@ -45,20 +46,32 @@ def message_text(self) -> MIMEText:
raise NotImplementedError("The method was not implemented")

@property
def message(self) -> MIMEMultipart:
error_msg = "Could not send notification email due to: {message}"
def prenotazioni_folder(self):
return self.prenotazione.getPrenotazioniFolder()

@property
def message_from(self) -> str:
"""
Return the proper from address.
If set in PrenotazioniFolder, return that value, otherwise, return the site one.
"""
mfrom = api.portal.get_registry_record("plone.email_from_address")
return getattr(self.prenotazioni_folder, "email_from", "") or mfrom

@property
def message(self) -> MIMEMultipart:
mfrom = self.message_from
recipient = self.prenotazione.email

if not mfrom:
logger.error(
error_msg.format(message="Email from address is not configured")
self.error_msg.format(message="Email from address is not configured")
)
return None

if not recipient:
logger.error(
error_msg.format(
self.error_msg.format(
message="Could not find recipients for the email message"
)
)
Expand Down Expand Up @@ -108,21 +121,13 @@ class PrenotazioneMovedICalEmailMessage(
@property
def message_subject(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_on_move_subject",
"",
)
getattr(self.prenotazioni_folder, "notify_on_move_subject", "")
)

@property
def message_text(self) -> MIMEText:
text = IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_on_move_message",
None,
),
getattr(self.prenotazioni_folder, "notify_on_move_message", None),
)
if CTE:
cs = Charset("utf-8")
Expand All @@ -139,7 +144,7 @@ class PrenotazioneAfterTransitionEmailMessage(PrenotazioneEmailMessage):
def message_subject(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
self.prenotazioni_folder,
f"notify_on_{self.event.transition and self.event.transition.__name__}_subject",
"",
)
Expand All @@ -149,7 +154,7 @@ def message_subject(self) -> str:
def message_text(self) -> MIMEText:
text = IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
self.prenotazioni_folder,
f"notify_on_{self.event.transition and self.event.transition.__name__}_message",
None,
),
Expand Down Expand Up @@ -188,13 +193,12 @@ def message(self) -> MIMEMultipart:
"""
customized to send Bcc instead To
"""
error_msg = "Could not send notification email due to: {message}"
mfrom = api.portal.get_registry_record("plone.email_from_address")
mfrom = self.message_from
bcc = ", ".join(getattr(self.prenotazione, "email_responsabile", []))

if not mfrom:
logger.error(
error_msg.format(message="Email from address is not configured")
self.error_msg.format(message="Email from address is not configured")
)
return None

Expand Down Expand Up @@ -277,21 +281,13 @@ class PrenotazioneReminderEmailMessage(PrenotazioneEmailMessage):
@property
def message_subject(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_as_reminder_subject",
"",
)
getattr(self.prenotazioni_folder, "notify_as_reminder_subject", "")
)

@property
def message_text(self) -> MIMEText:
text = IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_as_reminder_message",
None,
),
getattr(self.prenotazioni_folder, "notify_as_reminder_message", None),
)
if CTE:
cs = Charset("utf-8")
Expand Down
11 changes: 5 additions & 6 deletions src/redturtle/prenotazioni/events/prenotazione.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# -*- coding: utf-8 -*-
import hashlib
from email.utils import formataddr
from email.utils import parseaddr

from plone import api
from plone.registry.interfaces import IRegistry
from Products.CMFPlone.interfaces.controlpanel import IMailSchema
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.globalrequest import getRequest

from redturtle.prenotazioni import is_migration
from redturtle.prenotazioni.adapters.booker import IBooker
from redturtle.prenotazioni.adapters.booking_code import IBookingCodeGenerator


def reallocate_gate(obj):
Expand Down Expand Up @@ -68,8 +70,5 @@ def set_booking_code(booking, event):
"""
if is_migration():
return

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
booking_code = getMultiAdapter((booking, getRequest()), IBookingCodeGenerator)()
setattr(booking, "booking_code", booking_code)
Loading

0 comments on commit 842e33e

Please sign in to comment.