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
TEMPLATE_FOLDER = 'contributions/spacy_nlp_pipeline_models'
template_base_dir = 'contributions/spacy_nlp_pipeline_models'
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 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('/<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"}'
)
}
response = jsonify(response_data)
response.status_code = 200
return response
return response_data, 200

View File

@ -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,21 +13,21 @@ 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'
)
@bp.route('/create', methods=['GET', 'POST'])
@bp.route('/create')
@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('/<hashid:spacy_nlp_pipeline_model_id>', 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}'
)

View File

@ -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__)

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 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('/<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"}'
)
}
response = jsonify(response_data)
response.status_code = 200
return response
return response_data, 200

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 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 "<a href="{tesseract_ocr_pipeline_model_url}">{tesseract_ocr_pipeline_model.title}</a>" 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('/<hashid:tesseract_ocr_pipeline_model_id>', 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 "<a href="{tesseract_ocr_pipeline_model.url}">{tesseract_ocr_pipeline_model.title}</a>" 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}'
)

View File

@ -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;