from flask_wtf import FlaskForm from werkzeug.utils import secure_filename from wtforms import FileField, StringField, SubmitField, ValidationError from wtforms.validators import DataRequired, Length class AddQueryResultForm(FlaskForm): ''' Form used to import one result json file. ''' description = StringField('Description', validators=[DataRequired(), Length(1, 255)]) file = FileField('File', validators=[DataRequired()]) title = StringField('Title', validators=[DataRequired(), Length(1, 32)]) submit = SubmitField() def validate_file(self, field): if not field.data.filename.lower().endswith('.json'): raise ValidationError('File does not have an approved extension: ' '.json') field.data.filename = secure_filename(field.data.filename)