nopaque/app/blueprints/contributions/spacy_nlp_pipeline_models/routes.py

71 lines
2.5 KiB
Python
Raw Normal View History

2023-03-14 10:13:35 +00:00
from flask import abort, flash, redirect, render_template, url_for
2024-11-14 13:36:18 +00:00
from flask_login import current_user, login_required
2023-03-14 10:13:35 +00:00
from app import db
from app.models import SpaCyNLPPipelineModel
from . import bp
from .forms import (
CreateSpaCyNLPPipelineModelForm,
2023-03-31 07:14:21 +00:00
UpdateSpaCyNLPPipelineModelForm
2023-03-14 10:13:35 +00:00
)
2024-11-14 13:36:18 +00:00
@bp.route('/')
@login_required
def index():
2024-11-15 14:59:08 +00:00
return redirect(url_for('contributions.index', _anchor='spacy-nlp-pipeline-models'))
2023-03-14 10:13:35 +00:00
2024-11-14 13:36:18 +00:00
@bp.route('/create', methods=['GET', 'POST'])
@login_required
def create():
form = CreateSpaCyNLPPipelineModelForm()
2023-03-14 10:13:35 +00:00
if form.is_submitted():
if not form.validate():
return {'errors': form.errors}, 400
try:
snpm = SpaCyNLPPipelineModel.create(
form.spacy_model_file.data,
compatible_service_versions=form.compatible_service_versions.data,
description=form.description.data,
pipeline_name=form.pipeline_name.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()
flash(f'SpaCy NLP Pipeline model "{snpm.title}" created')
2024-11-14 13:36:18 +00:00
return {}, 201, {'Location': url_for('.index')}
2023-03-14 10:13:35 +00:00
return render_template(
2024-11-15 14:59:08 +00:00
'contributions/spacy_nlp_pipeline_models/create.html.j2',
2023-03-28 12:19:37 +00:00
title='Create SpaCy NLP Pipeline Model',
form=form
2023-03-14 10:13:35 +00:00
)
2024-11-14 13:36:18 +00:00
@bp.route('/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
@login_required
2024-11-15 14:59:08 +00:00
def entity(spacy_nlp_pipeline_model_id):
2023-03-14 10:13:35 +00:00
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
if not (snpm.user == current_user or current_user.is_administrator):
2023-04-11 09:46:33 +00:00
abort(403)
2023-03-31 07:14:21 +00:00
form = UpdateSpaCyNLPPipelineModelForm(data=snpm.to_json_serializeable())
2023-03-14 10:13:35 +00:00
if form.validate_on_submit():
form.populate_obj(snpm)
if db.session.is_modified(snpm):
flash(f'SpaCy NLP Pipeline model "{snpm.title}" updated')
db.session.commit()
2024-11-14 13:36:18 +00:00
return redirect(url_for('.index'))
2023-03-14 10:13:35 +00:00
return render_template(
2024-11-15 14:59:08 +00:00
'contributions/spacy_nlp_pipeline_models/entity.html.j2',
2023-03-28 12:19:37 +00:00
title=f'{snpm.title} {snpm.version}',
2023-03-14 10:13:35 +00:00
form=form,
2023-03-28 12:19:37 +00:00
spacy_nlp_pipeline_model=snpm
2023-03-14 10:13:35 +00:00
)