nopaque/app/contributions/tesseract_ocr_pipeline_models/routes.py

76 lines
2.8 KiB
Python
Raw Normal View History

2023-03-13 08:49:59 +00:00
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
2023-03-13 08:49:59 +00:00
from . import bp, template_base_dir
from .forms import (
CreateTesseractOCRPipelineModelForm,
EditTesseractOCRPipelineModelForm
)
@bp.route('')
@login_required
def tesseract_ocr_pipeline_models():
return render_template(
2023-03-13 08:49:59 +00:00
f'{template_base_dir}/tesseract_ocr_pipeline_models.html.j2',
title='Tesseract OCR Pipeline Models'
)
@bp.route('/create', methods=['GET', 'POST'])
@login_required
def create_tesseract_ocr_pipeline_model():
2023-03-13 08:49:59 +00:00
form_prefix = 'create-tesseract-ocr-pipeline-model-form'
form = CreateTesseractOCRPipelineModelForm(prefix=form_prefix)
if form.is_submitted():
if not form.validate():
2023-03-13 08:49:59 +00:00
return {'errors': form.errors}, 400
try:
2023-03-13 08:49:59 +00:00
topm = TesseractOCRPipelineModel.create(
form.tesseract_model_file.data,
compatible_service_versions=form.compatible_service_versions.data,
description=form.description.data,
publisher=form.publisher.data,
publisher_url=form.publisher_url.data,
publishing_url=form.publishing_url.data,
publishing_year=form.publishing_year.data,
is_public=False,
title=form.title.data,
version=form.version.data,
user=current_user
)
except OSError:
abort(500)
db.session.commit()
2023-03-13 08:49:59 +00:00
flash(f'Tesseract OCR Pipeline model "{topm.title}" created')
return {}, 201, {'Location': url_for('.tesseract_ocr_pipeline_models')}
return render_template(
2023-03-13 08:49:59 +00:00
f'{template_base_dir}/create_tesseract_ocr_pipeline_model.html.j2',
form=form,
title='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):
2023-03-13 08:49:59 +00:00
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
form_prefix = 'edit-tesseract-ocr-pipeline-model-form'
form = EditTesseractOCRPipelineModelForm(
2023-03-13 08:49:59 +00:00
data=topm.to_json_serializeable(),
prefix=form_prefix
)
if form.validate_on_submit():
2023-03-13 08:49:59 +00:00
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(
2023-03-13 08:49:59 +00:00
f'{template_base_dir}/tesseract_ocr_pipeline_model.html.j2',
form=form,
2023-03-13 08:49:59 +00:00
tesseract_ocr_pipeline_model=topm,
title=f'{topm.title} {topm.version}'
)