From be5343a4bd3ed191c056ad682d19520c4d968e83 Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Wed, 17 Aug 2022 16:37:03 +0100 Subject: [PATCH] Added decorator to test availability of datasets Used decorator tool to validate dataset exists on views --- ref-test/app/admin/views.py | 7 ++----- ref-test/app/editor/views.py | 2 ++ ref-test/app/tools/data.py | 17 ++++++++++++++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/ref-test/app/admin/views.py b/ref-test/app/admin/views.py index 39a2599..d8463ef 100644 --- a/ref-test/app/admin/views.py +++ b/ref-test/app/admin/views.py @@ -2,7 +2,7 @@ from ..forms.admin import AddTimeAdjustment, CreateTest, CreateUser, DeleteUser, from ..models import Dataset, Entry, Test, User 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.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 flask import abort, Blueprint, jsonify, render_template, redirect, request, send_file, session @@ -247,15 +247,12 @@ def _download(id:str): @admin.route('/tests//', methods=['GET']) @admin.route('/tests/', methods=['GET']) @login_required +@check_dataset_exists def _tests(filter:str=None): - datasets = Dataset.query.all() tests = None _tests = Test.query.all() form = None 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 == 'create': form = CreateTest() diff --git a/ref-test/app/editor/views.py b/ref-test/app/editor/views.py index b2c1887..c002d1d 100644 --- a/ref-test/app/editor/views.py +++ b/ref-test/app/editor/views.py @@ -1,6 +1,7 @@ from ..forms.admin import EditDataset from ..models import Dataset, User 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.helpers import url_for @@ -27,6 +28,7 @@ def _editor(): return render_template('/editor/index.html', form=form) @editor.route('//') +@check_dataset_exists @login_required def _editor_console(id:str=None): dataset = Dataset.query.filter_by(id=id).first() diff --git a/ref-test/app/tools/data.py b/ref-test/app/tools/data.py index 7eab4d9..c4bfce6 100644 --- a/ref-test/app/tools/data.py +++ b/ref-test/app/tools/data.py @@ -1,8 +1,13 @@ +from ..models import Dataset + from flask import current_app as app +from flask import flash, redirect +from flask.helpers import url_for import json from pathlib import Path from random import shuffle +from functools import wraps def load(filename:str): data_dir = Path(app.config.get('DATA')) @@ -66,4 +71,14 @@ def get_tag_list(dataset:list): if block['type'] == 'question': output = list(set(output) | set(block['tags'])) if block['type'] == 'block': for question in block['questions']: output = list(set(output) | set(question['tags'])) - return output \ No newline at end of file + 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 \ No newline at end of file