Browse Source

added events for user notification

master
Holger Frey 7 years ago
parent
commit
40401472be
  1. 7
      ordr2/events.py
  2. 24
      ordr2/templates/emails/registration.jinja2
  3. 33
      tests/events.py

7
ordr2/events.py

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
from pyramid.events import subscriber
from pyramid.renderers import render
from pyramid_mailer import get_mailer
from pyramid_mailer.message import Message
@ -36,7 +37,7 @@ class UserNotification(object): @@ -36,7 +37,7 @@ class UserNotification(object):
class CompleteRegistration(UserNotification):
''' user notification for account activation '''
subject='[ordr] Your account was activated'
subject='[ordr] Please verify your email address'
template = 'ordr2:templates/emails/registration.jinja2'
@ -63,7 +64,6 @@ class OrderStatusChange(UserNotification): @@ -63,7 +64,6 @@ class OrderStatusChange(UserNotification):
@subscriber(UserNotification)
def notify_user(event):
''' notify a user about an event '''
body = render(
event.template,
{'user': event.account, 'data': event.data},
@ -75,4 +75,5 @@ def notify_user(event): @@ -75,4 +75,5 @@ def notify_user(event):
recipients=[event.account.email],
html=body
)
event.request.mailer.send(message)
mailer = get_mailer(event.request.registry)
mailer.send(message)

24
ordr2/templates/emails/registration.jinja2

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>[ordr] verify your email address</title>
<link href='http://fonts.googleapis.com/css?family=Anton&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
</head>
<body>
<h1>Hi there!</h1>
<p>
Please verify your email address for the account "{{ user.user_name }}" by following this link
<a href="{{ request.resource_url(context, data.hash) }}">{{ request.resource_url(context, data.hash) }}</a>
</p>
<p class="signature">
Regards,
<br/>
<span class="brand">ordr</span>
</p>
<p class="footprint">
<small>Please don't respont to this email! This is an automatically generated notification by the system.</small>
<a href="http://distractedbysquirrels.com/" target="_blank" title="This software was originally written by Sebastian Sebald." class="icon-dbs"></a>
</p>
</body>
</html>

33
tests/events.py

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
''' Tests for ordr2.security '''
import pytest
from pyramid.testing import DummyRequest
from pyramid_mailer import get_mailer
from . import app_config, get_user
def test_user_notification_init():
''' test creation of user notification events '''
from ordr2.events import UserNotification
notification = UserNotification('a', 'b', 'c')
assert notification.request == 'a'
assert notification.account == 'b'
assert notification.data == 'c'
def test_notify_user(app_config):
from ordr2.events import CompleteRegistration, notify_user
request = DummyRequest()
user = get_user('user')
notification = CompleteRegistration(request, user, {'hash': 'something'})
notify_user(notification)
mailer = get_mailer(request.registry)
last_mail = mailer.outbox[-1]
assert 'Please verify your email address ' in last_mail.html
assert 'http://example.com//something' in last_mail.html