Skip to content

Commit

Permalink
Booking canceled message + wf states translate (#207)
Browse files Browse the repository at this point in the history
* Init

* Add logics

* Translations

* Translate state

* Changelog
  • Loading branch information
folix-01 authored May 22, 2024
1 parent 3fb7fef commit a8b9d3e
Show file tree
Hide file tree
Showing 18 changed files with 825 additions and 286 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Changelog
- Write to history if could not send booking notification
[folix-01]

- Booking canceled message + wf states translations
[folix-01]



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ def notify_on_refuse_appio_message_default_factory(context):
)


@provider(IContextAwareDefaultFactory)
def notify_on_cancel_appio_subject_default_factory(context):
return api.portal.translate(
_(
"notify_on_cancel_appio_subject_default_value",
"[${prenotazioni_folder_title}] Booking canceled for ${title}",
)
)


@provider(IContextAwareDefaultFactory)
def notify_on_cancel_appio_message_default_factory(context):
return api.portal.translate(
_(
"notify_on_cancel_appio_message_default_value",
"The booking ${booking_type} of ${booking_date} at ${booking_time} was canceled.",
)
)


@provider(IContextAwareDefaultFactory)
def notify_as_reminder_appio_subject_default_factory(context):
return api.portal.translate(
Expand Down Expand Up @@ -223,6 +243,30 @@ class INotificationAppIO(model.Schema):
defaultFactory=notify_on_refuse_appio_message_default_factory,
required=False,
)
notify_on_cancel_appio_subject = schema.Text(
title=_(
"notify_on_cancel_subject",
default="[Cancel] subject",
),
description=_(
"notify_on_cancel_subject_help",
default="The message subject when a booking has been canceled.",
),
defaultFactory=notify_on_cancel_appio_subject_default_factory,
required=False,
)
notify_on_cancel_appio_message = schema.Text(
title=_(
"notify_on_cancel_message",
default="[Cancel] message",
),
description=_(
"notify_on_cancel_message_help",
default="The message text when a booking has been canceled.",
),
defaultFactory=notify_on_cancel_appio_message_default_factory,
required=False,
)
notify_as_reminder_appio_subject = schema.Text(
title=_(
"notify_as_reminder_subject",
Expand Down Expand Up @@ -268,6 +312,8 @@ class INotificationAppIO(model.Schema):
"notify_on_move_appio_message",
"notify_on_refuse_appio_subject",
"notify_on_refuse_appio_message",
"notify_on_cancel_appio_subject",
"notify_on_cancel_appio_message",
"notify_as_reminder_appio_subject",
"notify_as_reminder_appio_message",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,24 @@ def send_booking_reminder(context, event):
name="booking_transition_appio_sender",
)
sender_adapter.send()


@handle_exception_by_log
@notify_the_message_failure
def send_booking_removed(context, event):
if not booking_folder_provides_current_behavior(context):
return

message_adapter = getMultiAdapter(
(context, event),
IBookingAPPIoMessage,
name="removed_notification_appio_message",
)

if message_adapter and message_adapter.message:
sender_adapter = getMultiAdapter(
(message_adapter, context, getRequest()),
IBookingNotificationSender,
name="booking_transition_appio_sender",
)
sender_adapter.send()
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,27 @@ def subject(self) -> str:
"",
)
)


@implementer(IBookingAPPIoMessage)
@adapter(IPrenotazione, IBookingReminderEvent)
class PrenotazioneRemovedAppIOMessage(PrenotazioneAPPIoMessage):
@property
def message(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_as_removed_appio_message",
"",
)
)

@property
def subject(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_as_removed_appio_subject",
"",
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ def notify_on_refuse_message_default_factory(context):
)


@provider(IContextAwareDefaultFactory)
def notify_on_cancel_subject_default_factory(context):
return api.portal.translate(
_(
"notify_on_cancel_subject_default_value",
"[${prenotazioni_folder_title}] Booking canceled for ${title}",
)
)


@provider(IContextAwareDefaultFactory)
def notify_on_cancel_message_default_factory(context):
return api.portal.translate(
_(
"notify_on_cancel_message_default_value",
"The booking ${booking_type} of ${booking_date} at ${booking_time} was canceled.",
)
)


@provider(IContextAwareDefaultFactory)
def notify_as_reminder_subject_default_factory(context):
return api.portal.translate(
Expand Down Expand Up @@ -232,6 +252,30 @@ class INotificationEmail(model.Schema):
defaultFactory=notify_on_refuse_message_default_factory,
required=False,
)
notify_on_cancel_subject = schema.TextLine(
title=_(
"notify_on_cancel_subject",
default="[Cancel] subject",
),
description=_(
"notify_on_cancel_subject_help",
default="The message subject when a booking has been canceled.",
),
defaultFactory=notify_on_cancel_subject_default_factory,
required=False,
)
notify_on_cancel_message = schema.Text(
title=_(
"notify_on_cancel_message",
default="[Cancel] message",
),
description=_(
"notify_on_cancel_message_help",
default="The message text when a booking has been canceled.",
),
defaultFactory=notify_on_cancel_message_default_factory,
required=False,
)
notify_as_reminder_subject = schema.TextLine(
title=_(
"notify_as_reminder_subject",
Expand Down Expand Up @@ -278,6 +322,8 @@ class INotificationEmail(model.Schema):
"notify_on_move_message",
"notify_on_refuse_subject",
"notify_on_refuse_message",
"notify_on_cancel_subject",
"notify_on_cancel_message",
"notify_as_reminder_subject",
"notify_as_reminder_message",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def send(self, force=False):
If True, the message will be sent even if the email is not allowed
(ie. for operator notifications)
"""

message = self.message_adapter.message

if force or getUtility(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ def send_booking_reminder(context, event):
sender_adapter.send()


@notify_the_message_failure
def send_booking_removed(context, event):
if not booking_folder_provides_current_behavior(context):
return

message_adapter = getMultiAdapter(
(context, event),
IBookingEmailMessage,
name="removed_notification_email_message",
)
sender_adapter = getMultiAdapter(
(message_adapter, context, getRequest()),
IBookingNotificationSender,
name="booking_transition_email_sender",
)

sender_adapter.send()


def send_booking_canceled_to_managers(booking, event):
"""
Send email notification for managers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,25 @@ def message(self) -> MIMEMultipart:
msg["Bcc"] = bcc

return msg


@implementer(IBookingEmailMessage)
@adapter(IPrenotazione, IBookingReminderEvent)
class PrenotazioneRemovedEmailMessage(PrenotazioneEmailMessage):
@property
def message_subject(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(self.prenotazioni_folder, "notify_as_removed_subject", "")
)

@property
def message_text(self) -> MIMEText:
text = IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(self.prenotazioni_folder, "notify_as_removed_message", None),
)
if CTE:
cs = Charset("utf-8")
cs.body_encoding = CTE # e.g. 'base64'
return MIMEText(text, "html", cs)
else:
return MIMEText(text, "html")
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def notify_on_refuse_sms_message_default_factory(context):
)


@provider(IContextAwareDefaultFactory)
def notify_on_cancel_sms_message_default_factory(context):
return api.portal.translate(
_(
"notify_on_cancel_sms_message_default_value",
"[${prenotazioni_folder_title}]: The booking ${booking_type} of ${booking_date} at ${booking_time} was canceled.",
)
)


@provider(IContextAwareDefaultFactory)
def notify_as_reminder_sms_message_default_factory(context):
return api.portal.translate(
Expand Down Expand Up @@ -122,6 +132,18 @@ class INotificationSMS(model.Schema):
defaultFactory=notify_on_refuse_sms_message_default_factory,
required=False,
)
notify_on_cancel_sms_message = schema.Text(
title=_(
"notify_on_cancel_message",
default="[Cancel] message",
),
description=_(
"notify_on_cancel_message_help",
default="The message text when a booking has been canceled.",
),
defaultFactory=notify_on_cancel_sms_message_default_factory,
required=False,
)
notify_as_reminder_sms_message = schema.Text(
title=_(
"notify_as_reminder_message",
Expand Down Expand Up @@ -151,6 +173,7 @@ class INotificationSMS(model.Schema):
"notify_on_confirm_sms_message",
"notify_on_move_sms_message",
"notify_on_refuse_sms_message",
"notify_on_cancel_sms_message",
"notify_as_reminder_sms_message",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, message_adapter, booking, request) -> None:
self.request = request

def send(self):

if self.is_notification_allowed():
# dont foget to write the history log about sending
# self.write_message_to_booking_history(self.booking, self.message_adapter.message_history)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,23 @@ def send_booking_reminder(context, event):
name="booking_transition_sms_sender",
)
sender_adapter.send()


@handle_exception_by_log
@notify_the_message_failure
def send_booking_removed(context, event):
if not booking_folder_provides_current_behavior(context):
return

message_adapter = getMultiAdapter(
(context, event),
IBookingSMSMessage,
name="removed_notification_sms_message",
)
if message_adapter and message_adapter.message:
sender_adapter = getMultiAdapter(
(message_adapter, context, getRequest()),
IBookingNotificationSender,
name="booking_transition_sms_sender",
)
sender_adapter.send()
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,23 @@ def message_history(self) -> str:
return api.portal.translate(
_("history_sms_reminder_sent", default="SMS reminder was sent")
)


@implementer(IBookingSMSMessage)
@adapter(IPrenotazione, IBookingReminderEvent)
class PrenotazioneRemovedSMSMessage(PrenotazioneSMSMessage):
@property
def message(self) -> str:
return IStringInterpolator(IContextWrapper(self.prenotazione)())(
getattr(
self.prenotazione.getPrenotazioniFolder(),
"notify_as_removed_sms_message",
"",
)
)

@property
def message_history(self) -> str:
return api.portal.translate(
_("history_sms_removed_sent", default="SMS removed notification was sent")
)
12 changes: 11 additions & 1 deletion src/redturtle/prenotazioni/content/prenotazioni_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ def data_validation(data):
default=False,
required=False,
)
notify_on_cancel = schema.Bool(
title=_("notify_on_cancel", default="Notify when canceled."),
description=_(
"notify_on_cancel_help",
default="Notify via mail the user when his booking has been canceled.",
),
default=False,
required=False,
)
max_bookings_allowed = schema.Int(
title=_(
"max_bookings_allowed_label",
Expand Down Expand Up @@ -540,6 +549,7 @@ def data_validation(data):
"notify_on_confirm",
"notify_on_move",
"notify_on_refuse",
"notify_on_cancel",
],
)

Expand Down Expand Up @@ -582,7 +592,7 @@ def get_booking_types(self) -> Generator[PrenotazioneType, None, None]:
def get_notification_flags(self):
return {
action: getattr(self, f"notify_on_{action}", False)
for action in ("confirm", "submit", "refuse")
for action in ("confirm", "submit", "refuse", "cancel")
}

# BBB: compatibility with old code (booking_types was a List of IBookingTypeRow)
Expand Down
Loading

0 comments on commit a8b9d3e

Please sign in to comment.