diff --git a/ref-test/app/api/views.py b/ref-test/app/api/views.py index 794ef26..86c4ca0 100644 --- a/ref-test/app/api/views.py +++ b/ref-test/app/api/views.py @@ -1,7 +1,9 @@ -from ..models import Dataset, Entry +from ..models import Dataset, Entry, User +from ..tools.data import validate_json from ..tools.test import evaluate_answers, generate_questions -from flask import Blueprint, jsonify, request +from flask import Blueprint, flash, jsonify, request, url_for +from flask_login import login_required from datetime import datetime, timedelta from json import loads @@ -26,7 +28,7 @@ def _fetch_questions(): time_adjustment = test.adjustments[user_code] _time_limit += time_adjustment end_delta = timedelta(minutes=_time_limit) - end_time = datetime.utcnow() + end_delta + end_time = datetime.now() + end_delta else: end_time = None entry.start() @@ -63,4 +65,37 @@ def _submit_quiz(): 'success': 'Your submission has been processed. Redirecting you to receive your results.', 'id': id }), 200 - \ No newline at end of file + +@api.route('/editor/', methods=['POST']) +@login_required +def _editor(id:str=None): + request_data = request.get_json() + id = request_data['id'] + dataset = Dataset.query.filter_by(id=id).first() + if not dataset: return jsonify({'error': 'Invalid request. Dataset not found.'}), 404 + data_path = dataset.get_file() + if request_data['action'] == 'fetch': + with open(data_path, 'r') as data_file: + data = loads(data_file.read()) + return jsonify({'success': 'Successfully downloaded dataset', 'data': data}), 200 + default = request_data['default'] + creator = request_data['creator'] + name = request_data['name'] + data = request_data['data'] + if not validate_json(data): return jsonify({'error': 'The data you submitted was invalid.'}), 400 + user = User.query.filter_by(id=creator).first() + dataset.set_name(name) + dataset.creator = user + success, message = dataset.update(data=data, default=default) + if not success: return jsonify({'error': message}), 400 + return jsonify({'success': message}), 200 + +@api.route('/editor/new/', methods=['POST']) +@login_required +def _editor_new(): + new_dataset = Dataset() + new_dataset.set_name('New Dataset') + success, message = new_dataset.create(data=[], default=False) + if not success: return jsonify({'error':message}), 400 + flash(message, 'success') + return jsonify({'success': message, 'redirect_to': url_for('editor._editor_console', id=new_dataset.id)}), 200 \ No newline at end of file diff --git a/ref-test/app/editor/views.py b/ref-test/app/editor/views.py index fd729c4..b2c1887 100644 --- a/ref-test/app/editor/views.py +++ b/ref-test/app/editor/views.py @@ -1,4 +1,10 @@ -from flask import Blueprint, render_template +from ..forms.admin import EditDataset +from ..models import Dataset, User +from ..tools.forms import get_dataset_choices, send_errors_to_client + +from flask import Blueprint, flash, jsonify, redirect, render_template, request +from flask.helpers import url_for +from flask_login import login_required editor = Blueprint( name='editor', @@ -7,6 +13,26 @@ editor = Blueprint( static_folder='static' ) -@editor.route('/') +@editor.route('/', methods=['GET','POST']) +@login_required def _editor(): - return render_template('/editor/index.html') \ No newline at end of file + form = EditDataset() + form.dataset.choices = get_dataset_choices() + if request.method == 'POST': + if form.validate_on_submit(): + id = request.form.get('dataset') + return jsonify({'success': 'Selected dataset', 'redirect_to': url_for('editor._editor_console', id=id)}),200 + return send_errors_to_client(form=form) + form.process() + return render_template('/editor/index.html', form=form) + +@editor.route('//') +@login_required +def _editor_console(id:str=None): + dataset = Dataset.query.filter_by(id=id).first() + datasets = Dataset.query.count() + users = User.query.all() + if not dataset: + flash('Invalid dataset ID.', 'error') + return redirect(url_for('admin._questions')) + return render_template('/editor/console.html', dataset=dataset, datasets=datasets, users=users) \ No newline at end of file