File length validation for task order upload
This commit is contained in:
@@ -4,6 +4,7 @@ from wtforms.validators import Required, Optional
|
||||
from flask_wtf.file import FileAllowed
|
||||
|
||||
from .forms import BaseForm
|
||||
from atst.forms.validators import FileLength
|
||||
from atst.utils.localization import translate
|
||||
|
||||
|
||||
@@ -16,7 +17,8 @@ class TaskOrderForm(BaseForm):
|
||||
pdf = FileField(
|
||||
None,
|
||||
validators=[
|
||||
FileAllowed(["pdf"], translate("forms.task_order.file_format_not_allowed"))
|
||||
FileAllowed(["pdf"], translate("forms.task_order.file_format_not_allowed")),
|
||||
FileLength(),
|
||||
],
|
||||
render_kw={"accept": ".pdf,application/pdf"},
|
||||
)
|
||||
|
@@ -99,3 +99,17 @@ def RequiredIf(criteria_function, message=translate("forms.validators.is_require
|
||||
raise StopValidation()
|
||||
|
||||
return _required_if
|
||||
|
||||
|
||||
def FileLength(max_length=50000000, message=None):
|
||||
def _file_length(_form, field):
|
||||
if field.data is None:
|
||||
return True
|
||||
|
||||
content = field.data.read()
|
||||
if len(content) > max_length:
|
||||
raise ValidationError(message)
|
||||
else:
|
||||
field.data.seek(0)
|
||||
|
||||
return _file_length
|
||||
|
Reference in New Issue
Block a user