Author: jezdez
Date: 2011-09-21 08:58:32 -0700 (Wed, 21 Sep 2011)
New Revision: 16863

Modified:
   django/trunk/django/contrib/staticfiles/finders.py
   django/trunk/django/core/files/storage.py
   django/trunk/tests/regressiontests/file_storage/tests.py
   django/trunk/tests/regressiontests/staticfiles_tests/tests.py
Log:
Fixed #16703 -- Raise an exception if the storage location of the 
DefaultStorageFinder is empty.

Modified: django/trunk/django/contrib/staticfiles/finders.py
===================================================================
--- django/trunk/django/contrib/staticfiles/finders.py  2011-09-21 15:58:21 UTC 
(rev 16862)
+++ django/trunk/django/contrib/staticfiles/finders.py  2011-09-21 15:58:32 UTC 
(rev 16863)
@@ -3,7 +3,7 @@
 from django.core.exceptions import ImproperlyConfigured
 from django.core.files.storage import default_storage, Storage, 
FileSystemStorage
 from django.utils.datastructures import SortedDict
-from django.utils.functional import memoize, LazyObject
+from django.utils.functional import empty, memoize, LazyObject
 from django.utils.importlib import import_module
 from django.utils._os import safe_join
 
@@ -133,7 +133,7 @@
         List all files in all app storages.
         """
         for storage in self.storages.itervalues():
-            if storage.exists(''): # check if storage location exists
+            if storage.exists(''):  # check if storage location exists
                 for path in utils.get_files(storage, ignore_patterns):
                     yield path, storage
 
@@ -210,13 +210,22 @@
         for path in utils.get_files(self.storage, ignore_patterns):
             yield path, self.storage
 
+
 class DefaultStorageFinder(BaseStorageFinder):
     """
     A static files finder that uses the default storage backend.
     """
     storage = default_storage
 
+    def __init__(self, *args, **kwargs):
+        super(DefaultStorageFinder, self).__init__(*args, **kwargs)
+        base_location = getattr(self.storage, 'base_location', empty)
+        if not base_location:
+            raise ImproperlyConfigured("The storage backend of the "
+                                       "staticfiles finder %r doesn't have "
+                                       "a valid location." % self.__class__)
 
+
 def find(path, all=False):
     """
     Find a static file with the given path using all enabled finders.
@@ -237,10 +246,12 @@
     # No match.
     return all and [] or None
 
+
 def get_finders():
     for finder_path in settings.STATICFILES_FINDERS:
         yield get_finder(finder_path)
 
+
 def _get_finder(import_path):
     """
     Imports the staticfiles finder class described by import_path, where

Modified: django/trunk/django/core/files/storage.py
===================================================================
--- django/trunk/django/core/files/storage.py   2011-09-21 15:58:21 UTC (rev 
16862)
+++ django/trunk/django/core/files/storage.py   2011-09-21 15:58:32 UTC (rev 
16863)
@@ -12,8 +12,9 @@
 from django.utils.functional import LazyObject
 from django.utils.importlib import import_module
 from django.utils.text import get_valid_filename
-from django.utils._os import safe_join
+from django.utils._os import safe_join, abspathu
 
+
 __all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
 
 class Storage(object):
@@ -145,9 +146,10 @@
     def __init__(self, location=None, base_url=None):
         if location is None:
             location = settings.MEDIA_ROOT
+        self.base_location = location
+        self.location = abspathu(self.base_location)
         if base_url is None:
             base_url = settings.MEDIA_URL
-        self.location = os.path.abspath(location)
         self.base_url = base_url
 
     def _open(self, name, mode='rb'):

Modified: django/trunk/tests/regressiontests/file_storage/tests.py
===================================================================
--- django/trunk/tests/regressiontests/file_storage/tests.py    2011-09-21 
15:58:21 UTC (rev 16862)
+++ django/trunk/tests/regressiontests/file_storage/tests.py    2011-09-21 
15:58:32 UTC (rev 16863)
@@ -96,6 +96,14 @@
         shutil.rmtree(self.temp_dir)
         shutil.rmtree(self.temp_dir2)
 
+    def test_emtpy_location(self):
+        """
+        Makes sure an exception is raised if the location is empty
+        """
+        storage = self.storage_class(location='')
+        self.assertEqual(storage.base_location, '')
+        self.assertEqual(storage.location, os.getcwd())
+
     def test_file_access_options(self):
         """
         Standard file access options are available, and work as expected.

Modified: django/trunk/tests/regressiontests/staticfiles_tests/tests.py
===================================================================
--- django/trunk/tests/regressiontests/staticfiles_tests/tests.py       
2011-09-21 15:58:21 UTC (rev 16862)
+++ django/trunk/tests/regressiontests/staticfiles_tests/tests.py       
2011-09-21 15:58:32 UTC (rev 16863)
@@ -496,6 +496,9 @@
     """
     A few misc finder tests.
     """
+    def setUp(self):
+        default_storage._wrapped = empty
+
     def test_get_finder(self):
         self.assertTrue(isinstance(finders.get_finder(
             'django.contrib.staticfiles.finders.FileSystemFinder'),
@@ -509,15 +512,19 @@
         self.assertRaises(ImproperlyConfigured,
             finders.get_finder, 'foo.bar.FooBarFinder')
 
+    @override_settings(STATICFILES_DIRS='a string')
     def test_non_tuple_raises_exception(self):
         """
         We can't determine if STATICFILES_DIRS is set correctly just by
         looking at the type, but we can determine if it's definitely wrong.
         """
-        with self.settings(STATICFILES_DIRS='a string'):
-            self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
+        self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
 
+    @override_settings(MEDIA_ROOT='')
+    def test_location_empty(self):
+        self.assertRaises(ImproperlyConfigured, finders.DefaultStorageFinder)
 
+
 class TestTemplateTag(StaticFilesTestCase):
 
     def test_template_tag(self):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to