from flask import abort, flash, Markup, 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 .forms import ( CreateTesseractOCRPipelineModelForm, EditTesseractOCRPipelineModelForm ) @bp.route('') @login_required def tesseract_ocr_pipeline_models(): return render_template( f'{TEMPLATE_FOLDER}/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(): form = CreateTesseractOCRPipelineModelForm(prefix='create-tesseract-ocr-pipeline-model-form') if form.is_submitted(): if not form.validate(): response = {'errors': form.errors} return response, 400 try: tesseract_ocr_pipeline_model = 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() 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} return render_template( f'{TEMPLATE_FOLDER}/create_tesseract_ocr_pipeline_model.html.j2', form=form, title='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) form = EditTesseractOCRPipelineModelForm( data=tesseract_ocr_pipeline_model.to_json_serializeable(), prefix='edit-tesseract-ocr-pipeline-model-form' ) 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) db.session.commit() return redirect(url_for('.tesseract_ocr_pipeline_models')) return render_template( f'{TEMPLATE_FOLDER}/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}' )