from flask_wtf import FlaskForm from werkzeug.utils import secure_filename from wtforms import (FileField, StringField, SubmitField, ValidationError, IntegerField) from wtforms.validators import DataRequired, Length class AddCorpusFileForm(FlaskForm): ''' Form to add a .vrt corpus file to the current corpus. ''' # Required fields author = StringField('Author', validators=[DataRequired(), Length(1, 255)]) file = FileField('File', validators=[DataRequired()]) publishing_year = IntegerField('Publishing year', validators=[DataRequired()]) title = StringField('Title', validators=[DataRequired(), Length(1, 255)]) # Optional fields address = StringField('Adress', validators=[Length(0, 255)]) booktitle = StringField('Booktitle', validators=[Length(0, 255)]) chapter = StringField('Chapter', validators=[Length(0, 255)]) editor = StringField('Editor', validators=[Length(0, 255)]) institution = StringField('Institution', validators=[Length(0, 255)]) journal = StringField('Journal', validators=[Length(0, 255)]) pages = StringField('Pages', validators=[Length(0, 255)]) publisher = StringField('Publisher', validators=[Length(0, 255)]) school = StringField('School', validators=[Length(0, 255)]) submit = SubmitField() def __init__(self, corpus, *args, **kwargs): super().__init__(*args, **kwargs) self.corpus = corpus def validate_file(self, field): if not field.data.filename.lower().endswith('.vrt'): raise ValidationError('File does not have an approved extension: ' '.vrt') field.data.filename = secure_filename(field.data.filename) for corpus_file in self.corpus.files: if field.data.filename == corpus_file.filename: raise ValidationError('File already registered to corpus.') class EditCorpusFileForm(FlaskForm): ''' Form to edit meta data of one corpus file. ''' # Required fields author = StringField('Author', validators=[DataRequired(), Length(1, 255)]) publishing_year = IntegerField('Publishing year', validators=[DataRequired()]) title = StringField('Title', validators=[DataRequired(), Length(1, 255)]) # Optional fields address = StringField('Adress', validators=[Length(0, 255)]) booktitle = StringField('Booktitle', validators=[Length(0, 255)]) chapter = StringField('Chapter', validators=[Length(0, 255)]) editor = StringField('Editor', validators=[Length(0, 255)]) institution = StringField('Institution', validators=[Length(0, 255)]) journal = StringField('Journal', validators=[Length(0, 255)]) pages = StringField('Pages', validators=[Length(0, 255)]) publisher = StringField('Publisher', validators=[Length(0, 255)]) school = StringField('School', validators=[Length(0, 255)]) submit = SubmitField() class AddCorpusForm(FlaskForm): ''' Form to add a a new corpus. ''' description = StringField('Description', validators=[DataRequired(), Length(1, 255)]) submit = SubmitField() title = StringField('Title', validators=[DataRequired(), Length(1, 32)]) class ImportCorpusForm(FlaskForm): ''' Form to import a corpus. ''' description = StringField('Description', validators=[DataRequired(), Length(1, 255)]) file = FileField('File', validators=[DataRequired()]) submit = SubmitField() title = StringField('Title', validators=[DataRequired(), Length(1, 32)]) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def validate_file(self, field): if not field.data.filename.lower().endswith('.zip'): raise ValidationError('File does not have an approved extension: ' '.zip') field.data.filename = secure_filename(field.data.filename)