From df7d618b2a4fc2d98a3f17e8e39e3e26f0d4f93d Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 1 Aug 2019 14:01:22 +0200 Subject: [PATCH] Add file type validation --- app/main/forms.py | 12 +++++++----- app/services/forms.py | 10 +++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index d9f369fd..316be954 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -1,5 +1,5 @@ from flask_wtf import FlaskForm -from wtforms import MultipleFileField, StringField, SubmitField +from wtforms import MultipleFileField, StringField, SubmitField, ValidationError from wtforms.validators import DataRequired, Length @@ -14,8 +14,10 @@ class CreateCorpusForm(FlaskForm): 'Title', validators=[DataRequired(), Length(1, 32)] ) - ''' + def validate_files(form, field): - for file in form.files.getlist[field.name]: - if file.mimetype - ''' + for file in field.data: + if not file.filename.lower().endswith('.vrt'): + raise ValidationError( + 'File does not have an approved extension: .vrt' + ) diff --git a/app/services/forms.py b/app/services/forms.py index 655f0e58..d0c0146b 100644 --- a/app/services/forms.py +++ b/app/services/forms.py @@ -1,5 +1,5 @@ from flask_wtf import FlaskForm -from wtforms import MultipleFileField, SelectField, StringField, SubmitField +from wtforms import MultipleFileField, SelectField, StringField, SubmitField, ValidationError from wtforms.validators import DataRequired, Length @@ -29,3 +29,11 @@ class CreateOCRJobForm(FlaskForm): 'Title', validators=[DataRequired(), Length(1, 32)] ) + + def validate_files(form, field): + for file in field.data: + if not file.filename.lower().endswith(('.pdf', '.tif', '.tiff')): + raise ValidationError( + 'File does not have an approved extension: ' + '.pdf | .tif | .tiff' + )