Holger Frey
7 years ago
1 changed files with 78 additions and 0 deletions
@ -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) |
Reference in new issue