diff --git a/app/contributions/spacy_nlp_pipeline_models/__init__.py b/app/contributions/spacy_nlp_pipeline_models/__init__.py index 6b73681a..8ff119d0 100644 --- a/app/contributions/spacy_nlp_pipeline_models/__init__.py +++ b/app/contributions/spacy_nlp_pipeline_models/__init__.py @@ -1,7 +1,7 @@ from flask import Blueprint -TEMPLATE_FOLDER = 'contributions/spacy_nlp_pipeline_models' +template_base_dir = 'contributions/spacy_nlp_pipeline_models' bp = Blueprint('spacy_nlp_pipeline_models', __name__) diff --git a/app/contributions/spacy_nlp_pipeline_models/json_routes.py b/app/contributions/spacy_nlp_pipeline_models/json_routes.py index 9247f85b..f7a9a254 100644 --- a/app/contributions/spacy_nlp_pipeline_models/json_routes.py +++ b/app/contributions/spacy_nlp_pipeline_models/json_routes.py @@ -1,4 +1,4 @@ -from flask import abort, current_app, jsonify, request +from flask import abort, current_app, request from flask_login import login_required, current_user from threading import Thread from app import db @@ -29,9 +29,7 @@ def delete_spacy_model(spacy_nlp_pipeline_model_id): 'message': \ f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion' } - response = jsonify(resonse_data) - response.status_code = 202 - return response + return resonse_data, 202 @bp.route('//is_public', methods=['PUT']) @@ -53,6 +51,4 @@ def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id): f' is now {"public" if is_public else "private"}' ) } - response = jsonify(response_data) - response.status_code = 200 - return response + return response_data, 200 diff --git a/app/contributions/spacy_nlp_pipeline_models/routes.py b/app/contributions/spacy_nlp_pipeline_models/routes.py index 2e416c47..fd972902 100644 --- a/app/contributions/spacy_nlp_pipeline_models/routes.py +++ b/app/contributions/spacy_nlp_pipeline_models/routes.py @@ -1,8 +1,8 @@ -from flask import abort, flash, Markup, redirect, render_template, url_for -from flask_login import login_required, current_user +from flask import abort, flash, redirect, render_template, url_for +from flask_login import current_user, login_required from app import db from app.models import SpaCyNLPPipelineModel -from . import bp, TEMPLATE_FOLDER +from . import bp, template_base_dir from .forms import ( CreateSpaCyNLPPipelineModelForm, EditSpaCyNLPPipelineModelForm @@ -13,7 +13,7 @@ from .forms import ( @login_required def spacy_nlp_pipeline_models(): return render_template( - f'{TEMPLATE_FOLDER}/spacy_nlp_pipeline_models.html.j2', + f'{template_base_dir}/spacy_nlp_pipeline_models.html.j2', title='SpaCy NLP Pipeline Models' ) @@ -21,13 +21,13 @@ def spacy_nlp_pipeline_models(): @bp.route('/create', methods=['GET', 'POST']) @login_required def create_spacy_nlp_pipeline_model(): - form = CreateSpaCyNLPPipelineModelForm(prefix='create-spacy-nlp-pipeline-model-form') + form_prefix = 'create-spacy-nlp-pipeline-model-form' + form = CreateSpaCyNLPPipelineModelForm(prefix=form_prefix) if form.is_submitted(): if not form.validate(): - response = {'errors': form.errors} - return response, 400 + return {'errors': form.errors}, 400 try: - spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.create( + snpm = SpaCyNLPPipelineModel.create( form.spacy_model_file.data, compatible_service_versions=form.compatible_service_versions.data, description=form.description.data, @@ -44,18 +44,10 @@ def create_spacy_nlp_pipeline_model(): except OSError: abort(500) db.session.commit() - spacy_nlp_pipeline_model_url = url_for( - '.spacy_nlp_pipeline_model', - spacy_nlp_pipeline_model_id=spacy_nlp_pipeline_model.id - ) - message = Markup( - f'SpaCy NLP Pipeline model "{spacy_nlp_pipeline_model.title}" ' - 'created' - ) - flash(message) - return '', 201, {'Location': spacy_nlp_pipeline_model_url} + flash(f'SpaCy NLP Pipeline model "{snpm.title}" created') + return {}, 201, {'Location': url_for('.spacy_nlp_pipeline_models')} return render_template( - f'{TEMPLATE_FOLDER}/create_spacy_nlp_pipeline_model.html.j2', + f'{template_base_dir}/create_spacy_nlp_pipeline_model.html.j2', form=form, title='Create SpaCy NLP Pipeline Model' ) @@ -64,24 +56,21 @@ def create_spacy_nlp_pipeline_model(): @bp.route('/', methods=['GET', 'POST']) @login_required def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id): - spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id) + snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id) + form_prefix = 'edit-spacy-nlp-pipeline-model-form' form = EditSpaCyNLPPipelineModelForm( - data=spacy_nlp_pipeline_model.to_json_serializeable(), - prefix='edit-spacy-nlp-pipeline-model-form' + data=snpm.to_json_serializeable(), + prefix=form_prefix ) if form.validate_on_submit(): - form.populate_obj(spacy_nlp_pipeline_model) - if db.session.is_modified(spacy_nlp_pipeline_model): - message = Markup( - f'SpaCy NLP Pipeline model "{spacy_nlp_pipeline_model.title}" ' - 'updated' - ) - flash(message) + form.populate_obj(snpm) + if db.session.is_modified(snpm): + flash(f'SpaCy NLP Pipeline model "{snpm.title}" updated') db.session.commit() return redirect(url_for('.spacy_nlp_pipeline_models')) return render_template( - f'{TEMPLATE_FOLDER}/spacy_nlp_pipeline_model.html.j2', + f'{template_base_dir}/spacy_nlp_pipeline_model.html.j2', form=form, - spacy_nlp_pipeline_model=spacy_nlp_pipeline_model, - title=f'{spacy_nlp_pipeline_model.title} {spacy_nlp_pipeline_model.version}' + spacy_nlp_pipeline_model=snpm, + title=f'{snpm.title} {snpm.version}' ) diff --git a/app/contributions/tesseract_ocr_pipeline_models/__init__.py b/app/contributions/tesseract_ocr_pipeline_models/__init__.py index c60e0915..cf44126d 100644 --- a/app/contributions/tesseract_ocr_pipeline_models/__init__.py +++ b/app/contributions/tesseract_ocr_pipeline_models/__init__.py @@ -1,7 +1,7 @@ from flask import Blueprint -TEMPLATE_FOLDER = 'contributions/tesseract_ocr_pipeline_models' +template_base_dir = 'contributions/tesseract_ocr_pipeline_models' bp = Blueprint('tesseract_ocr_pipeline_models', __name__) diff --git a/app/contributions/tesseract_ocr_pipeline_models/json_routes.py b/app/contributions/tesseract_ocr_pipeline_models/json_routes.py index f4c4ca79..81aa6598 100644 --- a/app/contributions/tesseract_ocr_pipeline_models/json_routes.py +++ b/app/contributions/tesseract_ocr_pipeline_models/json_routes.py @@ -1,4 +1,4 @@ -from flask import abort, current_app, jsonify, request +from flask import abort, current_app, request from flask_login import login_required, current_user from threading import Thread from app import db @@ -29,9 +29,7 @@ def delete_tesseract_model(tesseract_ocr_pipeline_model_id): 'message': \ f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion' } - response = jsonify(response_data) - response.status_code = 202 - return response + return response_data, 202 @bp.route('//is_public', methods=['PUT']) @@ -53,6 +51,4 @@ def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_i f' is now {"public" if is_public else "private"}' ) } - response = jsonify(response_data) - response.status_code = 200 - return response + return response_data, 200 diff --git a/app/contributions/tesseract_ocr_pipeline_models/routes.py b/app/contributions/tesseract_ocr_pipeline_models/routes.py index 823e54d9..b888cef7 100644 --- a/app/contributions/tesseract_ocr_pipeline_models/routes.py +++ b/app/contributions/tesseract_ocr_pipeline_models/routes.py @@ -1,8 +1,8 @@ -from flask import abort, flash, Markup, redirect, render_template, url_for +from flask import abort, flash, redirect, render_template, url_for from flask_login import login_required, current_user from app import db from app.models import TesseractOCRPipelineModel -from . import bp, TEMPLATE_FOLDER +from . import bp, template_base_dir from .forms import ( CreateTesseractOCRPipelineModelForm, EditTesseractOCRPipelineModelForm @@ -13,7 +13,7 @@ from .forms import ( @login_required def tesseract_ocr_pipeline_models(): return render_template( - f'{TEMPLATE_FOLDER}/tesseract_ocr_pipeline_models.html.j2', + f'{template_base_dir}/tesseract_ocr_pipeline_models.html.j2', title='Tesseract OCR Pipeline Models' ) @@ -21,13 +21,13 @@ def tesseract_ocr_pipeline_models(): @bp.route('/create', methods=['GET', 'POST']) @login_required def create_tesseract_ocr_pipeline_model(): - form = CreateTesseractOCRPipelineModelForm(prefix='create-tesseract-ocr-pipeline-model-form') + form_prefix = 'create-tesseract-ocr-pipeline-model-form' + form = CreateTesseractOCRPipelineModelForm(prefix=form_prefix) if form.is_submitted(): if not form.validate(): - response = {'errors': form.errors} - return response, 400 + return {'errors': form.errors}, 400 try: - tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.create( + topm = TesseractOCRPipelineModel.create( form.tesseract_model_file.data, compatible_service_versions=form.compatible_service_versions.data, description=form.description.data, @@ -43,15 +43,10 @@ def create_tesseract_ocr_pipeline_model(): except OSError: abort(500) db.session.commit() - tesseract_ocr_pipeline_model_url = url_for( - '.tesseract_ocr_pipeline_model', - tesseract_ocr_pipeline_model_id=tesseract_ocr_pipeline_model.id - ) - message = Markup(f'Tesseract OCR Pipeline model "{tesseract_ocr_pipeline_model.title}" created') - flash(message) - return {}, 201, {'Location': tesseract_ocr_pipeline_model_url} + flash(f'Tesseract OCR Pipeline model "{topm.title}" created') + return {}, 201, {'Location': url_for('.tesseract_ocr_pipeline_models')} return render_template( - f'{TEMPLATE_FOLDER}/create_tesseract_ocr_pipeline_model.html.j2', + f'{template_base_dir}/create_tesseract_ocr_pipeline_model.html.j2', form=form, title='Create Tesseract OCR Pipeline Model' ) @@ -60,21 +55,21 @@ def create_tesseract_ocr_pipeline_model(): @bp.route('/', methods=['GET', 'POST']) @login_required def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id): - tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id) + topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id) + form_prefix = 'edit-tesseract-ocr-pipeline-model-form' form = EditTesseractOCRPipelineModelForm( - data=tesseract_ocr_pipeline_model.to_json_serializeable(), - prefix='edit-tesseract-ocr-pipeline-model-form' + data=topm.to_json_serializeable(), + prefix=form_prefix ) if form.validate_on_submit(): - form.populate_obj(tesseract_ocr_pipeline_model) - if db.session.is_modified(tesseract_ocr_pipeline_model): - message = Markup(f'Tesseract OCR Pipeline model "{tesseract_ocr_pipeline_model.title}" updated') - flash(message) + form.populate_obj(topm) + if db.session.is_modified(topm): + flash(f'Tesseract OCR Pipeline model "{topm.title}" updated') db.session.commit() return redirect(url_for('.tesseract_ocr_pipeline_models')) return render_template( - f'{TEMPLATE_FOLDER}/tesseract_ocr_pipeline_model.html.j2', + f'{template_base_dir}/tesseract_ocr_pipeline_model.html.j2', form=form, - tesseract_ocr_pipeline_model=tesseract_ocr_pipeline_model, - title=f'{tesseract_ocr_pipeline_model.title} {tesseract_ocr_pipeline_model.version}' + tesseract_ocr_pipeline_model=topm, + title=f'{topm.title} {topm.version}' ) diff --git a/app/corpora/files/__init__.py b/app/corpora/files/__init__.py index 2f47a450..a52ff995 100644 --- a/app/corpora/files/__init__.py +++ b/app/corpora/files/__init__.py @@ -1,7 +1,7 @@ from flask import Blueprint -TEMPLATE_FOLDER = 'corpora/files' +template_base_dir = 'corpora/files' bp = Blueprint('files', __name__) diff --git a/app/corpora/files/routes.py b/app/corpora/files/routes.py index 7715f417..5ef4e0d0 100644 --- a/app/corpora/files/routes.py +++ b/app/corpora/files/routes.py @@ -11,7 +11,7 @@ import os from app import db from app.models import Corpus, CorpusFile, CorpusStatus from ..decorators import corpus_follower_permission_required -from . import bp, TEMPLATE_FOLDER +from . import bp, template_base_dir from .forms import CreateCorpusFileForm, UpdateCorpusFileForm @@ -58,7 +58,7 @@ def create_corpus_file(corpus_id): flash(f'Corpus File "{corpus_file.filename}" added', category='corpus') return '', 201, {'Location': corpus.url} return render_template( - f'{TEMPLATE_FOLDER}/create_corpus_file.html.j2', + f'{template_base_dir}/create_corpus_file.html.j2', corpus=corpus, form=form, title='Add corpus file' @@ -79,7 +79,7 @@ def corpus_file(corpus_id, corpus_file_id): flash(f'Corpus file "{corpus_file.filename}" updated', category='corpus') return redirect(corpus_file.corpus.url) return render_template( - f'{TEMPLATE_FOLDER}/corpus_file.html.j2', + f'{template_base_dir}/corpus_file.html.j2', corpus=corpus_file.corpus, corpus_file=corpus_file, form=form, diff --git a/app/errors/__init__.py b/app/errors/__init__.py index 1f0480b4..847658fb 100644 --- a/app/errors/__init__.py +++ b/app/errors/__init__.py @@ -1,6 +1,6 @@ from werkzeug.exceptions import HTTPException -from .handlers import generic_error_handler +from .handlers import generic def init_app(app): - app.register_error_handler(HTTPException, generic_error_handler) + app.register_error_handler(HTTPException, generic) diff --git a/app/errors/handlers.py b/app/errors/handlers.py index 5a6c413d..fe7aaf4f 100644 --- a/app/errors/handlers.py +++ b/app/errors/handlers.py @@ -1,6 +1,13 @@ -from flask import render_template +from flask import jsonify, render_template, request, Response +from werkzeug.exceptions import HTTPException +from typing import Tuple, Union -def generic_error_handler(e): - print('test') - return render_template('errors/error.html.j2', error=e), e.code +def generic(error: HTTPException) -> Tuple[Union[str, Response], int]: + ''' Generic error handler ''' + accent_json: bool = request.accept_mimetypes.accept_json + accept_html: bool = request.accept_mimetypes.accept_html + if accent_json and not accept_html: + response: Response = jsonify(str(error)) + return response, error.code + return render_template('errors/error.html.j2', error=error), error.code diff --git a/app/static/js/Forms/Form.js b/app/static/js/Forms/Form.js index a9604c69..c3496e18 100644 --- a/app/static/js/Forms/Form.js +++ b/app/static/js/Forms/Form.js @@ -92,7 +92,6 @@ class Form { } if (request.status === 400) { let responseJson = JSON.parse(request.responseText); - console.log(responseJson); for (let [inputName, inputErrors] of Object.entries(responseJson.errors)) { let inputFieldElement = this.formElement .querySelector(`input[name$="${inputName}"], select[name$="${inputName}"]`) @@ -122,10 +121,11 @@ class Form { request.setRequestHeader('Accept', 'application/json'); let formData = new FormData(this.formElement); switch (this.formElement.enctype) { - case 'application/x-www-form-urlencoded': + case 'application/x-www-form-urlencoded': { let urlSearchParams = new URLSearchParams(formData); request.send(urlSearchParams); break; + } case 'multipart/form-data': { request.send(formData); break; diff --git a/app/templates/errors/error.html.j2 b/app/templates/errors/error.html.j2 index ef19de5f..8db723d2 100644 --- a/app/templates/errors/error.html.j2 +++ b/app/templates/errors/error.html.j2 @@ -4,7 +4,7 @@ {% block page_content %}
-

{{ error.name }}

+

{{ error.code }} {{ error.name }}

{{ error.description }}

{% endblock page_content %}