Skip to content

Commit

Permalink
Added NONE extensions configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Mar 28, 2017
1 parent 2ef111b commit 14fe798
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Current
- Added backend level configuration ``FS_{BACKEND_NAME}_{KEY}``
- Improved backend documentation
- Use setuptools entry points to register backends.
- Added `NONE` extensions specification

0.3.0 (2017-03-05)
------------------
Expand Down
14 changes: 13 additions & 1 deletion flask_fs/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__all__ = (
'TEXT', 'DOCUMENTS', 'IMAGES', 'AUDIO', 'DATA', 'SCRIPTS', 'ARCHIVES', 'EXECUTABLES',
'DEFAULTS', 'ALL', 'All', 'AllExcept',
'DEFAULTS', 'ALL', 'NONE', 'All', 'AllExcept', 'DisallowAll'
)

#: This just contains plain text files (.txt).
Expand Down Expand Up @@ -76,9 +76,21 @@ def __contains__(self, item):
return True


class DisallowAll(object):
'''
This type can be used to disallow all extensions.
There is a predefined instance named `NONE`.
'''
def __contains__(self, item):
return False


#: This "contains" all items. You can use it to allow all extensions to be uploaded.
ALL = All()

#: This "contains" no items. You can use it to force a whitelist by configuration.
NONE = DisallowAll()


class AllExcept(object):
'''
Expand Down
8 changes: 7 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from flask import url_for

from flask_fs import Storage, DEFAULTS
from flask_fs import Storage, DEFAULTS, NONE
from flask_fs.backends.local import LocalBackend


Expand Down Expand Up @@ -106,3 +106,9 @@ def test_configuration_cascading(app):
assert avatars.base_url == 'http://somewhere-else.net/avatars/'
assert images.base_url == 'http://somewhere.net/test/images/'
assert images.config.endpoint == 'http://localhost:9000'


def test_configurable_extensions(app):
files = Storage('files', NONE)
app.configure(files, FS_ALLOW=['txt'])
assert files.extension_allowed('txt')
7 changes: 7 additions & 0 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def test_lowercase_ext():
def test_all():
assert 'txt' in files.ALL
assert 'exe' in files.ALL
assert 'any' in files.ALL


def test_none():
assert 'txt' not in files.NONE
assert 'exe' not in files.NONE
assert 'any' not in files.NONE


def test_all_except():
Expand Down

0 comments on commit 14fe798

Please sign in to comment.