From f1677e29316bdf8072463a648bb9c79f968ff8b1 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Wed, 16 Nov 2022 14:15:52 +0100 Subject: [PATCH] Fix url issues and remove unused modules --- app/contributions/routes.py | 16 +++ app/corpora/query_results_forms.py | 21 --- app/corpora/query_results_routes.py | 135 ------------------ app/corpora/query_results_tasks.py | 13 -- .../js/RessourceLists/QueryResultList.js | 134 ----------------- app/static/js/RessourceLists/RessourceList.js | 3 +- .../SpacyNLPPipelineModelList.js | 6 +- app/static/js/Utils.js | 16 +-- app/templates/_scripts.html.j2 | 1 - .../contributions/contributions.html.j2 | 56 ++++---- .../spacy_nlp_pipeline_models.html.j2 | 25 ++++ .../tesseract_ocr_pipeline_models.html.j2 | 25 ++++ app/templates/corpora/analyse_corpus.html.j2 | 4 - app/templates/main/dashboard.html.j2 | 29 ++-- .../services/spacy_nlp_pipeline.html.j2 | 2 +- .../services/tesseract_ocr_pipeline.html.j2 | 2 +- 16 files changed, 114 insertions(+), 374 deletions(-) delete mode 100644 app/corpora/query_results_forms.py delete mode 100644 app/corpora/query_results_routes.py delete mode 100644 app/corpora/query_results_tasks.py delete mode 100644 app/static/js/RessourceLists/QueryResultList.js create mode 100644 app/templates/contributions/spacy_nlp_pipeline_models.html.j2 create mode 100644 app/templates/contributions/tesseract_ocr_pipeline_models.html.j2 diff --git a/app/contributions/routes.py b/app/contributions/routes.py index 1466444c..79ed3fa8 100644 --- a/app/contributions/routes.py +++ b/app/contributions/routes.py @@ -32,6 +32,14 @@ def contributions(): ) +@bp.route('/tesseract-ocr-pipeline-models') +def tesseract_ocr_pipeline_models(): + return render_template( + 'contributions/tesseract_ocr_pipeline_models.html.j2', + title='Tesseract OCR Pipeline Models' + ) + + @bp.route('/tesseract-ocr-pipeline-models/', methods=['GET', 'POST']) def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id): tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id) @@ -127,6 +135,14 @@ def create_tesseract_ocr_pipeline_model(): ) +@bp.route('/spacy-nlp-pipeline-models') +def spacy_nlp_pipeline_models(): + return render_template( + 'contributions/spacy_nlp_pipeline_models.html.j2', + title='SpaCy NLP Pipeline Models' + ) + + @bp.route('/spacy-nlp-pipeline-models/', methods=['GET', 'POST']) def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id): spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id) diff --git a/app/corpora/query_results_forms.py b/app/corpora/query_results_forms.py deleted file mode 100644 index bb55e513..00000000 --- a/app/corpora/query_results_forms.py +++ /dev/null @@ -1,21 +0,0 @@ -from flask_wtf import FlaskForm -from werkzeug.utils import secure_filename -from wtforms import FileField, StringField, SubmitField, ValidationError -from wtforms.validators import DataRequired, Length - - -class AddQueryResultForm(FlaskForm): - ''' - Form used to import one result json file. - ''' - description = StringField('Description', - validators=[DataRequired(), Length(1, 255)]) - file = FileField('File', validators=[DataRequired()]) - title = StringField('Title', validators=[DataRequired(), Length(1, 32)]) - submit = SubmitField() - - def validate_file(self, field): - if not field.data.filename.lower().endswith('.json'): - raise ValidationError('File does not have an approved extension: ' - '.json') - field.data.filename = secure_filename(field.data.filename) diff --git a/app/corpora/query_results_routes.py b/app/corpora/query_results_routes.py deleted file mode 100644 index 6c22e11e..00000000 --- a/app/corpora/query_results_routes.py +++ /dev/null @@ -1,135 +0,0 @@ -from flask import (abort, current_app, flash, make_response, redirect, request, - render_template, url_for, send_from_directory) -from flask_login import current_user, login_required -from . import bp -from . import tasks -from .forms import (AddQueryResultForm, DisplayOptionsForm, - InspectDisplayOptionsForm) -from .. import db -from ..models import QueryResult -import json -import os - - -@bp.route('/result/add', methods=['GET', 'POST']) -@login_required -def add_query_result(): - ''' - View to import a result as a json file. - ''' - abort(503) - form = AddQueryResultForm(prefix='add-query-result-form') - if form.is_submitted(): - if not form.validate(): - return make_response(form.errors, 400) - query_result = QueryResult(user=current_user, - description=form.description.data, - filename=form.file.data.filename, - title=form.title.data) - db.session.add(query_result) - db.session.flush() - db.session.refresh(query_result) - try: - os.makedirs(os.path.dirname(query_result.path)) - except OSError: - current_app.logger.error( - f'Make dir {query_result.path} led to an OSError!') - db.session.rollback() - flash('Internal Server Error', 'error') - return make_response( - {'redirect_url': url_for('.add_query_result')}, 500) - # save the uploaded file - form.file.data.save(query_result.path) - # parse json from file - with open(query_result.path, 'r') as file: - query_result_file_content = json.load(file) - # parse json schema - # with open('app/static/json_schema/nopaque_cqi_py_results_schema.json', 'r') as file: # noqa - # schema = json.load(file) - # try: - # # validate imported json file - # validate(instance=query_result_file_content, schema=schema) - # except Exception: - # tasks.delete_query_result(query_result.id) - # flash('Uploaded file is invalid', 'result') - # return make_response( - # {'redirect_url': url_for('.add_query_result')}, 201) - query_result_file_content.pop('matches') - query_result_file_content.pop('cpos_lookup') - query_result.query_metadata = query_result_file_content - db.session.commit() - flash('Query result added', 'result') - return make_response({'redirect_url': url_for('.query_result', query_result_id=query_result.id)}, 201) # noqa - return render_template('corpora/query_results/add_query_result.html.j2', - form=form, title='Add query result') - - -@bp.route('/result/') -@login_required -def query_result(query_result_id): - abort(503) - query_result = QueryResult.query.get_or_404(query_result_id) - if not (query_result.user == current_user - or current_user.is_administrator()): - abort(403) - return render_template('corpora/query_results/query_result.html.j2', - query_result=query_result, title='Query result') - - -@bp.route('/result//inspect') -@login_required -def inspect_query_result(query_result_id): - ''' - View to inspect imported result file in a corpus analysis like interface - ''' - abort(503) - query_result = QueryResult.query.get_or_404(query_result_id) - query_metadata = query_result.query_metadata - if not (query_result.user == current_user - or current_user.is_administrator()): - abort(403) - display_options_form = DisplayOptionsForm( - prefix='display-options-form', - results_per_page=request.args.get('results_per_page', 30), - result_context=request.args.get('context', 20) - ) - inspect_display_options_form = InspectDisplayOptionsForm( - prefix='inspect-display-options-form' - ) - with open(query_result.path, 'r') as query_result_file: - query_result_file_content = json.load(query_result_file) - return render_template( - 'corpora/query_results/inspect.html.j2', - query_result=query_result, - display_options_form=display_options_form, - inspect_display_options_form=inspect_display_options_form, # noqa - query_result_file_content=query_result_file_content, - query_metadata=query_metadata, - title='Inspect query result' - ) - - -@bp.route('/result//delete') -@login_required -def delete_query_result(query_result_id): - abort(503) - query_result = QueryResult.query.get_or_404(query_result_id) - if not (query_result.user == current_user - or current_user.is_administrator()): - abort(403) - flash(f'Query result "{query_result}" marked for deletion', 'result') - tasks.delete_query_result(query_result_id) - return redirect(url_for('services.service', service="corpus_analysis")) - - -@bp.route('/result//download') -@login_required -def download_query_result(query_result_id): - abort(503) - query_result = QueryResult.query.get_or_404(query_result_id) - if not (query_result.user == current_user - or current_user.is_administrator()): - abort(403) - return send_from_directory(as_attachment=True, - directory=os.path.dirname(query_result.path), - filename=query_result.filename) diff --git a/app/corpora/query_results_tasks.py b/app/corpora/query_results_tasks.py deleted file mode 100644 index 653096b3..00000000 --- a/app/corpora/query_results_tasks.py +++ /dev/null @@ -1,13 +0,0 @@ -from .. import db -from ..decorators import background -from ..models import QueryResult - - -@background -def delete_query_result(query_result_id, *args, **kwargs): - with kwargs['app'].app_context(): - query_result = QueryResult.query.get(query_result_id) - if query_result is None: - raise Exception(f'QueryResult {query_result_id} not found') - query_result.delete() - db.session.commit() diff --git a/app/static/js/RessourceLists/QueryResultList.js b/app/static/js/RessourceLists/QueryResultList.js deleted file mode 100644 index cffc4318..00000000 --- a/app/static/js/RessourceLists/QueryResultList.js +++ /dev/null @@ -1,134 +0,0 @@ -class QueryResultList extends RessourceList { - static autoInit() { - for (let queryResultListElement of document.querySelectorAll('.query-result-list:not(.no-autoinit)')) { - new QueryResultList(queryResultListElement); - } - } - - static options = { - item: ` - -

-
- - delete - send - - - `.trim(), - ressourceMapper: queryResult => { - return { - 'id': queryResult.id, - 'corpus-title': queryResult.corpus_title, - 'creation-date': queryResult.creation_date, - 'description': queryResult.description, - 'query': queryResult.query, - 'title': queryResult.title - }; - }, - sortArgs: ['creation-date', {order: 'desc'}], - valueNames: [ - {data: ['id']}, - {data: ['creation-date']}, - 'corpus-title', - 'description', - 'query', - 'title' - ] - }; - - constructor(listElement, options = {}) { - super(listElement, {...QueryResultList.options, ...options}); - } - - init(user) { - super._init(user.query_results); - } - - onclick(event) { - let action; - let actionButtonElement; - let deleteModal; - let deleteModalElement; - let queryResultElement; - let queryResultId; - let tmp; - - queryResultElement = event.target.closest('tr[data-id]'); - if (queryResultElement === null) {return;} - queryResultId = queryResultElement.dataset.id; - actionButtonElement = event.target.closest('.action-button[data-action]'); - action = actionButtonElement === null ? 'view' : actionButtonElement.dataset.action; - switch (action) { - case 'delete': - tmp = document.createElement('div'); - tmp.innerHTML = ` - - `.trim(); - deleteModalElement = document.querySelector('#modals').appendChild(tmp.firstChild); - deleteModal = M.Modal.init( - deleteModalElement, - { - onCloseEnd: () => { - deleteModal.destroy(); - deleteModalElement.remove(); - } - } - ); - deleteModal.open(); - break; - case 'view': - window.location.href = `/query_results/${queryResultId}`; - break; - default: - break; - } - } - - onPATCH(patch) { - let filteredPatch; - let match; - let operation; - let queryResultId; - let re; - let valueName; - - re = new RegExp(`^/users/${this.userId}/query_results/([A-Za-z0-9]*)`); - filteredPatch = patch.filter(operation => re.test(operation.path)); - for (operation of filteredPatch) { - switch(operation.op) { - case 'add': - re = new RegExp(`^/users/${this.userId}/query_results/([A-Za-z0-9]*)$`); - if (re.test(operation.path)) { - this.add(operation.value); - } - break; - case 'remove': - re = new RegExp(`^/users/${this.userId}/query_results/([A-Za-z0-9]*)$`); - if (re.test(operation.path)) { - [match, queryResultId] = operation.path.match(re); - this.remove(queryResultId); - } - break; - case 'replace': - re = new RegExp(`^/users/${this.userId}/query_results/([A-Za-z0-9]*)/(corpus_title|description|query|title)$`); - if (re.test(operation.path)) { - [match, queryResultId, valueName] = operation.path.match(re); - this.replace(queryResultId, valueName.replace('_', '-'), operation.value); - } - break; - default: - break; - } - } - } -} diff --git a/app/static/js/RessourceLists/RessourceList.js b/app/static/js/RessourceLists/RessourceList.js index a2242054..871a1e2f 100644 --- a/app/static/js/RessourceLists/RessourceList.js +++ b/app/static/js/RessourceLists/RessourceList.js @@ -10,9 +10,8 @@ class RessourceList { JobList.autoInit(); JobInputList.autoInit(); JobResultList.autoInit(); - SpacyNLPPipelineModelList.autoInit(); + SpaCyNLPPipelineModelList.autoInit(); TesseractOCRPipelineModelList.autoInit(); - QueryResultList.autoInit(); UserList.autoInit(); } diff --git a/app/static/js/RessourceLists/SpacyNLPPipelineModelList.js b/app/static/js/RessourceLists/SpacyNLPPipelineModelList.js index fcc68a0d..9a760be0 100644 --- a/app/static/js/RessourceLists/SpacyNLPPipelineModelList.js +++ b/app/static/js/RessourceLists/SpacyNLPPipelineModelList.js @@ -1,7 +1,7 @@ -class SpacyNLPPipelineModelList extends RessourceList { +class SpaCyNLPPipelineModelList extends RessourceList { static autoInit() { for (let spaCyNLPPipelineModelListElement of document.querySelectorAll('.spacy-nlp-pipeline-model-list:not(.no-autoinit)')) { - new SpacyNLPPipelineModelList(spaCyNLPPipelineModelListElement); + new SpaCyNLPPipelineModelList(spaCyNLPPipelineModelListElement); } } @@ -70,7 +70,7 @@ class SpacyNLPPipelineModelList extends RessourceList { }; constructor(listElement, options = {}) { - super(listElement, {...SpacyNLPPipelineModelList.options, ...options}); + super(listElement, {...SpaCyNLPPipelineModelList.options, ...options}); } init (user) { diff --git a/app/static/js/Utils.js b/app/static/js/Utils.js index e6076e43..4a274054 100644 --- a/app/static/js/Utils.js +++ b/app/static/js/Utils.js @@ -33,8 +33,8 @@ class Utils { `