Skip to content
Snippets Groups Projects
Commit 461f80be authored by Patrick Jentsch's avatar Patrick Jentsch
Browse files

Change email function names

parent 716e1c35
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ from . import auth ...@@ -5,7 +5,7 @@ from . import auth
from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm, from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
RegistrationForm) RegistrationForm)
from .. import db from .. import db
from ..email import send_email from ..email import create_message, send_async
from ..models import User from ..models import User
import os import os
import shutil import shutil
...@@ -17,8 +17,7 @@ def before_request(): ...@@ -17,8 +17,7 @@ def before_request():
Checks if a user is unconfirmed when visiting specific sites. Redirects to Checks if a user is unconfirmed when visiting specific sites. Redirects to
unconfirmed view if user is unconfirmed. unconfirmed view if user is unconfirmed.
""" """
if (current_user.is_authenticated if (current_user.is_authenticated and not current_user.confirmed
and not current_user.confirmed
and request.blueprint != 'auth' and request.blueprint != 'auth'
and request.endpoint != 'static'): and request.endpoint != 'static'):
return redirect(url_for('auth.unconfirmed')) return redirect(url_for('auth.unconfirmed'))
...@@ -69,8 +68,9 @@ def register(): ...@@ -69,8 +68,9 @@ def register():
shutil.rmtree(user_dir) shutil.rmtree(user_dir)
os.mkdir(user_dir) os.mkdir(user_dir)
token = user.generate_confirmation_token() token = user.generate_confirmation_token()
send_email(user.email, 'Confirm Your Account', msg = create_message(user.email, 'Confirm Your Account',
'auth/email/confirm', token=token, user=user) 'auth/email/confirm', token=token, user=user)
send_async(msg)
flash('A confirmation email has been sent to you by email.') flash('A confirmation email has been sent to you by email.')
return redirect(url_for('auth.login')) return redirect(url_for('auth.login'))
return render_template('auth/register.html.j2', return render_template('auth/register.html.j2',
...@@ -105,8 +105,9 @@ def unconfirmed(): ...@@ -105,8 +105,9 @@ def unconfirmed():
@login_required @login_required
def resend_confirmation(): def resend_confirmation():
token = current_user.generate_confirmation_token() token = current_user.generate_confirmation_token()
send_email(current_user.email, 'Confirm Your Account', msg = create_message(current_user.email, 'Confirm Your Account',
'auth/email/confirm', token=token, user=current_user) 'auth/email/confirm', token=token, user=current_user)
send_async(msg)
flash('A new confirmation email has been sent to you by email.') flash('A new confirmation email has been sent to you by email.')
return redirect(url_for('auth.unconfirmed')) return redirect(url_for('auth.unconfirmed'))
...@@ -116,23 +117,23 @@ def reset_password_request(): ...@@ -116,23 +117,23 @@ def reset_password_request():
if current_user.is_authenticated: if current_user.is_authenticated:
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
reset_password_request_form = ResetPasswordRequestForm( reset_password_request_form = ResetPasswordRequestForm(
prefix='reset-password-request-form' prefix='reset-password-request-form')
)
if reset_password_request_form.validate_on_submit(): if reset_password_request_form.validate_on_submit():
submitted_email = reset_password_request_form.email.data submitted_email = reset_password_request_form.email.data
user = User.query.filter_by(email=submitted_email.lower()).first() user = User.query.filter_by(email=submitted_email.lower()).first()
if user: if user:
token = user.generate_reset_token() token = user.generate_reset_token()
send_email(user.email, 'Reset Your Password', msg = create_message(user.email, 'Reset Your Password',
'auth/email/reset_password', token=token, user=user) 'auth/email/reset_password', token=token,
user=user)
send_async(msg)
flash('An email with instructions to reset your password has been ' flash('An email with instructions to reset your password has been '
'sent to you.') 'sent to you.')
return redirect(url_for('auth.login')) return redirect(url_for('auth.login'))
return render_template( return render_template(
'auth/reset_password_request.html.j2', 'auth/reset_password_request.html.j2',
reset_password_request_form=reset_password_request_form, reset_password_request_form=reset_password_request_form,
title='Password Reset' title='Password Reset')
)
@auth.route('/reset/<token>', methods=['GET', 'POST']) @auth.route('/reset/<token>', methods=['GET', 'POST'])
......
...@@ -4,18 +4,24 @@ from threading import Thread ...@@ -4,18 +4,24 @@ from threading import Thread
from . import mail from . import mail
def send_async_email(app, msg): def create_message(recipient, subject, template, **kwargs):
app = current_app._get_current_object()
sender = app.config['NOPAQUE_MAIL_SENDER']
subject_prefix = app.config['NOPAQUE_MAIL_SUBJECT_PREFIX']
msg = Message('{} {}'.format(subject_prefix, subject),
recipients=[recipient], sender=sender)
msg.body = render_template('{}.txt.j2'.format(template), **kwargs)
msg.html = render_template('{}.html.j2'.format(template), **kwargs)
return msg
def send(app, msg):
with app.app_context(): with app.app_context():
mail.send(msg) mail.send(msg)
def send_email(to, subject, template, **kwargs): def send_async(msg):
app = current_app._get_current_object() app = current_app._get_current_object()
msg = Message( thread = Thread(target=send, args=(app, msg))
'{} {}'.format(app.config['NOPAQUE_MAIL_SUBJECT_PREFIX'], subject),
recipients=[to], sender=app.config['NOPAQUE_MAIL_SENDER'])
msg.body = render_template('{}.txt.j2'.format(template), **kwargs)
msg.html = render_template('{}.html.j2'.format(template), **kwargs)
thread = Thread(target=send_async_email, args=(app, msg))
thread.start() thread.start()
return thread return thread
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment