Update send_mail api and add welcome email for user registration. Also add security state to template context for emails for more complex template rendering.

This commit is contained in:
Matt Wright
2012-08-22 15:15:39 -04:00
parent 86adcf0653
commit 53257c17a9
10 changed files with 41 additions and 31 deletions
+10 -5
View File
@@ -26,19 +26,24 @@ _security = LocalProxy(lambda: app.extensions['security'])
_datastore = LocalProxy(lambda: _security.datastore)
def generate_confirmation_link(user):
token = generate_confirmation_token(user)
url = url_for_security('confirm_email', token=token)
return request.url_root[:-1] + url, token
def send_confirmation_instructions(user):
"""Sends the confirmation instructions email for the specified user.
:param user: The user to send the instructions to
:param token: The confirmation token
"""
token = generate_confirmation_token(user)
url = url_for_security('confirm_email', token=token)
confirmation_link = request.url_root[:-1] + url
ctx = dict(user=user, confirmation_link=confirmation_link)
confirmation_link, token = generate_confirmation_link(user)
send_mail('Please confirm your email', user.email,
'confirmation_instructions', ctx)
'confirmation_instructions',
user=user, confirmation_link=confirmation_link)
confirm_instructions_sent.send(user, app=app._get_current_object())
return token
+1 -5
View File
@@ -32,15 +32,11 @@ def send_login_instructions(user, next):
:param token: The login token
"""
token = generate_login_token(user, next)
url = url_for_security('token_login', token=token)
login_link = request.url_root[:-1] + url
ctx = dict(user=user, login_link=login_link)
send_mail('Login Instructions', user.email,
'login_instructions', ctx)
'login_instructions', user=user, login_link=login_link)
login_instructions_sent.send(dict(user=user, login_token=token),
app=app._get_current_object())
+4 -7
View File
@@ -34,10 +34,9 @@ def send_reset_password_instructions(user):
url = url_for_security('reset_password', token=token)
reset_link = request.url_root[:-1] + url
send_mail('Password reset instructions',
user.email,
send_mail('Password reset instructions', user.email,
'reset_instructions',
dict(user=user, reset_link=reset_link))
user=user, reset_link=reset_link)
reset_password_instructions_sent.send(dict(user=user, token=token),
app=app._get_current_object())
@@ -48,10 +47,8 @@ def send_password_reset_notice(user):
:param user: The user to send the notice to
"""
send_mail('Your password has been reset',
user.email,
'reset_notice',
dict(user=user))
send_mail('Your password has been reset', user.email,
'reset_notice', user=user)
def generate_reset_password_token(user):
@@ -1,5 +1,3 @@
<p>Welcome {{ user.email }}!</p>
<p>You can confirm your email through the link below:</p>
<p>Please confirm your email through the link below:</p>
<p><a href="{{ confirmation_link }}">Confirm my account</a></p>
@@ -1,5 +1,3 @@
Welcome {{ user.email }}!
You can confirm your email through the link below:
Please confirm your email through the link below:
{{ confirmation_link }}
@@ -0,0 +1,7 @@
<p>Welcome {{ user.email }}!</p>
{% if security.confirmable %}
<p>You can confirm your email through the link below:</p>
<p><a href="{{ confirmation_link }}">Confirm my account</a></p>
{% endif %}
@@ -0,0 +1,7 @@
Welcome {{ user.email }}!
{% if security.confirmable %}
You can confirm your email through the link below:
{{ confirmation_link }}
{% endif %}
+2 -2
View File
@@ -230,7 +230,7 @@ def get_within_delta(key, app=None):
return timedelta(**{values[1]: int(values[0])})
def send_mail(subject, recipient, template, context=None):
def send_mail(subject, recipient, template, **context):
"""Send an email via the Flask-Mail extension.
:param subject: Email subject
@@ -241,7 +241,7 @@ def send_mail(subject, recipient, template, context=None):
from flask.ext.mail import Message
mail = current_app.extensions.get('mail')
context = context or {}
context.setdefault('security', _security)
msg = Message(subject,
sender=_security.email_sender,
+8 -5
View File
@@ -14,8 +14,8 @@ from flask import current_app as app, redirect, request, \
from werkzeug.datastructures import MultiDict
from werkzeug.local import LocalProxy
from flask_security.confirmable import confirm_by_token, \
send_confirmation_instructions, requires_confirmation
from flask_security.confirmable import generate_confirmation_link, \
send_confirmation_instructions, requires_confirmation, confirm_by_token
from flask_security.decorators import login_required
from flask_security.exceptions import ConfirmationError, ResetPasswordError, \
PasswordlessLoginError
@@ -26,7 +26,7 @@ from flask_security.recoverable import reset_by_token, \
send_reset_password_instructions
from flask_security.signals import user_registered
from flask_security.utils import get_url, get_post_login_redirect, do_flash, \
get_message, config_value, login_user, logout_user, \
get_message, config_value, login_user, logout_user, send_mail, \
anonymous_user_required, url_for_security as url_for, verify_password
@@ -142,17 +142,20 @@ def register():
register_user_form=form,
**_ctx('register'))
token = None
confirmation_link, token = None, None
user = _datastore.create_user(**form.to_dict())
_commit()
if _security.confirmable:
token = send_confirmation_instructions(user)
confirmation_link, token = generate_confirmation_link(user)
do_flash(*get_message('CONFIRM_REGISTRATION', email=user.email))
user_registered.send(dict(user=user, confirm_token=token),
app=app._get_current_object())
send_mail('Welcome', user.email, 'welcome',
user=user, confirmation_link=confirmation_link)
_logger.debug('User %s registered' % user)
if not _security.confirmable or _security.login_without_confirmation:
-1
View File
@@ -362,7 +362,6 @@ class ExpiredConfirmationTest(SecurityTest):
r = self.client.get('/confirm/' + token, follow_redirects=True)
self.assertEqual(len(outbox), 1)
self.assertIn(e, outbox[0].html)
self.assertNotIn(token, outbox[0].html)
expire_text = self.AUTH_CONFIG['SECURITY_CONFIRM_EMAIL_WITHIN']