Merge branch 'development' of gitlab.ub.uni-bielefeld.de:sfb1288inf/opaque into development

This commit is contained in:
Stephan Porada 2020-04-21 10:31:40 +02:00
commit a3c049c4ed
5 changed files with 40 additions and 28 deletions

View File

@ -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():
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():
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():
@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():
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'])

View File

@ -5,16 +5,20 @@ def delete_corpus_(app, corpus_id):
with app.app_context(): with app.app_context():
corpus = Corpus.query.get(corpus_id) corpus = Corpus.query.get(corpus_id)
if corpus is None: if corpus is None:
raise Exception('Corpus {} not found!'.format(corpus_id)) # raise Exception('Corpus {} not found!'.format(corpus_id))
corpus.delete() pass
else:
corpus.delete()
def delete_corpus_file_(app, corpus_file_id): def delete_corpus_file_(app, corpus_file_id):
with app.app_context(): with app.app_context():
corpus_file = CorpusFile.query.get(corpus_file_id) corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None: if corpus_file is None:
raise Exception('Corpus file {} not found!'.format(corpus_file_id)) # raise Exception('Corpus file {} not found!'.format(corpus_file_id))
corpus_file.delete() pass
else:
corpus_file.delete()
def edit_corpus_file_(app, corpus_file_id): def edit_corpus_file_(app, corpus_file_id):
@ -22,4 +26,5 @@ def edit_corpus_file_(app, corpus_file_id):
corpus_file = CorpusFile.query.get(corpus_file_id) corpus_file = CorpusFile.query.get(corpus_file_id)
if corpus_file is None: if corpus_file is None:
raise Exception('Corpus file {} not found!'.format(corpus_file_id)) raise Exception('Corpus file {} not found!'.format(corpus_file_id))
corpus_file.insert_metadata() else:
corpus_file.insert_metadata()

View File

@ -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

View File

@ -6,6 +6,7 @@ from .. import logger
from ..auth.forms import LoginForm from ..auth.forms import LoginForm
from ..models import User from ..models import User
@main.route('/', methods=['GET', 'POST']) @main.route('/', methods=['GET', 'POST'])
def index(): def index():
login_form = LoginForm(prefix='login-form') login_form = LoginForm(prefix='login-form')

View File

@ -47,8 +47,7 @@ def settings():
edit_email_form=edit_email_form, edit_email_form=edit_email_form,
edit_password_form=edit_password_form, edit_password_form=edit_password_form,
edit_general_settings_form=edit_general_settings_form, edit_general_settings_form=edit_general_settings_form,
title='Settings' title='Settings')
)
@profile.route('/delete', methods=['GET', 'POST']) @profile.route('/delete', methods=['GET', 'POST'])