from ..models import Dataset, Test from ..tools.data import analyse, check_dataset_exists, check_test_exists from ..tools.logs import write from ..tools.data import parse_questions from flask import Blueprint, render_template, request, jsonify from flask.helpers import abort, flash, redirect, url_for from flask_login import login_required analysis = Blueprint( name='analysis', import_name=__name__, template_folder='templates', static_folder='static' ) @analysis.route('/', methods=['GET','POST']) @login_required @check_dataset_exists @check_test_exists def _analysis(): try: _tests = Test.query.all() _datasets = Dataset.query.all() except Exception as exception: write('system.log', f'Database error when processing request \'{request.url}\': {exception}') return abort(500) tests = [ test for test in _tests if test.entries ] datasets = [ dataset for dataset in _datasets if dataset.entries ] if request.method == 'POST': selection = request.get_json() if selection['class'] == 'test': try: test = Test.query.filter_by(id=selection['id']).first() except Exception as exception: write('system.log', f'Database error when processing request \'{request.url}\': {exception}') return abort(500) if not test: return jsonify({'error': 'Invalid entry ID.'}), 404 return url_for('analysis._test', id=selection['id']), 200 if selection['class'] == 'dataset': try: dataset = Dataset.query.filter_by(id=selection['id']).first() except Exception as exception: write('system.log', f'Database error when processing request \'{request.url}\': {exception}') return abort(500) if not dataset: return jsonify({'error': 'Invalid entry ID.'}), 404 return url_for('analysis._dataset', id=selection['id']), 200 return jsonify({'error': 'Invalid entry ID.'}), 404 return render_template('/analysis/index.html', tests=tests, datasets=datasets) @analysis.route('/test/') @analysis.route('/test/') @login_required @check_test_exists def _test(id:str=None): if id in [None, '']: flash(message='Please select a valid exam.', category='error') return redirect(url_for('analysis._analysis')) try: test = Test.query.filter_by(id=id).first() except Exception as exception: write('system.log', f'Database error when processing request \'{request.url}\': {exception}') return abort(500) if not test: flash('Invalid exam.', 'error') return redirect(url_for('analysis._analysis')) return render_template('/analysis/analysis.html', analysis=analyse(test), subject=test.get_code(), type='exam', dataset=test.dataset, questions=parse_questions(test.dataset.get_data())) @analysis.route('/dataset/') @analysis.route('/dataset/') @login_required @check_dataset_exists def _dataset(id:str=None): if id in [None, '']: flash(message='Please select a valid dataset.', category='error') return redirect(url_for('analysis._analysis')) try: dataset = Dataset.query.filter_by(id=id).first() except Exception as exception: write('system.log', f'Database error when processing request \'{request.url}\': {exception}') return abort(500) if not dataset: flash('Invalid dataset.', 'error') return redirect(url_for('analysis._analysis')) return render_template('/analysis/analysis.html', analysis=analyse(dataset), subject=dataset.get_name(), type='dataset', dataset=dataset, questions=parse_questions(dataset.get_data()))