diff --git a/web/app/results/views.py b/web/app/results/views.py index 1ea64407..3e2099f2 100644 --- a/web/app/results/views.py +++ b/web/app/results/views.py @@ -11,6 +11,7 @@ from flask_login import current_user, login_required import json import os from .. import logger +from jsonschema import validate @results.route('/import_results', methods=['GET', 'POST']) @@ -32,6 +33,7 @@ def import_results(): if not (result.creator == current_user or current_user.is_administrator()): abort(403) + # create paths to save the uploaded json file dir = os.path.join(str(result.user_id), 'results', 'corpus_analysis_results', @@ -40,23 +42,43 @@ def import_results(): abs_file_path = os.path.join(abs_dir, import_results_form.file.data.filename) os.makedirs(abs_dir) + # save the json file import_results_form.file.data.save(abs_file_path) - # Saves all needed metadata entries in one json field - with open(abs_file_path, 'r') as f: - corpus_metadata = json.load(f) - del corpus_metadata['matches'] - del corpus_metadata['cpos_lookup'] - result_file = ResultFile( - result_id=result.id, - dir=dir, - filename=import_results_form.file.data.filename) - result.corpus_metadata = corpus_metadata + # Create ResultFile db entry + result_file = ResultFile(result_id=result.id, + dir=dir, + filename=import_results_form.file.data.filename) # noqa db.session.add(result_file) db.session.commit() - flash('Result file added!', 'result') - return make_response( - {'redirect_url': url_for('results.results_overview')}, - 201) + # reads uploaded json file + with open(abs_file_path, 'r') as f: + corpus_metadata = json.load(f) + try: + # open json schema to validate against it + with open('app/static/json_schema/nopaque_cqi_py_results_schema.json', # noqa + 'r') as s: + schema = json.load(s) + # validate if imported json is actually a json result file + validate(instance=corpus_metadata, schema=schema) + # if validated continue + # delete matches and cpos_lookup from read json file + del corpus_metadata['matches'] + del corpus_metadata['cpos_lookup'] + # save metadate directly as json into one field + result.corpus_metadata = corpus_metadata + flash('Result file added!', 'result') + db.session.commit() + return make_response( + {'redirect_url': url_for('results.results_overview')}, + 201) + except Exception as e: + # this runs if validation fails + flash('Uploaded file was not a valid result JSON!', 'result') + # deletes before created Result and ResultFile db entries + tasks.delete_result(result.id) + return make_response( + {'redirect_url': url_for('results.import_results')}, + 201) return render_template('results/import_results.html.j2', import_results_form=import_results_form, title='Add corpus file') @@ -70,7 +92,6 @@ def results_overview(): ''' # get all results of current user results = User.query.get(current_user.id).results - logger.warning(results) def __p_time(time_str): # helper to convert the datetime into a nice readable string diff --git a/web/app/static/js/nopaque.lists.js b/web/app/static/js/nopaque.lists.js index 8c464263..2fad01ab 100644 --- a/web/app/static/js/nopaque.lists.js +++ b/web/app/static/js/nopaque.lists.js @@ -162,7 +162,7 @@ RessourceList.options = { -
Filename | -{# Actions #} | -
---|
Filename | +{# Actions #} | +
---|
No results yet improted.
+No results yet imported.