Skip to content

Commit

Permalink
Fix docstrings to match new FileStorage location
Browse files Browse the repository at this point in the history
When Werkzeug 1.0 was released, the import API changed.

The import for `FileStorage` was fixed, but the docstrings were
not updated.

Also, a test has been written to catch the case when not a proper
`FileStorage` was provided.

modified:   flask_uploads.py
modified:   tests.py
modified:   tox.ini
  • Loading branch information
jugmac00 committed Jun 30, 2020
1 parent dc3c086 commit 7754da0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
25 changes: 16 additions & 9 deletions flask_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,16 @@ def path(self, filename, folder=None):
return os.path.join(target_folder, filename)

def file_allowed(self, storage, basename):
"""
This tells whether a file is allowed. It should return `True` if the
given `werkzeug.FileStorage` object can be saved with the given
basename, and `False` if it can't. The default implementation just
checks the extension, so you can override this if you want.
"""This tells whether a file is allowed.
It should return `True` if the given
`werkzeug.datastructures.FileStorage` object can be saved with
the given basename, and `False` if it can't.
:param storage: The `werkzeug.FileStorage` to check.
The default implementation just checks the extension,
so you can override this if you want.
:param storage: The `werkzeug.datastructures.FileStorage` to check.
:param basename: The basename it will be saved under.
"""
return self.extension_allowed(extension(basename))
Expand All @@ -405,9 +408,13 @@ def get_basename(self, filename):
return lowercase_ext(secure_filename(filename))

def save(self, storage, folder=None, name=None):
"""
This saves a `werkzeug.FileStorage` into this upload set. If the
upload is not allowed, an `UploadNotAllowed` error will be raised.
"""This saves the `storage` into this upload set.
A `storage` is a `werkzeug.datastructures.FileStorage`.
If the upload is not allowed,
an `UploadNotAllowed` error will be raised.
Otherwise, the file will be saved and its name (including the folder)
will be returned.
Expand Down
13 changes: 13 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os.path

import pytest
from flask import Flask
from flask import url_for

Expand Down Expand Up @@ -246,6 +247,18 @@ def test_secured_filename(self):
assert res2 == 'myapp.wsgi'
assert tfs2.saved == '/uploads/myapp.wsgi'

def test_storage_is_not_a_werkzeug_datastructure(self):
"""UploadSet.save needs a valid FileStorage object.
When something different is passed in, a TypeError gets raised.
"""
uset = UploadSet('files', ALL)
uset._config = Config('/uploads')
non_storage = 'this is no werkzeug.datastructure.FileStorage'

with pytest.raises(TypeError):
uset.save(non_storage)


class TestConflictResolution(object):
def setup(self):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ commands =
check-python-versions {posargs}

[isort]
known_third_party = flask,setuptools,sphinx_rtd_theme,werkzeug
known_third_party = flask,pytest,setuptools,sphinx_rtd_theme,werkzeug
force_single_line = True

[flake8]
Expand Down

0 comments on commit 7754da0

Please sign in to comment.