Skip to content

Commit

Permalink
Merge pull request #578 from azmeuk/pr-558-fix
Browse files Browse the repository at this point in the history
 fix: file validators were editing the file fields content
  • Loading branch information
azmeuk authored Oct 2, 2023
2 parents 50921f0 + 6c8fc9d commit 6804696
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 8 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changes
=======

Version 1.2.1
-------------

Unreleased

- Fix a bug introduced with :pr:`556` where file validators were editing
the file fields content. :pr:`578`

Version 1.2.0
-------------

Expand Down
22 changes: 9 additions & 13 deletions src/flask_wtf/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ class FileRequired(DataRequired):
"""

def __call__(self, form, field):
if not isinstance(field.data, list):
field.data = [field.data]
field_data = [field.data] if not isinstance(field.data, list) else field.data
if not (
all(isinstance(x, FileStorage) and x for x in field.data) and field.data
all(isinstance(x, FileStorage) and x for x in field_data) and field_data
):
raise StopValidation(
self.message or field.gettext("This field is required.")
Expand All @@ -76,14 +75,13 @@ def __init__(self, upload_set, message=None):
self.message = message

def __call__(self, form, field):
if not isinstance(field.data, list):
field.data = [field.data]
field_data = [field.data] if not isinstance(field.data, list) else field.data
if not (
all(isinstance(x, FileStorage) and x for x in field.data) and field.data
all(isinstance(x, FileStorage) and x for x in field_data) and field_data
):
return

filenames = [f.filename.lower() for f in field.data]
filenames = [f.filename.lower() for f in field_data]

for filename in filenames:
if isinstance(self.upload_set, abc.Iterable):
Expand All @@ -97,7 +95,7 @@ def __call__(self, form, field):
).format(extensions=", ".join(self.upload_set))
)

if not self.upload_set.file_allowed(field.data, filename):
if not self.upload_set.file_allowed(field_data, filename):
raise StopValidation(
self.message
or field.gettext("File does not have an approved extension.")
Expand All @@ -124,16 +122,14 @@ def __init__(self, max_size, min_size=0, message=None):
self.message = message

def __call__(self, form, field):
if not isinstance(field.data, list):
field.data = [field.data]
field_data = [field.data] if not isinstance(field.data, list) else field.data
if not (
all(isinstance(x, FileStorage) and x for x in field.data) and field.data
all(isinstance(x, FileStorage) and x for x in field_data) and field_data
):
return

for f in field.data:
for f in field_data:
file_size = len(f.read())
print(f, file_size, self.max_size, self.min_size)
f.seek(0) # reset cursor position to beginning of file

if (file_size < self.min_size) or (file_size > self.max_size):
Expand Down

0 comments on commit 6804696

Please sign in to comment.