Change email function names

This commit is contained in:
Patrick Jentsch 2020-04-21 10:26:44 +02:00
parent 716e1c357a
commit 461f80be20
2 changed files with 28 additions and 21 deletions

View File

@ -5,7 +5,7 @@ from . import auth
from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
RegistrationForm)
from .. import db
from ..email import send_email
from ..email import create_message, send_async
from ..models import User
import os
import shutil
@ -17,8 +17,7 @@ def before_request():
Checks if a user is unconfirmed when visiting specific sites. Redirects to
unconfirmed view if user is unconfirmed.
"""
if (current_user.is_authenticated
and not current_user.confirmed
if (current_user.is_authenticated and not current_user.confirmed
and request.blueprint != 'auth'
and request.endpoint != 'static'):
return redirect(url_for('auth.unconfirmed'))
@ -69,8 +68,9 @@ def register():
shutil.rmtree(user_dir)
os.mkdir(user_dir)
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)
send_async(msg)
flash('A confirmation email has been sent to you by email.')
return redirect(url_for('auth.login'))
return render_template('auth/register.html.j2',
@ -105,8 +105,9 @@ def unconfirmed():
@login_required
def resend_confirmation():
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)
send_async(msg)
flash('A new confirmation email has been sent to you by email.')
return redirect(url_for('auth.unconfirmed'))
@ -116,23 +117,23 @@ def reset_password_request():
if current_user.is_authenticated:
return redirect(url_for('main.dashboard'))
reset_password_request_form = ResetPasswordRequestForm(
prefix='reset-password-request-form'
)
prefix='reset-password-request-form')
if reset_password_request_form.validate_on_submit():
submitted_email = reset_password_request_form.email.data
user = User.query.filter_by(email=submitted_email.lower()).first()
if user:
token = user.generate_reset_token()
send_email(user.email, 'Reset Your Password',
'auth/email/reset_password', token=token, user=user)
msg = create_message(user.email, 'Reset Your Password',
'auth/email/reset_password', token=token,
user=user)
send_async(msg)
flash('An email with instructions to reset your password has been '
'sent to you.')
return redirect(url_for('auth.login'))
return render_template(
'auth/reset_password_request.html.j2',
reset_password_request_form=reset_password_request_form,
title='Password Reset'
)
title='Password Reset')
@auth.route('/reset/<token>', methods=['GET', 'POST'])

View File

@ -4,18 +4,24 @@ from threading import Thread
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():
mail.send(msg)
def send_email(to, subject, template, **kwargs):
def send_async(msg):
app = current_app._get_current_object()
msg = Message(
'{} {}'.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 = Thread(target=send, args=(app, msg))
thread.start()
return thread