nopaque/app/contributions/routes.py

55 lines
1.7 KiB
Python
Raw Normal View History

2022-10-26 08:23:22 +00:00
from flask import abort, flash, Markup, render_template, url_for
from flask_login import login_required
from app import db
from app.decorators import permission_required
2022-10-26 08:23:22 +00:00
from app.models import TesseractOCRModel, Permission
from . import bp
from .forms import ContributionForm
@bp.before_request
@login_required
@permission_required(Permission.CONTRIBUTE)
def before_request():
pass
2022-10-26 08:23:22 +00:00
@bp.route('')
def contributions():
2022-10-26 08:23:22 +00:00
pass
2022-10-25 11:07:10 +00:00
2022-10-26 08:23:22 +00:00
@bp.route('/tesseract-ocr-pipeline-models', methods=['GET', 'POST'])
def tesseract_ocr_pipeline_models():
form = ContributionForm(
prefix='contribute-tesseract-ocr-pipeline-model-form'
)
2022-10-25 11:07:10 +00:00
if form.is_submitted():
if not form.validate():
response = {'errors': form.errors}
return response, 400
try:
2022-10-26 08:23:22 +00:00
tesseract_ocr_model = TesseractOCRModel.create(
form.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,
shared=form.shared.data,
title=form.title.data,
version=form.version.data
)
2022-10-25 11:07:10 +00:00
except OSError:
abort(500)
db.session.commit()
2022-10-26 08:23:22 +00:00
message = Markup(f'Model "{tesseract_ocr_model.title}" created')
flash(message)
return {}, 201, {'Location': url_for('contributions.contributions')}
return render_template(
'contributions/contribute.html.j2',
form=form,
title='Contribution'
)