Exception handling for database queries
This commit is contained in:
		@@ -1,10 +1,12 @@
 | 
			
		||||
from ..forms.quiz import StartQuiz
 | 
			
		||||
from ..models import Entry, Test
 | 
			
		||||
from ..tools.forms import send_errors_to_client
 | 
			
		||||
from ..tools.logs import write
 | 
			
		||||
from ..tools.test import redirect_if_started
 | 
			
		||||
 | 
			
		||||
from flask import abort, Blueprint, jsonify, redirect, render_template, request, session
 | 
			
		||||
from flask.helpers import flash, url_for
 | 
			
		||||
from flask import Blueprint, jsonify, render_template, request, session
 | 
			
		||||
from flask.helpers import abort, flash, redirect, url_for
 | 
			
		||||
from sqlalchemy.exc import SQLAlchemyError
 | 
			
		||||
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
@@ -37,7 +39,10 @@ def _start():
 | 
			
		||||
            entry.set_club(request.form.get('club'))
 | 
			
		||||
            entry.set_email(request.form.get('email'))
 | 
			
		||||
            code = request.form.get('test_code').replace('—', '').lower()
 | 
			
		||||
            test = Test.query.filter_by(code=code).first()
 | 
			
		||||
            try: test = Test.query.filter_by(code=code).first()
 | 
			
		||||
            except SQLAlchemyError as exception:
 | 
			
		||||
                write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
 | 
			
		||||
                return abort(500)
 | 
			
		||||
            entry.test = test
 | 
			
		||||
            entry.user_code = request.form.get('user_code')
 | 
			
		||||
            entry.user_code = None if entry.user_code == '' else entry.user_code.lower()
 | 
			
		||||
@@ -59,16 +64,23 @@ def _start():
 | 
			
		||||
@quiz.route('/quiz/')
 | 
			
		||||
def _quiz():
 | 
			
		||||
    id = session.get('id')
 | 
			
		||||
    if not id or not Entry.query.filter_by(id=id).first():
 | 
			
		||||
        flash('Your session was not recognised. Please sign in to the quiz again.', 'error')
 | 
			
		||||
        session.pop('id', None)
 | 
			
		||||
        return redirect(url_for('quiz._start'))
 | 
			
		||||
    try:
 | 
			
		||||
        if not id or not Entry.query.filter_by(id=id).first():
 | 
			
		||||
            flash('Your session was not recognised. Please sign in to the quiz again.', 'error')
 | 
			
		||||
            session.pop('id', None)
 | 
			
		||||
            return redirect(url_for('quiz._start'))
 | 
			
		||||
    except SQLAlchemyError as exception:
 | 
			
		||||
        write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
 | 
			
		||||
        return abort(500)
 | 
			
		||||
    return render_template('/quiz/client.html')
 | 
			
		||||
 | 
			
		||||
@quiz.route('/result/')
 | 
			
		||||
def _result():
 | 
			
		||||
    id = session.get('id')
 | 
			
		||||
    entry = Entry.query.filter_by(id=id).first()
 | 
			
		||||
    try: entry = Entry.query.filter_by(id=id).first()
 | 
			
		||||
    except SQLAlchemyError as exception:
 | 
			
		||||
        write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
 | 
			
		||||
        return abort(500)
 | 
			
		||||
    if not entry: return abort(404)
 | 
			
		||||
    session.pop('id',None)
 | 
			
		||||
    score = round(100*entry.result['score']/entry.result['max'])
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user