Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15094 - Added check for forgetting trailing comma in STATICFIL…

…ES_DIRS tuple. Also reorganized staticfiles settings-checks for better consistency.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15386 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7aad3d3fa8a8db8eb8ea9e64134d60b2400029f0 1 parent 74d485c
Carl Meyer authored February 01, 2011
8  django/contrib/staticfiles/finders.py
@@ -46,11 +46,19 @@ def __init__(self, apps=None, *args, **kwargs):
46 46
         self.storages = SortedDict()
47 47
         # Set of locations with static files
48 48
         self.locations = set()
  49
+        if not isinstance(settings.STATICFILES_DIRS, (list, tuple)):
  50
+            raise ImproperlyConfigured(
  51
+                "Your STATICFILES_DIRS setting is not a tuple or list; "
  52
+                "perhaps you forgot a trailing comma?")
49 53
         for root in settings.STATICFILES_DIRS:
50 54
             if isinstance(root, (list, tuple)):
51 55
                 prefix, root = root
52 56
             else:
53 57
                 prefix = ''
  58
+            if os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
  59
+                raise ImproperlyConfigured(
  60
+                    "The STATICFILES_DIRS setting should "
  61
+                    "not contain the STATIC_ROOT setting")
54 62
             self.locations.add((prefix, root))
55 63
         # Don't initialize multiple storages for the same location
56 64
         for prefix, root in self.locations:
7  django/contrib/staticfiles/handlers.py
@@ -26,12 +26,7 @@ def get_base_dir(self):
26 26
         return settings.STATIC_ROOT
27 27
 
28 28
     def get_base_url(self):
29  
-        if not settings.STATIC_URL:
30  
-            raise ImproperlyConfigured("You're using the staticfiles app "
31  
-                "without having set the STATIC_URL setting. Set it to "
32  
-                "URL that handles the files served from STATIC_ROOT.")
33  
-        if settings.DEBUG:
34  
-            utils.check_settings()
  29
+        utils.check_settings()
35 30
         return settings.STATIC_URL
36 31
 
37 32
     def _should_handle(self, path):
5  django/contrib/staticfiles/storage.py
@@ -10,7 +10,7 @@
10 10
 class StaticFilesStorage(FileSystemStorage):
11 11
     """
12 12
     Standard file system storage for static files.
13  
-    
  13
+
14 14
     The defaults for ``location`` and ``base_url`` are
15 15
     ``STATIC_ROOT`` and ``STATIC_URL``.
16 16
     """
@@ -28,8 +28,7 @@ def __init__(self, location=None, base_url=None, *args, **kwargs):
28 28
             raise ImproperlyConfigured("You're using the staticfiles app "
29 29
                 "without having set the STATIC_URL setting. Set it to "
30 30
                 "URL that handles the files served from STATIC_ROOT.")
31  
-        if settings.DEBUG:
32  
-            utils.check_settings()
  31
+        utils.check_settings()
33 32
         super(StaticFilesStorage, self).__init__(location, base_url, *args, **kwargs)
34 33
 
35 34
 
15  django/contrib/staticfiles/utils.py
@@ -35,9 +35,13 @@ def get_files(storage, ignore_patterns=[], location=''):
35 35
 
36 36
 def check_settings():
37 37
     """
38  
-    Checks if the MEDIA_(ROOT|URL) and STATIC_(ROOT|URL)
39  
-    settings have the same value.
  38
+    Checks if the staticfiles settings have sane values.
  39
+
40 40
     """
  41
+    if not settings.STATIC_URL:
  42
+        raise ImproperlyConfigured(
  43
+            "You're using the staticfiles app "
  44
+            "without having set the required STATIC_URL setting.")
41 45
     if settings.MEDIA_URL == settings.STATIC_URL:
42 46
         raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL "
43 47
                                    "settings must have different values")
@@ -45,10 +49,3 @@ def check_settings():
45 49
             (settings.MEDIA_ROOT == settings.STATIC_ROOT)):
46 50
         raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT "
47 51
                                    "settings must have different values")
48  
-    for path in settings.STATICFILES_DIRS:
49  
-        # in case the item contains a prefix
50  
-        if isinstance(path, (list, tuple)):
51  
-            path = path[1]
52  
-        if os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(path):
53  
-            raise ImproperlyConfigured("The STATICFILES_DIRS setting should "
54  
-                                       "not contain the STATIC_ROOT setting")
22  tests/regressiontests/staticfiles_tests/tests.py
@@ -10,8 +10,6 @@
10 10
 from django.core.exceptions import ImproperlyConfigured
11 11
 from django.core.files.storage import default_storage
12 12
 from django.core.management import call_command
13  
-from django.db.models.loading import load_app
14  
-from django.template import Template, Context
15 13
 from django.test import TestCase
16 14
 from django.utils._os import rmtree_errorhandler
17 15
 
@@ -383,7 +381,27 @@ def test_get_finder(self):
383 381
         self.assertTrue(isinstance(finders.get_finder(
384 382
             'django.contrib.staticfiles.finders.FileSystemFinder'),
385 383
             finders.FileSystemFinder))
  384
+
  385
+    def test_get_finder_bad_classname(self):
386 386
         self.assertRaises(ImproperlyConfigured,
387 387
             finders.get_finder, 'django.contrib.staticfiles.finders.FooBarFinder')
  388
+
  389
+    def test_get_finder_bad_module(self):
388 390
         self.assertRaises(ImproperlyConfigured,
389 391
             finders.get_finder, 'foo.bar.FooBarFinder')
  392
+
  393
+
  394
+class TestStaticfilesDirsType(TestCase):
  395
+    """
  396
+    We can't determine if STATICFILES_DIRS is set correctly just by looking at
  397
+    the type, but we can determine if it's definitely wrong.
  398
+    """
  399
+    def setUp(self):
  400
+        self.old_settings_dir = settings.STATICFILES_DIRS
  401
+        settings.STATICFILES_DIRS = 'a string'
  402
+
  403
+    def tearDown(self):
  404
+        settings.STATICFILES_DIRS = self.old_settings_dir
  405
+
  406
+    def test_non_tuple_raises_exception(self):
  407
+        self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)

0 notes on commit 7aad3d3

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