summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorLing-Xiao Yang <ling-xiao.yang@savoirfairelinux.com>2017-01-02 17:35:43 -0500
committerTim Graham <timograham@gmail.com>2017-02-01 09:48:24 -0500
commit0ec4dc91e0e7befdd06aa0613b5d0fbe3c785ee7 (patch)
treea90c017fa9555b3cbcb7e05459a8ee6dbe66a6cd /django
parentac5f886c5610a6bca26dab10170b445d1e9df450 (diff)
Fixed #27661 -- Moved FileSystemFinder's ImproperlyConfigured exceptions to system checks.
Thanks Simon Charette, Mariusz Felisiak, Tim Graham, and Adam Johnson for review.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/staticfiles/apps.py5
-rw-r--r--django/contrib/staticfiles/checks.py14
-rw-r--r--django/contrib/staticfiles/finders.py33
3 files changed, 44 insertions, 8 deletions
diff --git a/django/contrib/staticfiles/apps.py b/django/contrib/staticfiles/apps.py
index 0911c0113d..65865da0d2 100644
--- a/django/contrib/staticfiles/apps.py
+++ b/django/contrib/staticfiles/apps.py
@@ -1,4 +1,6 @@
from django.apps import AppConfig
+from django.contrib.staticfiles.checks import check_finders
+from django.core import checks
from django.utils.translation import ugettext_lazy as _
@@ -6,3 +8,6 @@ class StaticFilesConfig(AppConfig):
name = 'django.contrib.staticfiles'
verbose_name = _("Static Files")
ignore_patterns = ['CVS', '.*', '*~']
+
+ def ready(self):
+ checks.register(check_finders, 'staticfiles')
diff --git a/django/contrib/staticfiles/checks.py b/django/contrib/staticfiles/checks.py
new file mode 100644
index 0000000000..fb57bf726d
--- /dev/null
+++ b/django/contrib/staticfiles/checks.py
@@ -0,0 +1,14 @@
+from django.contrib.staticfiles.finders import get_finders
+
+
+def check_finders(app_configs=None, **kwargs):
+ """Check all registered staticfiles finders."""
+ errors = []
+ for finder in get_finders():
+ try:
+ finder_errors = finder.check()
+ except NotImplementedError:
+ pass
+ else:
+ errors.extend(finder_errors)
+ return errors
diff --git a/django/contrib/staticfiles/finders.py b/django/contrib/staticfiles/finders.py
index fcb31c8547..76029b33b6 100644
--- a/django/contrib/staticfiles/finders.py
+++ b/django/contrib/staticfiles/finders.py
@@ -5,6 +5,7 @@ from collections import OrderedDict
from django.apps import apps
from django.conf import settings
from django.contrib.staticfiles import utils
+from django.core.checks import Error
from django.core.exceptions import ImproperlyConfigured
from django.core.files.storage import (
FileSystemStorage, Storage, default_storage,
@@ -21,6 +22,11 @@ class BaseFinder:
"""
A base file finder to be used for custom staticfiles finder classes.
"""
+ def check(self, **kwargs):
+ raise NotImplementedError(
+ 'subclasses may provide a check() method to verify the finder is '
+ 'configured correctly.'
+ )
def find(self, path, all=False):
"""
@@ -52,19 +58,11 @@ class FileSystemFinder(BaseFinder):
self.locations = []
# Maps dir paths to an appropriate storage instance
self.storages = OrderedDict()
- if not isinstance(settings.STATICFILES_DIRS, (list, tuple)):
- raise ImproperlyConfigured(
- "Your STATICFILES_DIRS setting is not a tuple or list; "
- "perhaps you forgot a trailing comma?")
for root in settings.STATICFILES_DIRS:
if isinstance(root, (list, tuple)):
prefix, root = root
else:
prefix = ''
- if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
- raise ImproperlyConfigured(
- "The STATICFILES_DIRS setting should "
- "not contain the STATIC_ROOT setting")
if (prefix, root) not in self.locations:
self.locations.append((prefix, root))
for prefix, root in self.locations:
@@ -73,6 +71,25 @@ class FileSystemFinder(BaseFinder):
self.storages[root] = filesystem_storage
super().__init__(*args, **kwargs)
+ def check(self, **kwargs):
+ errors = []
+ if not isinstance(settings.STATICFILES_DIRS, (list, tuple)):
+ errors.append(Error(
+ 'The STATICFILES_DIRS setting is not a tuple or list.',
+ hint='Perhaps you forgot a trailing comma?',
+ id='staticfiles.E001',
+ ))
+ for root in settings.STATICFILES_DIRS:
+ if isinstance(root, (list, tuple)):
+ _, root = root
+ if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
+ errors.append(Error(
+ 'The STATICFILES_DIRS setting should not contain the '
+ 'STATIC_ROOT setting.',
+ id='staticfiles.E002',
+ ))
+ return errors
+
def find(self, path, all=False):
"""
Looks for files in the extra locations