mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
Simplify code for editing models via forms
This commit is contained in:
parent
527ab5ac0f
commit
e92624f0c0
@ -13,7 +13,7 @@ from wtforms.validators import InputRequired, Length
|
|||||||
from app.services import SERVICES
|
from app.services import SERVICES
|
||||||
|
|
||||||
|
|
||||||
class CreateContributionBaseForm(FlaskForm):
|
class ContributionBaseForm(FlaskForm):
|
||||||
title = StringField(
|
title = StringField(
|
||||||
'Title',
|
'Title',
|
||||||
validators=[InputRequired(), Length(max=64)]
|
validators=[InputRequired(), Length(max=64)]
|
||||||
@ -48,7 +48,7 @@ class CreateContributionBaseForm(FlaskForm):
|
|||||||
submit = SubmitField()
|
submit = SubmitField()
|
||||||
|
|
||||||
|
|
||||||
class CreateTesseractOCRPipelineModelForm(CreateContributionBaseForm):
|
class CreateTesseractOCRPipelineModelForm(ContributionBaseForm):
|
||||||
tesseract_model_file = FileField(
|
tesseract_model_file = FileField(
|
||||||
'File',
|
'File',
|
||||||
validators=[FileRequired()]
|
validators=[FileRequired()]
|
||||||
@ -72,7 +72,7 @@ class CreateTesseractOCRPipelineModelForm(CreateContributionBaseForm):
|
|||||||
self.compatible_service_versions.default = ''
|
self.compatible_service_versions.default = ''
|
||||||
|
|
||||||
|
|
||||||
class CreateSpaCyNLPPipelineModelForm(CreateContributionBaseForm):
|
class CreateSpaCyNLPPipelineModelForm(ContributionBaseForm):
|
||||||
spacy_model_file = FileField(
|
spacy_model_file = FileField(
|
||||||
'File',
|
'File',
|
||||||
validators=[FileRequired()]
|
validators=[FileRequired()]
|
||||||
@ -100,17 +100,8 @@ class CreateSpaCyNLPPipelineModelForm(CreateContributionBaseForm):
|
|||||||
self.compatible_service_versions.default = ''
|
self.compatible_service_versions.default = ''
|
||||||
|
|
||||||
|
|
||||||
class EditContributionBaseForm(CreateContributionBaseForm):
|
class EditContributionBaseForm(ContributionBaseForm):
|
||||||
def prefill(self, model_file):
|
pass
|
||||||
''' Pre-fill the form with data of an exististing corpus file '''
|
|
||||||
self.title.data = model_file.title
|
|
||||||
self.description.data = model_file.description
|
|
||||||
self.publisher.data = model_file.publisher
|
|
||||||
self.publishing_year.data = model_file.publishing_year
|
|
||||||
self.publisher_url.data = model_file.publisher_url
|
|
||||||
self.publishing_url.data = model_file.publishing_url
|
|
||||||
self.version.data = model_file.version
|
|
||||||
self.shared.data = model_file.shared
|
|
||||||
|
|
||||||
|
|
||||||
class EditTesseractOCRPipelineModelForm(EditContributionBaseForm):
|
class EditTesseractOCRPipelineModelForm(EditContributionBaseForm):
|
||||||
@ -122,7 +113,3 @@ class EditSpaCyNLPPipelineModelForm(EditContributionBaseForm):
|
|||||||
'Pipeline name',
|
'Pipeline name',
|
||||||
validators=[InputRequired(), Length(max=64)]
|
validators=[InputRequired(), Length(max=64)]
|
||||||
)
|
)
|
||||||
|
|
||||||
def prefill(self, model_file):
|
|
||||||
super().prefill(model_file)
|
|
||||||
self.pipeline_name.data = model_file.pipeline_name
|
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
from flask import abort, current_app, flash, Markup, render_template, url_for
|
from flask import (
|
||||||
|
abort,
|
||||||
|
current_app,
|
||||||
|
flash,
|
||||||
|
Markup,
|
||||||
|
redirect,
|
||||||
|
render_template,
|
||||||
|
url_for
|
||||||
|
)
|
||||||
from flask_login import login_required, current_user
|
from flask_login import login_required, current_user
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from app import db
|
from app import db
|
||||||
@ -43,33 +51,17 @@ def tesseract_ocr_pipeline_models():
|
|||||||
@bp.route('/tesseract-ocr-pipeline-models/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST'])
|
@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):
|
def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
|
||||||
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
||||||
form = EditTesseractOCRPipelineModelForm(prefix='edit-tesseract-ocr-pipeline-model-form')
|
form = EditTesseractOCRPipelineModelForm(
|
||||||
|
obj=tesseract_ocr_pipeline_model,
|
||||||
|
prefix='edit-tesseract-ocr-pipeline-model-form'
|
||||||
|
)
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
if tesseract_ocr_pipeline_model.title != form.title.data:
|
form.populate_obj(tesseract_ocr_pipeline_model)
|
||||||
tesseract_ocr_pipeline_model.title = form.title.data
|
if db.session.is_modified(tesseract_ocr_pipeline_model):
|
||||||
if tesseract_ocr_pipeline_model.description != form.description.data:
|
message = Markup(f'Tesseract OCR Pipeline model "<a href="{tesseract_ocr_pipeline_model.url}">{tesseract_ocr_pipeline_model.title}</a>" updated')
|
||||||
tesseract_ocr_pipeline_model.description = form.description.data
|
flash(message)
|
||||||
if tesseract_ocr_pipeline_model.publisher != form.publisher.data:
|
db.session.commit()
|
||||||
tesseract_ocr_pipeline_model.publisher = form.publisher.data
|
return redirect(url_for('.tesseract_ocr_pipeline_models'))
|
||||||
if tesseract_ocr_pipeline_model.publishing_year != form.publishing_year.data:
|
|
||||||
tesseract_ocr_pipeline_model.publishing_year = form.publishing_year.data
|
|
||||||
if tesseract_ocr_pipeline_model.publisher_url != form.publisher_url.data:
|
|
||||||
tesseract_ocr_pipeline_model.publisher_url = form.publisher_url.data
|
|
||||||
if tesseract_ocr_pipeline_model.publishing_url != form.publishing_url.data:
|
|
||||||
tesseract_ocr_pipeline_model.publishing_url = form.publishing_url.data
|
|
||||||
if tesseract_ocr_pipeline_model.version != form.version.data:
|
|
||||||
tesseract_ocr_pipeline_model.version = form.version.data
|
|
||||||
if tesseract_ocr_pipeline_model.shared != form.shared.data:
|
|
||||||
tesseract_ocr_pipeline_model.shared = form.shared.data
|
|
||||||
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 "<a href="{tesseract_ocr_pipeline_model_url}">{tesseract_ocr_pipeline_model.title}</a>" updated')
|
|
||||||
flash(message)
|
|
||||||
return {}, 201, {'Location': tesseract_ocr_pipeline_model_url}
|
|
||||||
form.prefill(tesseract_ocr_pipeline_model)
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'contributions/tesseract_ocr_pipeline_model.html.j2',
|
'contributions/tesseract_ocr_pipeline_model.html.j2',
|
||||||
form=form,
|
form=form,
|
||||||
@ -146,36 +138,17 @@ def spacy_nlp_pipeline_models():
|
|||||||
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
|
@bp.route('/spacy-nlp-pipeline-models/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
|
||||||
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
|
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
|
||||||
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
||||||
form = EditSpaCyNLPPipelineModelForm(prefix='edit-spacy-nlp-pipeline-model-form')
|
form = EditSpaCyNLPPipelineModelForm(
|
||||||
|
obj=spacy_nlp_pipeline_model,
|
||||||
|
prefix='edit-spacy-nlp-pipeline-model-form'
|
||||||
|
)
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
if spacy_nlp_pipeline_model.title != form.title.data:
|
form.populate_obj(spacy_nlp_pipeline_model)
|
||||||
spacy_nlp_pipeline_model.title = form.title.data
|
if db.session.is_modified(spacy_nlp_pipeline_model):
|
||||||
if spacy_nlp_pipeline_model.description != form.description.data:
|
message = Markup(f'SpaCy NLP Pipeline model "<a href="{spacy_nlp_pipeline_model.url}">{spacy_nlp_pipeline_model.title}</a>" updated')
|
||||||
spacy_nlp_pipeline_model.description = form.description.data
|
flash(message)
|
||||||
if spacy_nlp_pipeline_model.pipeline_name != form.pipeline_name.data:
|
db.session.commit()
|
||||||
spacy_nlp_pipeline_model.pipeline_name = form.pipeline_name.data
|
return redirect(url_for('.spacy_nlp_pipeline_models'))
|
||||||
if spacy_nlp_pipeline_model.publisher != form.publisher.data:
|
|
||||||
spacy_nlp_pipeline_model.publisher = form.publisher.data
|
|
||||||
if spacy_nlp_pipeline_model.publishing_year != form.publishing_year.data:
|
|
||||||
spacy_nlp_pipeline_model.publishing_year = form.publishing_year.data
|
|
||||||
if spacy_nlp_pipeline_model.publisher_url != form.publisher_url.data:
|
|
||||||
spacy_nlp_pipeline_model.publisher_url = form.publisher_url.data
|
|
||||||
if spacy_nlp_pipeline_model.publishing_url != form.publishing_url.data:
|
|
||||||
spacy_nlp_pipeline_model.publishing_url = form.publishing_url.data
|
|
||||||
if spacy_nlp_pipeline_model.version != form.version.data:
|
|
||||||
spacy_nlp_pipeline_model.version = form.version.data
|
|
||||||
if spacy_nlp_pipeline_model.shared != form.shared.data:
|
|
||||||
spacy_nlp_pipeline_model.shared = form.shared.data
|
|
||||||
current_app.logger.warning(db.session.dirty)
|
|
||||||
db.session.commit()
|
|
||||||
spacy_nlp_pipeline_model_url = url_for(
|
|
||||||
'.spacy_nlp_pipeline_model',
|
|
||||||
spacy_nlp_pipeline_model_id=spacy_nlp_pipeline_model.id
|
|
||||||
)
|
|
||||||
message = Markup(f'SpaCy NLP Pipeline model "<a href="{spacy_nlp_pipeline_model_url}">{spacy_nlp_pipeline_model.title}</a>" updated')
|
|
||||||
flash(message)
|
|
||||||
return {}, 201, {'Location': url_for('.contributions')}
|
|
||||||
form.prefill(spacy_nlp_pipeline_model)
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'contributions/spacy_nlp_pipeline_model.html.j2',
|
'contributions/spacy_nlp_pipeline_model.html.j2',
|
||||||
form=form,
|
form=form,
|
||||||
|
@ -47,20 +47,7 @@ class CreateCorpusFileForm(CorpusFileBaseForm):
|
|||||||
|
|
||||||
|
|
||||||
class EditCorpusFileForm(CorpusFileBaseForm):
|
class EditCorpusFileForm(CorpusFileBaseForm):
|
||||||
def prefill(self, corpus_file):
|
pass
|
||||||
''' Pre-fill the form with data of an exististing corpus file '''
|
|
||||||
self.address.data = corpus_file.address
|
|
||||||
self.author.data = corpus_file.author
|
|
||||||
self.booktitle.data = corpus_file.booktitle
|
|
||||||
self.chapter.data = corpus_file.chapter
|
|
||||||
self.editor.data = corpus_file.editor
|
|
||||||
self.institution.data = corpus_file.institution
|
|
||||||
self.journal.data = corpus_file.journal
|
|
||||||
self.pages.data = corpus_file.pages
|
|
||||||
self.publisher.data = corpus_file.publisher
|
|
||||||
self.publishing_year.data = corpus_file.publishing_year
|
|
||||||
self.school.data = corpus_file.school
|
|
||||||
self.title.data = corpus_file.title
|
|
||||||
|
|
||||||
|
|
||||||
class ImportCorpusForm(FlaskForm):
|
class ImportCorpusForm(FlaskForm):
|
||||||
|
@ -176,52 +176,15 @@ def corpus_file(corpus_id, corpus_file_id):
|
|||||||
abort(404)
|
abort(404)
|
||||||
if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
|
if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
|
||||||
abort(403)
|
abort(403)
|
||||||
form = EditCorpusFileForm(prefix='edit-corpus-file-form')
|
form = EditCorpusFileForm(obj=corpus_file, prefix='edit-corpus-file-form')
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
has_changes = False
|
form.populate_obj(corpus_file)
|
||||||
if corpus_file.address != form.address.data:
|
if db.session.is_modified(corpus_file):
|
||||||
corpus_file.address = form.address.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.author != form.author.data:
|
|
||||||
corpus_file.author = form.author.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.booktitle != form.booktitle.data:
|
|
||||||
corpus_file.booktitle = form.booktitle.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.chapter != form.chapter.data:
|
|
||||||
corpus_file.chapter = form.chapter.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.editor != form.editor.data:
|
|
||||||
corpus_file.editor = form.editor.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.institution != form.institution.data:
|
|
||||||
corpus_file.institution = form.institution.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.journal != form.journal.data:
|
|
||||||
corpus_file.journal = form.journal.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.pages != form.pages.data:
|
|
||||||
corpus_file.pages = form.pages.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.publisher != form.publisher.data:
|
|
||||||
corpus_file.publisher = form.publisher.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.publishing_year != form.publishing_year.data:
|
|
||||||
corpus_file.publishing_year = form.publishing_year.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.school != form.school.data:
|
|
||||||
corpus_file.school = form.school.data
|
|
||||||
has_changes = True
|
|
||||||
if corpus_file.title != form.title.data:
|
|
||||||
corpus_file.title = form.title.data
|
|
||||||
has_changes = True
|
|
||||||
if has_changes:
|
|
||||||
corpus_file.corpus.status = CorpusStatus.UNPREPARED
|
corpus_file.corpus.status = CorpusStatus.UNPREPARED
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
message = Markup(f'Corpus file "<a href="{corpus_file.url}">{corpus_file.filename}</a>" updated')
|
message = Markup(f'Corpus file "<a href="{corpus_file.url}">{corpus_file.filename}</a>" updated')
|
||||||
flash(message, category='corpus')
|
flash(message, category='corpus')
|
||||||
return redirect(corpus_file.corpus.url)
|
return redirect(corpus_file.corpus.url)
|
||||||
form.prefill(corpus_file)
|
|
||||||
return render_template(
|
return render_template(
|
||||||
'corpora/corpus_file.html.j2',
|
'corpora/corpus_file.html.j2',
|
||||||
corpus=corpus_file.corpus,
|
corpus=corpus_file.corpus,
|
||||||
|
@ -552,6 +552,13 @@ class TesseractOCRPipelineModel(FileMixin, HashidMixin, db.Model):
|
|||||||
str(self.id)
|
str(self.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self):
|
||||||
|
return url_for(
|
||||||
|
'contributions.tesseract_ocr_pipeline_model',
|
||||||
|
tesseract_ocr_pipeline_model_id=self.id
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def insert_defaults():
|
def insert_defaults():
|
||||||
nopaque_user = User.query.filter_by(username='nopaque').first()
|
nopaque_user = User.query.filter_by(username='nopaque').first()
|
||||||
@ -660,6 +667,13 @@ class SpaCyNLPPipelineModel(FileMixin, HashidMixin, db.Model):
|
|||||||
str(self.id)
|
str(self.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self):
|
||||||
|
return url_for(
|
||||||
|
'contributions.spacy_nlp_pipeline_model',
|
||||||
|
spacy_nlp_pipeline_model_id=self.id
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def insert_defaults():
|
def insert_defaults():
|
||||||
nopaque_user = User.query.filter_by(username='nopaque').first()
|
nopaque_user = User.query.filter_by(username='nopaque').first()
|
||||||
|
Loading…
Reference in New Issue
Block a user