Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16703 -- Raise an exception if the storage location of the Def…

…aultStorageFinder is empty.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16863 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c4cc8756437de2c9b508910ebe84f75a8d98d699 1 parent eb5df8e
Jannis Leidel authored September 21, 2011
15  django/contrib/staticfiles/finders.py
@@ -3,7 +3,7 @@
3 3
 from django.core.exceptions import ImproperlyConfigured
4 4
 from django.core.files.storage import default_storage, Storage, FileSystemStorage
5 5
 from django.utils.datastructures import SortedDict
6  
-from django.utils.functional import memoize, LazyObject
  6
+from django.utils.functional import empty, memoize, LazyObject
7 7
 from django.utils.importlib import import_module
8 8
 from django.utils._os import safe_join
9 9
 
@@ -133,7 +133,7 @@ def list(self, ignore_patterns):
133 133
         List all files in all app storages.
134 134
         """
135 135
         for storage in self.storages.itervalues():
136  
-            if storage.exists(''): # check if storage location exists
  136
+            if storage.exists(''):  # check if storage location exists
137 137
                 for path in utils.get_files(storage, ignore_patterns):
138 138
                     yield path, storage
139 139
 
@@ -210,12 +210,21 @@ def list(self, ignore_patterns):
210 210
         for path in utils.get_files(self.storage, ignore_patterns):
211 211
             yield path, self.storage
212 212
 
  213
+
213 214
 class DefaultStorageFinder(BaseStorageFinder):
214 215
     """
215 216
     A static files finder that uses the default storage backend.
216 217
     """
217 218
     storage = default_storage
218 219
 
  220
+    def __init__(self, *args, **kwargs):
  221
+        super(DefaultStorageFinder, self).__init__(*args, **kwargs)
  222
+        base_location = getattr(self.storage, 'base_location', empty)
  223
+        if not base_location:
  224
+            raise ImproperlyConfigured("The storage backend of the "
  225
+                                       "staticfiles finder %r doesn't have "
  226
+                                       "a valid location." % self.__class__)
  227
+
219 228
 
220 229
 def find(path, all=False):
221 230
     """
@@ -237,10 +246,12 @@ def find(path, all=False):
237 246
     # No match.
238 247
     return all and [] or None
239 248
 
  249
+
240 250
 def get_finders():
241 251
     for finder_path in settings.STATICFILES_FINDERS:
242 252
         yield get_finder(finder_path)
243 253
 
  254
+
244 255
 def _get_finder(import_path):
245 256
     """
246 257
     Imports the staticfiles finder class described by import_path, where
6  django/core/files/storage.py
@@ -12,7 +12,8 @@
12 12
 from django.utils.functional import LazyObject
13 13
 from django.utils.importlib import import_module
14 14
 from django.utils.text import get_valid_filename
15  
-from django.utils._os import safe_join
  15
+from django.utils._os import safe_join, abspathu
  16
+
16 17
 
17 18
 __all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
18 19
 
@@ -145,9 +146,10 @@ class FileSystemStorage(Storage):
145 146
     def __init__(self, location=None, base_url=None):
146 147
         if location is None:
147 148
             location = settings.MEDIA_ROOT
  149
+        self.base_location = location
  150
+        self.location = abspathu(self.base_location)
148 151
         if base_url is None:
149 152
             base_url = settings.MEDIA_URL
150  
-        self.location = os.path.abspath(location)
151 153
         self.base_url = base_url
152 154
 
153 155
     def _open(self, name, mode='rb'):
8  tests/regressiontests/file_storage/tests.py
@@ -96,6 +96,14 @@ def tearDown(self):
96 96
         shutil.rmtree(self.temp_dir)
97 97
         shutil.rmtree(self.temp_dir2)
98 98
 
  99
+    def test_emtpy_location(self):
  100
+        """
  101
+        Makes sure an exception is raised if the location is empty
  102
+        """
  103
+        storage = self.storage_class(location='')
  104
+        self.assertEqual(storage.base_location, '')
  105
+        self.assertEqual(storage.location, os.getcwd())
  106
+
99 107
     def test_file_access_options(self):
100 108
         """
101 109
         Standard file access options are available, and work as expected.
11  tests/regressiontests/staticfiles_tests/tests.py
@@ -496,6 +496,9 @@ class TestMiscFinder(TestCase):
496 496
     """
497 497
     A few misc finder tests.
498 498
     """
  499
+    def setUp(self):
  500
+        default_storage._wrapped = empty
  501
+
499 502
     def test_get_finder(self):
500 503
         self.assertTrue(isinstance(finders.get_finder(
501 504
             'django.contrib.staticfiles.finders.FileSystemFinder'),
@@ -509,13 +512,17 @@ def test_get_finder_bad_module(self):
509 512
         self.assertRaises(ImproperlyConfigured,
510 513
             finders.get_finder, 'foo.bar.FooBarFinder')
511 514
 
  515
+    @override_settings(STATICFILES_DIRS='a string')
512 516
     def test_non_tuple_raises_exception(self):
513 517
         """
514 518
         We can't determine if STATICFILES_DIRS is set correctly just by
515 519
         looking at the type, but we can determine if it's definitely wrong.
516 520
         """
517  
-        with self.settings(STATICFILES_DIRS='a string'):
518  
-            self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
  521
+        self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
  522
+
  523
+    @override_settings(MEDIA_ROOT='')
  524
+    def test_location_empty(self):
  525
+        self.assertRaises(ImproperlyConfigured, finders.DefaultStorageFinder)
519 526
 
520 527
 
521 528
 class TestTemplateTag(StaticFilesTestCase):

0 notes on commit c4cc875

Please sign in to comment.
Something went wrong with that request. Please try again.