From 8b615ae50a93b22453a5ad36a4782fd74d0b4d92 Mon Sep 17 00:00:00 2001 From: Edward Robinson Date: Wed, 21 Nov 2012 14:09:32 +0000 Subject: [PATCH] A fix for #2. Slightly different approach to @rdegges as static_folder is the main issue. Plus some tests. --- flask_s3.py | 5 +++-- tests/test_flask_static.py | 12 +++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/flask_s3.py b/flask_s3.py index 343fe68..b363594 100644 --- a/flask_s3.py +++ b/flask_s3.py @@ -42,14 +42,15 @@ def url_for(endpoint, **values): def _bp_static_url(blueprint): """ builds the absolute url path for a blueprint's static folder """ - return u'%s%s' % (blueprint.url_prefix or '', blueprint.static_url_path) + return u'%s%s' % (blueprint.url_prefix or '', blueprint.static_url_path or '') def _gather_files(app, hidden): """ Gets all files in static folders and returns in dict.""" dirs = [(unicode(app.static_folder), app.static_url_path)] if hasattr(app, 'blueprints'): + blueprints = app.blueprints.values() bp_details = lambda x: (x.static_folder, _bp_static_url(x)) - dirs.extend([bp_details(x) for x in app.blueprints.values()]) + dirs.extend([bp_details(x) for x in blueprints if x.static_folder]) valid_files = defaultdict(list) for static_folder, static_url_loc in dirs: diff --git a/tests/test_flask_static.py b/tests/test_flask_static.py index 12135ac..63d1c6f 100644 --- a/tests/test_flask_static.py +++ b/tests/test_flask_static.py @@ -109,15 +109,15 @@ class S3Tests(unittest.TestCase): def setUp(self): self.app = Mock(spec=Flask) - def test__bp_static_url(self): """ Tests test__bp_static_url """ bps = [Mock(static_url_path='/foo', url_prefix=None), - Mock(static_url_path='/b/bar', url_prefix='/pref')] - expected = [u'/foo', u'/pref/b/bar'] + Mock(static_url_path=None, url_prefix='/pref'), + Mock(static_url_path='/b/bar', url_prefix='/pref'), + Mock(static_url_path=None, url_prefix=None)] + expected = [u'/foo', u'/pref', u'/pref/b/bar', u''] self.assertEquals(expected, [flask_s3._bp_static_url(x) for x in bps]) - @patch('os.walk') @patch('os.path.isdir') def test__gather_files(self, path_mock, os_mock): @@ -129,7 +129,9 @@ class S3Tests(unittest.TestCase): url_prefix=None) bp_b = Mock(static_folder='/home/zoo', static_url_path='/b/bar', url_prefix=None) - self.app.blueprints = { 'a': bp_a, 'b': bp_b} + bp_c = Mock(static_folder=None) + + self.app.blueprints = { 'a': bp_a, 'b': bp_b, 'c': bp_c} dirs = { '/home': [('/home', None, ['.a'])], '/home/bar': [('/home/bar', None, ['b'])], '/home/zoo': [('/home/zoo', None, ['c']),