2022-11-17 09:46:16 +00:00
|
|
|
from flask import (
|
|
|
|
abort,
|
|
|
|
current_app,
|
|
|
|
flash,
|
2023-03-06 14:03:13 +00:00
|
|
|
jsonify,
|
2022-11-17 09:46:16 +00:00
|
|
|
Markup,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
2023-03-06 14:03:13 +00:00
|
|
|
request,
|
2022-11-17 09:46:16 +00:00
|
|
|
url_for
|
|
|
|
)
|
2022-11-03 14:38:35 +00:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from threading import Thread
|
2022-10-26 08:23:22 +00:00
|
|
|
from app import db
|
2023-03-06 14:03:13 +00:00
|
|
|
from app.decorators import content_negotiation, permission_required
|
|
|
|
from app.models import SpaCyNLPPipelineModel, TesseractOCRPipelineModel
|
2022-02-03 11:39:16 +00:00
|
|
|
from . import bp
|
2022-11-15 14:11:16 +00:00
|
|
|
from .forms import (
|
|
|
|
CreateSpaCyNLPPipelineModelForm,
|
|
|
|
CreateTesseractOCRPipelineModelForm,
|
|
|
|
EditSpaCyNLPPipelineModelForm,
|
|
|
|
EditTesseractOCRPipelineModelForm
|
|
|
|
)
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.before_request
|
|
|
|
@login_required
|
|
|
|
def before_request():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-11-03 14:38:35 +00:00
|
|
|
@bp.route('/')
|
2022-09-02 11:02:04 +00:00
|
|
|
def contributions():
|
2022-11-03 14:38:35 +00:00
|
|
|
return render_template(
|
2022-11-15 14:11:16 +00:00
|
|
|
'contributions/contributions.html.j2',
|
|
|
|
title='Contributions'
|
2022-11-03 14:38:35 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 14:11:16 +00:00
|
|
|
|
2023-03-06 14:03:13 +00:00
|
|
|
##############################################################################
|
|
|
|
# SpaCy NLP Pipeline Models #
|
|
|
|
##############################################################################
|
|
|
|
#region spacy-nlp-pipeline-models
|
|
|
|
@bp.route('/spacy-nlp-pipeline-models')
|
|
|
|
def spacy_nlp_pipeline_models():
|
2022-11-03 14:38:35 +00:00
|
|
|
return render_template(
|
2023-03-06 14:03:13 +00:00
|
|
|
'contributions/spacy_nlp_pipeline_models.html.j2',
|
|
|
|
title='SpaCy NLP Pipeline Models'
|
2022-11-03 14:38:35 +00:00
|
|
|
)
|
2022-10-26 08:23:22 +00:00
|
|
|
|
2022-11-15 14:11:16 +00:00
|
|
|
|
2023-03-06 14:03:13 +00:00
|
|
|
@bp.route('/spacy-nlp-pipeline-models/create', methods=['GET', 'POST'])
|
|
|
|
def create_spacy_nlp_pipeline_model():
|
|
|
|
form = CreateSpaCyNLPPipelineModelForm(prefix='create-spacy-nlp-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:
|
2023-03-06 14:03:13 +00:00
|
|
|
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.create(
|
|
|
|
form.spacy_model_file.data,
|
2022-10-26 08:23:22 +00:00
|
|
|
compatible_service_versions=form.compatible_service_versions.data,
|
|
|
|
description=form.description.data,
|
2023-03-06 14:03:13 +00:00
|
|
|
pipeline_name=form.pipeline_name.data,
|
2022-10-26 08:23:22 +00:00
|
|
|
publisher=form.publisher.data,
|
|
|
|
publisher_url=form.publisher_url.data,
|
|
|
|
publishing_url=form.publishing_url.data,
|
|
|
|
publishing_year=form.publishing_year.data,
|
2022-11-29 14:28:10 +00:00
|
|
|
is_public=False,
|
2022-10-26 08:23:22 +00:00
|
|
|
title=form.title.data,
|
2022-11-03 14:38:35 +00:00
|
|
|
version=form.version.data,
|
|
|
|
user=current_user
|
2022-10-26 08:23:22 +00:00
|
|
|
)
|
2022-10-25 11:07:10 +00:00
|
|
|
except OSError:
|
|
|
|
abort(500)
|
|
|
|
db.session.commit()
|
2023-03-06 14:03:13 +00:00
|
|
|
spacy_nlp_pipeline_model_url = url_for(
|
|
|
|
'.spacy_nlp_pipeline_model',
|
|
|
|
spacy_nlp_pipeline_model_id=spacy_nlp_pipeline_model.id
|
2022-11-15 14:11:16 +00:00
|
|
|
)
|
2023-03-06 14:03:13 +00:00
|
|
|
message = Markup(f'SpaCy NLP Pipeline model "<a href="{spacy_nlp_pipeline_model_url}">{spacy_nlp_pipeline_model.title}</a>" created')
|
2022-10-26 08:23:22 +00:00
|
|
|
flash(message)
|
2023-03-06 14:03:13 +00:00
|
|
|
return {}, 201, {'Location': spacy_nlp_pipeline_model_url}
|
2022-10-02 21:49:14 +00:00
|
|
|
return render_template(
|
2023-03-06 14:03:13 +00:00
|
|
|
'contributions/create_spacy_nlp_pipeline_model.html.j2',
|
2022-10-02 21:49:14 +00:00
|
|
|
form=form,
|
2023-03-06 14:03:13 +00:00
|
|
|
title='Create SpaCy NLP Pipeline Model'
|
2022-11-16 13:15:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-15 14:11:16 +00:00
|
|
|
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
|
2022-11-07 08:15:38 +00:00
|
|
|
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
|
2022-11-15 14:11:16 +00:00
|
|
|
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
2022-11-17 09:46:16 +00:00
|
|
|
form = EditSpaCyNLPPipelineModelForm(
|
2022-11-24 11:24:29 +00:00
|
|
|
data=spacy_nlp_pipeline_model.to_json_serializeable(),
|
2022-11-17 09:46:16 +00:00
|
|
|
prefix='edit-spacy-nlp-pipeline-model-form'
|
|
|
|
)
|
2022-11-07 08:15:38 +00:00
|
|
|
if form.validate_on_submit():
|
2022-11-17 09:46:16 +00:00
|
|
|
form.populate_obj(spacy_nlp_pipeline_model)
|
|
|
|
if db.session.is_modified(spacy_nlp_pipeline_model):
|
|
|
|
message = Markup(f'SpaCy NLP Pipeline model "<a href="{spacy_nlp_pipeline_model.url}">{spacy_nlp_pipeline_model.title}</a>" updated')
|
|
|
|
flash(message)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('.spacy_nlp_pipeline_models'))
|
2022-11-07 08:15:38 +00:00
|
|
|
return render_template(
|
|
|
|
'contributions/spacy_nlp_pipeline_model.html.j2',
|
|
|
|
form=form,
|
2022-11-15 14:11:16 +00:00
|
|
|
spacy_nlp_pipeline_model=spacy_nlp_pipeline_model,
|
2022-11-18 12:46:52 +00:00
|
|
|
title=f'{spacy_nlp_pipeline_model.title} {spacy_nlp_pipeline_model.version}'
|
2022-11-07 08:15:38 +00:00
|
|
|
)
|
|
|
|
|
2023-03-06 14:03:13 +00:00
|
|
|
|
2023-03-07 15:34:49 +00:00
|
|
|
#region json-routes
|
2022-11-15 14:11:16 +00:00
|
|
|
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['DELETE'])
|
2023-03-07 15:34:49 +00:00
|
|
|
@login_required
|
|
|
|
@content_negotiation(produces='application/json')
|
2022-11-07 08:15:38 +00:00
|
|
|
def delete_spacy_model(spacy_nlp_pipeline_model_id):
|
|
|
|
def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
|
|
|
|
with app.app_context():
|
2022-11-15 14:11:16 +00:00
|
|
|
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
|
|
|
|
spacy_nlp_pipeline_model.delete()
|
2022-11-07 08:15:38 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-03-07 15:34:49 +00:00
|
|
|
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
|
|
|
if not (snpm.user == current_user or current_user.is_administrator()):
|
2022-11-07 08:15:38 +00:00
|
|
|
abort(403)
|
|
|
|
thread = Thread(
|
|
|
|
target=_delete_spacy_model,
|
|
|
|
args=(current_app._get_current_object(), spacy_nlp_pipeline_model_id)
|
|
|
|
)
|
|
|
|
thread.start()
|
2023-03-07 15:34:49 +00:00
|
|
|
response = jsonify(
|
|
|
|
f'SpaCy NLP Pipeline Model "{snpm.title}" marked for deletion'
|
|
|
|
)
|
|
|
|
response.status_code = 202
|
|
|
|
return response
|
2022-11-07 08:15:38 +00:00
|
|
|
|
2022-11-15 14:11:16 +00:00
|
|
|
|
2023-03-06 14:03:13 +00:00
|
|
|
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>/is_public', methods=['PUT'])
|
|
|
|
@login_required
|
|
|
|
@permission_required('CONTRIBUTE')
|
|
|
|
@content_negotiation(consumes='application/json', produces='application/json')
|
|
|
|
def update_spacy_nlp_pipeline_model_is_public(spacy_nlp_pipeline_model_id):
|
|
|
|
is_public = request.json
|
|
|
|
if not isinstance(is_public, bool):
|
2023-03-07 15:34:49 +00:00
|
|
|
abort(400)
|
|
|
|
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
|
|
|
if not (snpm.user == current_user or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
snpm.is_public = is_public
|
2023-03-06 14:03:13 +00:00
|
|
|
db.session.commit()
|
|
|
|
response = jsonify(
|
2023-03-07 15:34:49 +00:00
|
|
|
f'SpaCy NLP Pipeline Model "{snpm.title}"'
|
2023-03-06 14:03:13 +00:00
|
|
|
f' is now {"public" if is_public else "private"}'
|
|
|
|
)
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|
|
|
|
#endregion json-routes
|
|
|
|
#endregion spacy-nlp-pipeline-models
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
# Tesseract OCR Pipeline Models #
|
|
|
|
##############################################################################
|
|
|
|
#region tesseract-ocr-pipeline-models
|
|
|
|
@bp.route('/tesseract-ocr-pipeline-models')
|
|
|
|
def tesseract_ocr_pipeline_models():
|
|
|
|
return render_template(
|
|
|
|
'contributions/tesseract_ocr_pipeline_models.html.j2',
|
|
|
|
title='Tesseract OCR Pipeline Models'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/tesseract-ocr-pipeline-models/create', methods=['GET', 'POST'])
|
|
|
|
def create_tesseract_ocr_pipeline_model():
|
|
|
|
form = CreateTesseractOCRPipelineModelForm(prefix='create-tesseract-ocr-pipeline-model-form')
|
2022-11-07 08:15:38 +00:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
response = {'errors': form.errors}
|
|
|
|
return response, 400
|
|
|
|
try:
|
2023-03-06 14:03:13 +00:00
|
|
|
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.create(
|
|
|
|
form.tesseract_model_file.data,
|
2022-11-07 08:15:38 +00:00
|
|
|
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,
|
2022-11-29 14:28:10 +00:00
|
|
|
is_public=False,
|
2022-11-07 08:15:38 +00:00
|
|
|
title=form.title.data,
|
|
|
|
version=form.version.data,
|
|
|
|
user=current_user
|
|
|
|
)
|
|
|
|
except OSError:
|
|
|
|
abort(500)
|
|
|
|
db.session.commit()
|
2023-03-06 14:03:13 +00:00
|
|
|
tesseract_ocr_pipeline_model_url = url_for(
|
|
|
|
'.tesseract_ocr_pipeline_model',
|
|
|
|
tesseract_ocr_pipeline_model_id=tesseract_ocr_pipeline_model.id
|
2022-11-15 14:11:16 +00:00
|
|
|
)
|
2023-03-06 14:03:13 +00:00
|
|
|
message = Markup(f'Tesseract OCR Pipeline model "<a href="{tesseract_ocr_pipeline_model_url}">{tesseract_ocr_pipeline_model.title}</a>" created')
|
2022-11-07 08:15:38 +00:00
|
|
|
flash(message)
|
2023-03-06 14:03:13 +00:00
|
|
|
return {}, 201, {'Location': tesseract_ocr_pipeline_model_url}
|
2022-11-07 08:15:38 +00:00
|
|
|
return render_template(
|
2023-03-06 14:03:13 +00:00
|
|
|
'contributions/create_tesseract_ocr_pipeline_model.html.j2',
|
2022-11-07 08:15:38 +00:00
|
|
|
form=form,
|
2023-03-06 14:03:13 +00:00
|
|
|
title='Create Tesseract OCR Pipeline Model'
|
2022-11-07 08:15:38 +00:00
|
|
|
)
|
2022-11-17 11:11:21 +00:00
|
|
|
|
2023-03-06 14:03:13 +00:00
|
|
|
|
|
|
|
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST'])
|
|
|
|
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 "<a href="{tesseract_ocr_pipeline_model.url}">{tesseract_ocr_pipeline_model.title}</a>" updated')
|
|
|
|
flash(message)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('.tesseract_ocr_pipeline_models'))
|
|
|
|
return render_template(
|
|
|
|
'contributions/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}'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-07 15:34:49 +00:00
|
|
|
#region json-routes
|
2023-03-06 14:03:13 +00:00
|
|
|
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['DELETE'])
|
2023-03-07 15:34:49 +00:00
|
|
|
@login_required
|
|
|
|
@content_negotiation(produces='application/json')
|
2023-03-06 14:03:13 +00:00
|
|
|
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
|
|
|
|
def _delete_tesseract_ocr_pipeline_model(app, tesseract_ocr_pipeline_model_id):
|
|
|
|
with app.app_context():
|
|
|
|
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
|
|
|
|
tesseract_ocr_pipeline_model.delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
2023-03-07 15:34:49 +00:00
|
|
|
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
|
|
|
if not (topm.user == current_user or current_user.is_administrator()):
|
2022-11-17 11:11:21 +00:00
|
|
|
abort(403)
|
2023-03-06 14:03:13 +00:00
|
|
|
thread = Thread(
|
|
|
|
target=_delete_tesseract_ocr_pipeline_model,
|
|
|
|
args=(current_app._get_current_object(), tesseract_ocr_pipeline_model_id)
|
|
|
|
)
|
|
|
|
thread.start()
|
2023-03-07 15:34:49 +00:00
|
|
|
response = jsonify(
|
|
|
|
f'Tesseract OCR Pipeline Model "{topm.title}" marked for deletion'
|
|
|
|
)
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|
2023-03-06 14:03:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>/is_public', methods=['PUT'])
|
|
|
|
@login_required
|
|
|
|
@permission_required('CONTRIBUTE')
|
|
|
|
@content_negotiation(consumes='application/json', produces='application/json')
|
|
|
|
def update_tesseract_ocr_pipeline_model_is_public(tesseract_ocr_pipeline_model_id):
|
|
|
|
is_public = request.json
|
|
|
|
if not isinstance(is_public, bool):
|
2023-03-07 15:34:49 +00:00
|
|
|
abort(400)
|
|
|
|
topm = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
|
|
|
if not (topm.user == current_user or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
topm.is_public = is_public
|
2022-11-17 11:11:21 +00:00
|
|
|
db.session.commit()
|
2023-03-06 14:03:13 +00:00
|
|
|
response = jsonify(
|
2023-03-07 15:34:49 +00:00
|
|
|
f'Tesseract OCR Pipeline Model "{topm.title}"'
|
2023-03-06 14:03:13 +00:00
|
|
|
f' is now {"public" if is_public else "private"}'
|
|
|
|
)
|
|
|
|
response.status_code = 200
|
|
|
|
return response
|
|
|
|
#endregion json-routes
|
|
|
|
#endregion tesseract-ocr-pipeline-models
|