nopaque/app/contributions/forms.py

102 lines
3.4 KiB
Python
Raw Normal View History

2022-11-03 14:38:35 +00:00
from xml.dom import ValidationErr
from flask_wtf import FlaskForm
2022-10-25 11:07:10 +00:00
from flask_wtf.file import FileField, FileRequired
from wtforms import (
BooleanField,
StringField,
SubmitField,
SelectMultipleField,
2022-11-03 14:38:35 +00:00
IntegerField,
ValidationError
)
from wtforms.validators import InputRequired, Length
from app.services import SERVICES
2022-11-03 14:38:35 +00:00
class CreateContributionBaseForm(FlaskForm):
title = StringField(
'Title',
validators=[InputRequired(), Length(max=64)]
)
description = StringField(
'Description',
validators=[InputRequired(), Length(max=255)]
)
version = StringField(
'Version',
validators=[InputRequired(), Length(max=16)]
)
publisher = StringField(
'Publisher',
validators=[InputRequired(), Length(max=128)]
)
publisher_url = StringField(
'Publisher URL',
validators=[InputRequired(), Length(max=512)]
)
publishing_url = StringField(
'Publishing URL',
validators=[InputRequired(), Length(max=512)]
)
publishing_year = IntegerField(
'Publishing year',
validators=[InputRequired()]
)
2022-11-03 14:38:35 +00:00
shared = BooleanField(
'Shared'
)
submit = SubmitField()
2022-11-07 08:15:38 +00:00
class EditForm(CreateContributionBaseForm):
def prefill(self, model_file):
''' 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
2022-11-03 14:38:35 +00:00
class TesseractOCRModelContributionForm(CreateContributionBaseForm):
tesseract_model_file = FileField(
'File',
validators=[FileRequired()]
)
compatible_service_versions = SelectMultipleField(
'Compatible service versions'
)
def validate_traineddata(self, field):
if field.data.mimetype != '.traineddata':
raise ValidationError('traineddata files only!')
def __init__(self, *args, **kwargs):
service_manifest = SERVICES['tesseract-ocr-pipeline']
super().__init__(*args, **kwargs)
self.compatible_service_versions.choices = [('', 'Choose your option')]
self.compatible_service_versions.choices += [
(x, x) for x in service_manifest['versions'].keys()
]
self.compatible_service_versions.default = ''
2022-11-03 14:38:35 +00:00
2022-11-07 08:15:38 +00:00
class SpacyNLPModelContributionForm(CreateContributionBaseForm):
spacy_model_file = FileField(
'File',
validators=[FileRequired()]
)
compatible_service_versions = SelectMultipleField(
'Compatible service versions'
)
def validate_spacy(self, field):
if field.data.mimetype != '.tar.gz':
raise ValidationError('.tar.gz files only!')
2022-11-03 14:38:35 +00:00
2022-11-07 08:15:38 +00:00
def __init__(self, *args, **kwargs):
service_manifest = SERVICES['spacy-nlp-pipeline']
super().__init__(*args, **kwargs)
self.compatible_service_versions.choices = [('', 'Choose your option')]
self.compatible_service_versions.choices += [
(x, x) for x in service_manifest['versions'].keys()
]
self.compatible_service_versions.default = ''