Added decorator to test availability of datasets

Used decorator tool to validate dataset exists on views
This commit is contained in:
Vivek Santayana 2022-08-17 16:37:03 +01:00
parent 2da8eb7712
commit be5343a4bd
3 changed files with 20 additions and 6 deletions

View File

@ -2,7 +2,7 @@ from ..forms.admin import AddTimeAdjustment, CreateTest, CreateUser, DeleteUser,
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, send_errors_to_client 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_dataset_exists, check_is_json, validate_json
from ..tools.test import answer_options, get_correct_answers from ..tools.test import answer_options, get_correct_answers
from flask import abort, Blueprint, jsonify, render_template, redirect, request, send_file, session from flask import abort, Blueprint, jsonify, render_template, redirect, request, send_file, session
@ -247,15 +247,12 @@ def _download(id:str):
@admin.route('/tests/<string:filter>/', methods=['GET']) @admin.route('/tests/<string:filter>/', methods=['GET'])
@admin.route('/tests/', methods=['GET']) @admin.route('/tests/', methods=['GET'])
@login_required @login_required
@check_dataset_exists
def _tests(filter:str=None): def _tests(filter:str=None):
datasets = Dataset.query.all()
tests = None tests = None
_tests = Test.query.all() _tests = Test.query.all()
form = None form = None
now = datetime.now() now = datetime.now()
if not datasets:
flash('There are no available question datasets. Please upload a question dataset in order to set up an exam.', 'error')
return redirect(url_for('admin._questions'))
if filter not in ['create','active','scheduled','expired','all']: return redirect(url_for('admin._tests', filter='active')) if filter not in ['create','active','scheduled','expired','all']: return redirect(url_for('admin._tests', filter='active'))
if filter == 'create': if filter == 'create':
form = CreateTest() form = CreateTest()

View File

@ -1,6 +1,7 @@
from ..forms.admin import EditDataset from ..forms.admin import EditDataset
from ..models import Dataset, User from ..models import Dataset, User
from ..tools.forms import get_dataset_choices, send_errors_to_client from ..tools.forms import get_dataset_choices, send_errors_to_client
from ..tools.data import check_dataset_exists
from flask import Blueprint, flash, jsonify, redirect, render_template, request from flask import Blueprint, flash, jsonify, redirect, render_template, request
from flask.helpers import url_for from flask.helpers import url_for
@ -27,6 +28,7 @@ def _editor():
return render_template('/editor/index.html', form=form) return render_template('/editor/index.html', form=form)
@editor.route('/<string:id>/') @editor.route('/<string:id>/')
@check_dataset_exists
@login_required @login_required
def _editor_console(id:str=None): def _editor_console(id:str=None):
dataset = Dataset.query.filter_by(id=id).first() dataset = Dataset.query.filter_by(id=id).first()

View File

@ -1,8 +1,13 @@
from ..models import Dataset
from flask import current_app as app from flask import current_app as app
from flask import flash, redirect
from flask.helpers import url_for
import json import json
from pathlib import Path from pathlib import Path
from random import shuffle from random import shuffle
from functools import wraps
def load(filename:str): def load(filename:str):
data_dir = Path(app.config.get('DATA')) data_dir = Path(app.config.get('DATA'))
@ -67,3 +72,13 @@ def get_tag_list(dataset:list):
if block['type'] == 'block': if block['type'] == 'block':
for question in block['questions']: output = list(set(output) | set(question['tags'])) for question in block['questions']: output = list(set(output) | set(question['tags']))
return output return output
def check_dataset_exists(function):
@wraps(function)
def wrapper(*args, **kwargs):
datasets = Dataset.query.all()
if not datasets:
flash('There are no available question datasets. Please upload a question dataset first, or use the question editor to create a new dataset.', 'error')
return redirect(url_for('admin._questions'))
return function(*args, **kwargs)
return wrapper