''' custom events and event subsribers ''' from pyramid.events import subscriber from pyramid.renderers import render from pyramid_mailer import get_mailer from pyramid_mailer.message import Message # custom events class UserNotification(object): ''' base class for user notification emails :param pyramid.request.Request request: current request object :param ordr.models.account.Users account: send notification to this user :param dict 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 ActivationNotification(UserNotification): ''' user notification for account activation ''' subject = '[ordr] Your account was activated' template = 'ordr:templates/emails/activation.jinja2' class OrderStatusNotification(UserNotification): ''' user notification for order status change ''' subject = '[ordr] Order Status Change' template = 'ordr:templates/emails/order.jinja2' class PasswordResetNotification(UserNotification): ''' user notification for password reset link ''' subject = '[ordr] Password Reset' template = 'ordr:templates/emails/password_reset.jinja2' class RegistrationNotification(UserNotification): ''' user notification for account activation ''' subject = '[ordr] Please verify your email address' template = 'ordr:templates/emails/registration.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 ) mailer = get_mailer(event.request.registry) mailer.send(message)