Codestyle enhancements

This commit is contained in:
Patrick Jentsch 2023-03-13 09:49:59 +01:00
parent ca53974e50
commit e03b5258ef
7 changed files with 52 additions and 76 deletions

View File

@ -1,7 +1,7 @@
from flask import Blueprint 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__) bp = Blueprint('spacy_nlp_pipeline_models', __name__)

View File

@ -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 flask_login import login_required, current_user
from threading import Thread from threading import Thread
from app import db from app import db
@ -29,9 +29,7 @@ def delete_spacy_model(spacy_nlp_pipeline_model_id):
'message': \ 'message': \
f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion' f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
} }
response = jsonify(resonse_data) return resonse_data, 202
response.status_code = 202
return response
@bp.route('/<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT']) @bp.route('/<hashid:spacy_nlp_pipeline_model_id>/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"}' f' is now {"public" if is_public else "private"}'
) )
} }
response = jsonify(response_data) return response_data, 200
response.status_code = 200
return response

View File

@ -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 flask_login import current_user, login_required
from app import db from app import db
from app.models import SpaCyNLPPipelineModel from app.models import SpaCyNLPPipelineModel
from . import bp, TEMPLATE_FOLDER from . import bp, template_base_dir
from .forms import ( from .forms import (
CreateSpaCyNLPPipelineModelForm, CreateSpaCyNLPPipelineModelForm,
EditSpaCyNLPPipelineModelForm EditSpaCyNLPPipelineModelForm
@ -13,21 +13,21 @@ from .forms import (
@login_required @login_required
def spacy_nlp_pipeline_models(): def spacy_nlp_pipeline_models():
return render_template( 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' title='SpaCy NLP Pipeline Models'
) )
@bp.route('/create', methods=['GET', 'POST']) @bp.route('/create')
@login_required @login_required
def create_spacy_nlp_pipeline_model(): 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 form.is_submitted():
if not form.validate(): if not form.validate():
response = {'errors': form.errors} return {'errors': form.errors}, 400
return response, 400
try: try:
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.create( snpm = SpaCyNLPPipelineModel.create(
form.spacy_model_file.data, form.spacy_model_file.data,
compatible_service_versions=form.compatible_service_versions.data, compatible_service_versions=form.compatible_service_versions.data,
description=form.description.data, description=form.description.data,
@ -44,18 +44,10 @@ def create_spacy_nlp_pipeline_model():
except OSError: except OSError:
abort(500) abort(500)
db.session.commit() db.session.commit()
spacy_nlp_pipeline_model_url = url_for( flash(f'SpaCy NLP Pipeline model "{snpm.title}" created')
'.spacy_nlp_pipeline_model', return {}, 201, {'Location': url_for('.spacy_nlp_pipeline_models')}
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}
return render_template( 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, form=form,
title='Create SpaCy NLP Pipeline Model' title='Create SpaCy NLP Pipeline Model'
) )
@ -64,24 +56,21 @@ def create_spacy_nlp_pipeline_model():
@bp.route('/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST']) @bp.route('/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
@login_required @login_required
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id): 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( form = EditSpaCyNLPPipelineModelForm(
data=spacy_nlp_pipeline_model.to_json_serializeable(), data=snpm.to_json_serializeable(),
prefix='edit-spacy-nlp-pipeline-model-form' prefix=form_prefix
) )
if form.validate_on_submit(): if form.validate_on_submit():
form.populate_obj(spacy_nlp_pipeline_model) form.populate_obj(snpm)
if db.session.is_modified(spacy_nlp_pipeline_model): if db.session.is_modified(snpm):
message = Markup( flash(f'SpaCy NLP Pipeline model "{snpm.title}" updated')
f'SpaCy NLP Pipeline model "{spacy_nlp_pipeline_model.title}" '
'updated'
)
flash(message)
db.session.commit() db.session.commit()
return redirect(url_for('.spacy_nlp_pipeline_models')) return redirect(url_for('.spacy_nlp_pipeline_models'))
return render_template( return render_template(
f'{TEMPLATE_FOLDER}/spacy_nlp_pipeline_model.html.j2', f'{template_base_dir}/spacy_nlp_pipeline_model.html.j2',
form=form, form=form,
spacy_nlp_pipeline_model=spacy_nlp_pipeline_model, spacy_nlp_pipeline_model=snpm,
title=f'{spacy_nlp_pipeline_model.title} {spacy_nlp_pipeline_model.version}' title=f'{snpm.title} {snpm.version}'
) )

View File

@ -1,7 +1,7 @@
from flask import Blueprint 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__) bp = Blueprint('tesseract_ocr_pipeline_models', __name__)

View File

@ -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 flask_login import login_required, current_user
from threading import Thread from threading import Thread
from app import db from app import db
@ -29,9 +29,7 @@ def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
'message': \ 'message': \
f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion' f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
} }
response = jsonify(response_data) return response_data, 202
response.status_code = 202
return response
@bp.route('/<hashid:tesseract_ocr_pipeline_model_id>/is_public', methods=['PUT']) @bp.route('/<hashid:tesseract_ocr_pipeline_model_id>/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"}' f' is now {"public" if is_public else "private"}'
) )
} }
response = jsonify(response_data) return response_data, 200
response.status_code = 200
return response

View File

@ -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 flask_login import login_required, current_user
from app import db from app import db
from app.models import TesseractOCRPipelineModel from app.models import TesseractOCRPipelineModel
from . import bp, TEMPLATE_FOLDER from . import bp, template_base_dir
from .forms import ( from .forms import (
CreateTesseractOCRPipelineModelForm, CreateTesseractOCRPipelineModelForm,
EditTesseractOCRPipelineModelForm EditTesseractOCRPipelineModelForm
@ -13,7 +13,7 @@ from .forms import (
@login_required @login_required
def tesseract_ocr_pipeline_models(): def tesseract_ocr_pipeline_models():
return render_template( 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' title='Tesseract OCR Pipeline Models'
) )
@ -21,13 +21,13 @@ def tesseract_ocr_pipeline_models():
@bp.route('/create', methods=['GET', 'POST']) @bp.route('/create', methods=['GET', 'POST'])
@login_required @login_required
def create_tesseract_ocr_pipeline_model(): 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 form.is_submitted():
if not form.validate(): if not form.validate():
response = {'errors': form.errors} return {'errors': form.errors}, 400
return response, 400
try: try:
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.create( topm = TesseractOCRPipelineModel.create(
form.tesseract_model_file.data, form.tesseract_model_file.data,
compatible_service_versions=form.compatible_service_versions.data, compatible_service_versions=form.compatible_service_versions.data,
description=form.description.data, description=form.description.data,
@ -43,15 +43,10 @@ def create_tesseract_ocr_pipeline_model():
except OSError: except OSError:
abort(500) abort(500)
db.session.commit() db.session.commit()
tesseract_ocr_pipeline_model_url = url_for( flash(f'Tesseract OCR Pipeline model "{topm.title}" created')
'.tesseract_ocr_pipeline_model', return {}, 201, {'Location': url_for('.tesseract_ocr_pipeline_models')}
tesseract_ocr_pipeline_model_id=tesseract_ocr_pipeline_model.id
)
message = Markup(f'Tesseract OCR Pipeline model "<a href="{tesseract_ocr_pipeline_model_url}">{tesseract_ocr_pipeline_model.title}</a>" created')
flash(message)
return {}, 201, {'Location': tesseract_ocr_pipeline_model_url}
return render_template( 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, form=form,
title='Create Tesseract OCR Pipeline Model' title='Create Tesseract OCR Pipeline Model'
) )
@ -60,21 +55,21 @@ def create_tesseract_ocr_pipeline_model():
@bp.route('/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST']) @bp.route('/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST'])
@login_required @login_required
def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id): 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( form = EditTesseractOCRPipelineModelForm(
data=tesseract_ocr_pipeline_model.to_json_serializeable(), data=topm.to_json_serializeable(),
prefix='edit-tesseract-ocr-pipeline-model-form' prefix=form_prefix
) )
if form.validate_on_submit(): if form.validate_on_submit():
form.populate_obj(tesseract_ocr_pipeline_model) form.populate_obj(topm)
if db.session.is_modified(tesseract_ocr_pipeline_model): if db.session.is_modified(topm):
message = Markup(f'Tesseract OCR Pipeline model "<a href="{tesseract_ocr_pipeline_model.url}">{tesseract_ocr_pipeline_model.title}</a>" updated') flash(f'Tesseract OCR Pipeline model "{topm.title}" updated')
flash(message)
db.session.commit() db.session.commit()
return redirect(url_for('.tesseract_ocr_pipeline_models')) return redirect(url_for('.tesseract_ocr_pipeline_models'))
return render_template( return render_template(
f'{TEMPLATE_FOLDER}/tesseract_ocr_pipeline_model.html.j2', f'{template_base_dir}/tesseract_ocr_pipeline_model.html.j2',
form=form, form=form,
tesseract_ocr_pipeline_model=tesseract_ocr_pipeline_model, tesseract_ocr_pipeline_model=topm,
title=f'{tesseract_ocr_pipeline_model.title} {tesseract_ocr_pipeline_model.version}' title=f'{topm.title} {topm.version}'
) )

View File

@ -92,7 +92,6 @@ class Form {
} }
if (request.status === 400) { if (request.status === 400) {
let responseJson = JSON.parse(request.responseText); let responseJson = JSON.parse(request.responseText);
console.log(responseJson);
for (let [inputName, inputErrors] of Object.entries(responseJson.errors)) { for (let [inputName, inputErrors] of Object.entries(responseJson.errors)) {
let inputFieldElement = this.formElement let inputFieldElement = this.formElement
.querySelector(`input[name$="${inputName}"], select[name$="${inputName}"]`) .querySelector(`input[name$="${inputName}"], select[name$="${inputName}"]`)
@ -122,10 +121,11 @@ class Form {
request.setRequestHeader('Accept', 'application/json'); request.setRequestHeader('Accept', 'application/json');
let formData = new FormData(this.formElement); let formData = new FormData(this.formElement);
switch (this.formElement.enctype) { switch (this.formElement.enctype) {
case 'application/x-www-form-urlencoded': case 'application/x-www-form-urlencoded': {
let urlSearchParams = new URLSearchParams(formData); let urlSearchParams = new URLSearchParams(formData);
request.send(urlSearchParams); request.send(urlSearchParams);
break; break;
}
case 'multipart/form-data': { case 'multipart/form-data': {
request.send(formData); request.send(formData);
break; break;