Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: file validators were editing the file fields content #578

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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