2021-02-19 12:00:52 +00:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import (BooleanField, MultipleFileField, SelectField, StringField,
|
|
|
|
SubmitField, ValidationError)
|
|
|
|
from wtforms.validators import DataRequired, Length
|
|
|
|
from . import SERVICES
|
|
|
|
|
|
|
|
|
|
|
|
class AddJobForm(FlaskForm):
|
|
|
|
description = StringField('Description',
|
|
|
|
validators=[DataRequired(), Length(1, 255)])
|
|
|
|
submit = SubmitField()
|
|
|
|
title = StringField('Title', validators=[DataRequired(), Length(1, 32)])
|
2021-03-26 12:10:42 +00:00
|
|
|
version = SelectField('Version', validators=[DataRequired()])
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddNLPJobForm(AddJobForm):
|
|
|
|
check_encoding = BooleanField('Check encoding')
|
2021-03-26 12:10:42 +00:00
|
|
|
files = MultipleFileField('Files', validators=[DataRequired()])
|
|
|
|
language = SelectField('Language', choices=[('', 'Choose your option')],
|
|
|
|
default='', validators=[DataRequired()])
|
|
|
|
|
|
|
|
def validate_check_encoding(self, field):
|
|
|
|
if field.data and 'check_encoding' not in SERVICES['nlp']['versions'][self.version.data]: # noqa
|
|
|
|
raise ValidationError('Check encoding is not available in this version') # noqa
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
|
|
def validate_files(form, field):
|
|
|
|
for file in field.data:
|
|
|
|
if not file.filename.lower().endswith('.txt'):
|
|
|
|
raise ValidationError('File does not have an approved '
|
|
|
|
'extension: .txt')
|
|
|
|
|
2021-03-26 12:10:42 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
version = kwargs.pop('version', SERVICES['nlp']['versions']['latest'])
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if 'check_encoding' not in SERVICES['nlp']['versions'][version]:
|
|
|
|
self.check_encoding.render_kw = {'disabled': True}
|
|
|
|
self.language.choices += [(x, y) for x, y in SERVICES['nlp']['versions'][version]['models'].items()] # noqa
|
|
|
|
self.version.choices = [(x, x) for x in SERVICES['nlp']['versions'] if x != 'latest'] # noqa
|
|
|
|
self.version.default = version
|
|
|
|
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
|
|
class AddOCRJobForm(AddJobForm):
|
|
|
|
binarization = BooleanField('Binarazation')
|
|
|
|
files = MultipleFileField('Files', validators=[DataRequired()])
|
2021-03-26 12:10:42 +00:00
|
|
|
language = SelectField('Language', choices=[('', 'Choose your option')],
|
|
|
|
default='', validators=[DataRequired()])
|
|
|
|
|
|
|
|
def validate_binarization(self, field):
|
|
|
|
if field.data and 'binarization' not in SERVICES['ocr']['versions'][self.version.data]: # noqa
|
2021-02-19 12:00:52 +00:00
|
|
|
raise ValidationError('Binarization is not available in this version') # noqa
|
|
|
|
|
2021-03-26 12:10:42 +00:00
|
|
|
def validate_files(self, field):
|
2021-02-19 12:00:52 +00:00
|
|
|
for file in field.data:
|
|
|
|
if not file.filename.lower().endswith('.pdf'):
|
|
|
|
raise ValidationError('File does not have an approved '
|
|
|
|
'extension: .pdf')
|
|
|
|
|
2021-03-26 12:10:42 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
version = kwargs.pop('version', SERVICES['ocr']['versions']['latest'])
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
if 'binarization' not in SERVICES['ocr']['versions'][version]:
|
|
|
|
self.binarization.render_kw = {'disabled': True}
|
|
|
|
self.language.choices += [(x, y) for x, y in SERVICES['ocr']['versions'][version]['models'].items()] # noqa
|
|
|
|
self.version.choices = [(x, x) for x in SERVICES['ocr']['versions'] if x != 'latest'] # noqa
|
|
|
|
self.version.default = version
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddFileSetupJobForm(AddJobForm):
|
|
|
|
files = MultipleFileField('Files', validators=[DataRequired()])
|
|
|
|
|
|
|
|
def validate_files(form, field):
|
|
|
|
for file in field.data:
|
|
|
|
if not file.filename.lower().endswith(('.jpeg', '.jpg', '.png',
|
|
|
|
'.tiff', '.tif')):
|
|
|
|
raise ValidationError('File does not have an approved '
|
|
|
|
'extension: .jpeg | .jpg | .png | .tiff '
|
|
|
|
'| .tif')
|
2021-03-26 12:10:42 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2021-08-04 10:26:49 +00:00
|
|
|
version = kwargs.pop('version', SERVICES['file-setup']['versions']['latest'])
|
2021-03-26 12:10:42 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2021-08-04 10:26:49 +00:00
|
|
|
self.version.choices = [(x, x) for x in SERVICES['file-setup']['versions'] if x != 'latest'] # noqa
|
2021-03-26 12:10:42 +00:00
|
|
|
self.version.default = version
|
2021-08-04 10:26:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
AddJobForms = {
|
|
|
|
'file-setup': AddFileSetupJobForm,
|
|
|
|
'ocr': AddOCRJobForm,
|
|
|
|
'nlp': AddNLPJobForm
|
|
|
|
}
|