From 353c6a8f121e497eefaab88072fbceab9da893f6 Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Sat, 21 Oct 2017 11:49:21 +0200 Subject: [PATCH] added events module --- ordr2/events.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ordr2/events.py diff --git a/ordr2/events.py b/ordr2/events.py new file mode 100644 index 0000000..1be7429 --- /dev/null +++ b/ordr2/events.py @@ -0,0 +1,78 @@ +''' custom events and event subsribers ''' + +from pyramid.events import subscriber +from pyramid.renderers import render +from pyramid_mailer.message import Message + + +# custom events + +class UserNotification(object): + ''' base class for user notification emails + + :param request: + current request object + :type request: + `pyramid.request.Request` + :param account: + account to send the notification to + :type account: + :class:`ordr2.models.account.Users` + :param data: + additional data to pass to the mail template + ''' + + #: subject of the notification + subject = None + + #: template to render + template = None + + def __init__(self, request, account, data=None): + self.request = request + self.account = account + self.data = data + + +class CompleteRegistration(UserNotification): + ''' user notification for account activation ''' + subject='[ordr] Your account was activated' + template = 'ordr2:templates/emails/registration.jinja2' + + +class AccountActivation(UserNotification): + ''' user notification for account activation ''' + subject='[ordr] Your account was activated' + template = 'ordr2:templates/emails/activation.jinja2' + + +class PasswordReset(UserNotification): + ''' user notification for password reset link ''' + subject='[ordr] Password Reset' + template = 'ordr2:templates/emails/password_reset.jinja2' + + +class OrderStatusChange(UserNotification): + ''' user notification for order status change ''' + subject='[ordr] Order Status Change' + template = 'ordr2:templates/emails/order.jinja2' + + +# subsribers for events + +@subscriber(UserNotification) +def notify_user(event): + ''' notify a user about an event ''' + + body = render( + event.template, + {'user': event.account, 'data': event.data}, + event.request + ) + message = Message( + subject=event.subject, + sender=event.request.registry.settings['mail.default_sender'], + recipients=[event.account.email], + html=body + ) + event.request.mailer.send(message)