More elegant error handling
This commit is contained in:
parent
90bc30757a
commit
49a7fb1007
@ -1,7 +1,7 @@
|
|||||||
from ..forms.admin import AddTimeAdjustment, CreateTest, CreateUser, DeleteUser, Login, Register, ResetPassword, UpdatePassword, UpdateUser, UploadData
|
from ..forms.admin import AddTimeAdjustment, CreateTest, CreateUser, DeleteUser, Login, Register, ResetPassword, UpdatePassword, UpdateUser, UploadData
|
||||||
from ..models import Dataset, Entry, Test, User
|
from ..models import Dataset, Entry, Test, User
|
||||||
from ..tools.auth import disable_if_logged_in, require_account_creation
|
from ..tools.auth import disable_if_logged_in, require_account_creation
|
||||||
from ..tools.forms import get_dataset_choices, get_time_options
|
from ..tools.forms import get_dataset_choices, get_time_options, send_errors_to_client
|
||||||
from ..tools.data import check_is_json, validate_json
|
from ..tools.data import check_is_json, validate_json
|
||||||
from ..tools.test import answer_options, get_correct_answers
|
from ..tools.test import answer_options, get_correct_answers
|
||||||
|
|
||||||
@ -61,8 +61,7 @@ def _login():
|
|||||||
return jsonify({'success': f'Successfully logged in.'}), 200
|
return jsonify({'success': f'Successfully logged in.'}), 200
|
||||||
return jsonify({'error': f'The password you entered is incorrect.'}), 401
|
return jsonify({'error': f'The password you entered is incorrect.'}), 401
|
||||||
return jsonify({'error': f'The username you entered does not exist.'}), 401
|
return jsonify({'error': f'The username you entered does not exist.'}), 401
|
||||||
errors = [*form.username.errors, *form.password.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
if 'remembered_username' in session: form.username.data = session.pop('remembered_username')
|
if 'remembered_username' in session: form.username.data = session.pop('remembered_username')
|
||||||
next = request.args.get('next')
|
next = request.args.get('next')
|
||||||
return render_template('/admin/auth/login.html', form=form, next=next)
|
return render_template('/admin/auth/login.html', form=form, next=next)
|
||||||
@ -90,8 +89,7 @@ def _register():
|
|||||||
return jsonify({'success': message}), 200
|
return jsonify({'success': message}), 200
|
||||||
flash(message=message, category='error')
|
flash(message=message, category='error')
|
||||||
return jsonify({'error': message}), 401
|
return jsonify({'error': message}), 401
|
||||||
errors = [*form.username.errors, *form.email.errors, *form.password.errors, *form.password_reenter.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
return render_template('admin/auth/register.html', form=form)
|
return render_template('admin/auth/register.html', form=form)
|
||||||
|
|
||||||
@admin.route('/reset/', methods=['GET','POST'])
|
@admin.route('/reset/', methods=['GET','POST'])
|
||||||
@ -108,8 +106,7 @@ def _reset():
|
|||||||
if not user: return jsonify({'error': 'The user account does not exist.'}), 400
|
if not user: return jsonify({'error': 'The user account does not exist.'}), 400
|
||||||
if not user.get_email() == request.form.get('email'): return jsonify({'error': 'The email address does not match the user account.'}), 400
|
if not user.get_email() == request.form.get('email'): return jsonify({'error': 'The email address does not match the user account.'}), 400
|
||||||
return user.reset_password()
|
return user.reset_password()
|
||||||
errors = [*form.username.errors, *form.email.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
|
|
||||||
token = request.args.get('token')
|
token = request.args.get('token')
|
||||||
if token:
|
if token:
|
||||||
@ -134,8 +131,7 @@ def _update_password():
|
|||||||
session['remembered_username'] = user.get_username()
|
session['remembered_username'] = user.get_username()
|
||||||
flash('Your password has been reset.', 'success')
|
flash('Your password has been reset.', 'success')
|
||||||
return jsonify({'success':'Your password has been reset'}), 200
|
return jsonify({'success':'Your password has been reset'}), 200
|
||||||
errors = [*form.password.errors, *form.password_reenter.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 401
|
|
||||||
|
|
||||||
@admin.route('/settings/users/', methods=['GET', 'POST'])
|
@admin.route('/settings/users/', methods=['GET', 'POST'])
|
||||||
@login_required
|
@login_required
|
||||||
@ -152,8 +148,7 @@ def _users():
|
|||||||
success, message = new_user.register(notify=request.form.get('notify'), password=password)
|
success, message = new_user.register(notify=request.form.get('notify'), password=password)
|
||||||
if success: return jsonify({'success': message}), 200
|
if success: return jsonify({'success': message}), 200
|
||||||
return jsonify({'error': message}), 401
|
return jsonify({'error': message}), 401
|
||||||
errors = [*form.username.errors, *form.email.errors, *form.password.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 401
|
|
||||||
return render_template('/admin/settings/users.html', form = form, users = users)
|
return render_template('/admin/settings/users.html', form = form, users = users)
|
||||||
|
|
||||||
@admin.route('/settings/users/delete/<string:id>', methods=['GET', 'POST'])
|
@admin.route('/settings/users/delete/<string:id>', methods=['GET', 'POST'])
|
||||||
@ -170,8 +165,7 @@ def _delete_user(id:str):
|
|||||||
success, message = user.delete(notify=request.form.get('notify'))
|
success, message = user.delete(notify=request.form.get('notify'))
|
||||||
if success: return jsonify({'success': message}), 200
|
if success: return jsonify({'success': message}), 200
|
||||||
return jsonify({'error': message}), 400
|
return jsonify({'error': message}), 400
|
||||||
errors = form.password.errors
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
|
|
||||||
if id == current_user.id:
|
if id == current_user.id:
|
||||||
flash('Cannot delete your own user account.', 'error')
|
flash('Cannot delete your own user account.', 'error')
|
||||||
@ -199,8 +193,7 @@ def _update_user(id:str):
|
|||||||
flash(message, 'success')
|
flash(message, 'success')
|
||||||
return jsonify({'success': message}), 200
|
return jsonify({'success': message}), 200
|
||||||
return jsonify({'error': message}), 400
|
return jsonify({'error': message}), 400
|
||||||
errors = [*form.confirm_password.errors, *form.email.errors, *form.password.errors, *form.password_reenter.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
if not user:
|
if not user:
|
||||||
flash('User not found.', 'error')
|
flash('User not found.', 'error')
|
||||||
return redirect(url_for('admin._users'))
|
return redirect(url_for('admin._users'))
|
||||||
@ -222,8 +215,7 @@ def _questions():
|
|||||||
)
|
)
|
||||||
if success: return jsonify({'success': message}), 200
|
if success: return jsonify({'success': message}), 200
|
||||||
return jsonify({'error': message}), 400
|
return jsonify({'error': message}), 400
|
||||||
errors = form.data_file.errors
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
|
|
||||||
data = Dataset.query.all()
|
data = Dataset.query.all()
|
||||||
return render_template('/admin/settings/questions.html', form=form, data=data)
|
return render_template('/admin/settings/questions.html', form=form, data=data)
|
||||||
@ -299,9 +291,7 @@ def _create_test():
|
|||||||
flash(message=message, category='success')
|
flash(message=message, category='success')
|
||||||
return jsonify({'success': message}), 200
|
return jsonify({'success': message}), 200
|
||||||
return jsonify({'error': message}), 400
|
return jsonify({'error': message}), 400
|
||||||
else:
|
return send_errors_to_client(form=form)
|
||||||
errors = [*form.start_date.errors, *form.expiry_date.errors, *form.time_limit.errors]
|
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
|
|
||||||
@admin.route('/tests/edit/', methods=['POST'])
|
@admin.route('/tests/edit/', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from ..forms.quiz import StartQuiz
|
from ..forms.quiz import StartQuiz
|
||||||
from ..models import Entry, Test
|
from ..models import Entry, Test
|
||||||
|
from ..tools.forms import send_errors_to_client
|
||||||
from ..tools.test import redirect_if_started
|
from ..tools.test import redirect_if_started
|
||||||
|
|
||||||
from flask import abort, Blueprint, jsonify, redirect, render_template, request, session
|
from flask import abort, Blueprint, jsonify, redirect, render_template, request, session
|
||||||
@ -52,8 +53,7 @@ def _start():
|
|||||||
'id': entry.id
|
'id': entry.id
|
||||||
}), 200
|
}), 200
|
||||||
return jsonify({'error': 'There was an error processing the user test and/or user codes.'}), 400
|
return jsonify({'error': 'There was an error processing the user test and/or user codes.'}), 400
|
||||||
errors = [*form.test_code.errors, *form.user_code.errors, *form.first_name.errors, *form.surname.errors, *form.email.errors, *form.club.errors]
|
return send_errors_to_client(form=form)
|
||||||
return jsonify({ 'error': errors}), 400
|
|
||||||
return render_template('/quiz/start_quiz.html', form = form)
|
return render_template('/quiz/start_quiz.html', form = form)
|
||||||
|
|
||||||
@quiz.route('/quiz/')
|
@quiz.route('/quiz/')
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
from ..extensions import db
|
from ..extensions import db
|
||||||
|
|
||||||
|
from flask import jsonify
|
||||||
from wtforms.validators import ValidationError
|
from wtforms.validators import ValidationError
|
||||||
|
|
||||||
import json
|
import json
|
||||||
@ -53,4 +54,8 @@ def get_dataset_choices():
|
|||||||
label = f'{label} (Default)' if dataset.default else label
|
label = f'{label} (Default)' if dataset.default else label
|
||||||
choice = (dataset.id, label)
|
choice = (dataset.id, label)
|
||||||
dataset_choices.append(choice)
|
dataset_choices.append(choice)
|
||||||
return dataset_choices
|
return dataset_choices
|
||||||
|
|
||||||
|
def send_errors_to_client(form):
|
||||||
|
errors = [*form.errors]
|
||||||
|
return jsonify({ 'error': errors}), 400
|
Loading…
Reference in New Issue
Block a user