diff --git a/app/auth/views.py b/app/auth/views.py index 7353ebf95f9bef6ea664f53b3a88282fc00e8c97..d9111ff1a86bd94cfc8042159051ca9e2cdd6702 100644 --- a/app/auth/views.py +++ b/app/auth/views.py @@ -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', - 'auth/email/confirm', token=token, user=user) + 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', - 'auth/email/confirm', token=token, user=current_user) + 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']) diff --git a/app/corpora/background_functions.py b/app/corpora/background_functions.py index 1b8f7fc09cbd7d7430089d00335ed1f2852be19c..55d9af6bdb2aee7839f578f2b2e85edaf4ac0b4c 100644 --- a/app/corpora/background_functions.py +++ b/app/corpora/background_functions.py @@ -5,16 +5,20 @@ def delete_corpus_(app, corpus_id): with app.app_context(): corpus = Corpus.query.get(corpus_id) if corpus is None: - raise Exception('Corpus {} not found!'.format(corpus_id)) - corpus.delete() + # raise Exception('Corpus {} not found!'.format(corpus_id)) + pass + else: + corpus.delete() def delete_corpus_file_(app, corpus_file_id): with app.app_context(): corpus_file = CorpusFile.query.get(corpus_file_id) if corpus_file is None: - raise Exception('Corpus file {} not found!'.format(corpus_file_id)) - corpus_file.delete() + # raise Exception('Corpus file {} not found!'.format(corpus_file_id)) + pass + else: + corpus_file.delete() 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) if corpus_file is None: raise Exception('Corpus file {} not found!'.format(corpus_file_id)) - corpus_file.insert_metadata() + else: + corpus_file.insert_metadata() diff --git a/app/email.py b/app/email.py index 0c7889326a7651e1babc3f2a1e61eba2325509a3..ab24c7642bb001426c452790d00f4b9b8e92575d 100644 --- a/app/email.py +++ b/app/email.py @@ -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 diff --git a/app/main/views.py b/app/main/views.py index d93a51fbf91018dab48fbde5176c1af15c66bf9f..dfe5cf679460abb41f26ae890e1095fbd69ec183 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -6,6 +6,7 @@ from .. import logger from ..auth.forms import LoginForm from ..models import User + @main.route('/', methods=['GET', 'POST']) def index(): login_form = LoginForm(prefix='login-form') diff --git a/app/profile/views.py b/app/profile/views.py index 96f588f0d671242310adf6b75addfc768e84ec50..383da74144724bad25ff46d313f28f6180b0c2b6 100644 --- a/app/profile/views.py +++ b/app/profile/views.py @@ -47,8 +47,7 @@ def settings(): edit_email_form=edit_email_form, edit_password_form=edit_password_form, edit_general_settings_form=edit_general_settings_form, - title='Settings' - ) + title='Settings') @profile.route('/delete', methods=['GET', 'POST'])