2017-01-19 04:30:21 +08:00
|
|
|
import functools
|
2010-10-20 09:33:24 +08:00
|
|
|
import os
|
2013-08-03 13:41:15 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.staticfiles import utils
|
2017-01-03 06:35:43 +08:00
|
|
|
from django.core.checks import Error
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.files.storage import (
|
|
|
|
FileSystemStorage, Storage, default_storage,
|
|
|
|
)
|
2011-01-02 09:31:55 +08:00
|
|
|
from django.utils._os import safe_join
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.functional import LazyObject, empty
|
|
|
|
from django.utils.module_loading import import_string
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-11-16 22:55:50 +08:00
|
|
|
# To keep track on which directories the finder has searched the static files.
|
|
|
|
searched_locations = []
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class BaseFinder:
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
|
|
|
A base file finder to be used for custom staticfiles finder classes.
|
|
|
|
"""
|
2017-01-03 06:35:43 +08:00
|
|
|
def check(self, **kwargs):
|
|
|
|
raise NotImplementedError(
|
|
|
|
'subclasses may provide a check() method to verify the finder is '
|
|
|
|
'configured correctly.'
|
|
|
|
)
|
2013-11-16 22:55:50 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
def find(self, path, all=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Given a relative file path, find an absolute file path.
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2017-01-25 04:31:57 +08:00
|
|
|
If the ``all`` parameter is False (default) return only the first found
|
|
|
|
file path; if True, return a list of all found files paths.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of BaseFinder must provide a find() method')
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
def list(self, ignore_patterns):
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Given an optional list of paths to ignore, return a two item iterable
|
|
|
|
consisting of the relative path and storage instance.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of BaseFinder must provide a list() method')
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FileSystemFinder(BaseFinder):
|
|
|
|
"""
|
|
|
|
A static files finder that uses the ``STATICFILES_DIRS`` setting
|
|
|
|
to locate files.
|
|
|
|
"""
|
2013-12-24 19:25:17 +08:00
|
|
|
def __init__(self, app_names=None, *args, **kwargs):
|
2011-02-02 04:10:29 +08:00
|
|
|
# List of locations with static files
|
|
|
|
self.locations = []
|
2010-10-21 11:16:58 +08:00
|
|
|
# Maps dir paths to an appropriate storage instance
|
2019-02-05 19:22:08 +08:00
|
|
|
self.storages = {}
|
2010-10-20 09:33:24 +08:00
|
|
|
for root in settings.STATICFILES_DIRS:
|
|
|
|
if isinstance(root, (list, tuple)):
|
|
|
|
prefix, root = root
|
|
|
|
else:
|
|
|
|
prefix = ''
|
2011-02-02 04:10:29 +08:00
|
|
|
if (prefix, root) not in self.locations:
|
|
|
|
self.locations.append((prefix, root))
|
2010-10-20 09:33:24 +08:00
|
|
|
for prefix, root in self.locations:
|
2011-01-16 07:38:00 +08:00
|
|
|
filesystem_storage = FileSystemStorage(location=root)
|
|
|
|
filesystem_storage.prefix = prefix
|
|
|
|
self.storages[root] = filesystem_storage
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2017-01-03 06:35:43 +08:00
|
|
|
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)):
|
2018-09-28 07:49:37 +08:00
|
|
|
prefix, root = root
|
|
|
|
if prefix.endswith('/'):
|
|
|
|
errors.append(Error(
|
|
|
|
'The prefix %r in the STATICFILES_DIRS setting must '
|
|
|
|
'not end with a slash.' % prefix,
|
|
|
|
id='staticfiles.E003',
|
|
|
|
))
|
2017-01-03 06:35:43 +08:00
|
|
|
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
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
def find(self, path, all=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Look for files in the extra locations as defined in STATICFILES_DIRS.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
|
|
|
matches = []
|
|
|
|
for prefix, root in self.locations:
|
2013-11-16 22:55:50 +08:00
|
|
|
if root not in searched_locations:
|
|
|
|
searched_locations.append(root)
|
2010-10-20 09:33:24 +08:00
|
|
|
matched_path = self.find_location(root, path, prefix)
|
|
|
|
if matched_path:
|
|
|
|
if not all:
|
|
|
|
return matched_path
|
|
|
|
matches.append(matched_path)
|
|
|
|
return matches
|
|
|
|
|
|
|
|
def find_location(self, root, path, prefix=None):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Find a requested static file in a location and return the found
|
2010-10-20 09:33:24 +08:00
|
|
|
absolute path (or ``None`` if no match).
|
|
|
|
"""
|
|
|
|
if prefix:
|
2010-12-31 22:22:32 +08:00
|
|
|
prefix = '%s%s' % (prefix, os.sep)
|
2010-10-20 09:33:24 +08:00
|
|
|
if not path.startswith(prefix):
|
|
|
|
return None
|
|
|
|
path = path[len(prefix):]
|
2011-01-02 09:31:55 +08:00
|
|
|
path = safe_join(root, path)
|
2010-10-20 09:33:24 +08:00
|
|
|
if os.path.exists(path):
|
|
|
|
return path
|
|
|
|
|
|
|
|
def list(self, ignore_patterns):
|
|
|
|
"""
|
|
|
|
List all files in all locations.
|
|
|
|
"""
|
|
|
|
for prefix, root in self.locations:
|
|
|
|
storage = self.storages[root]
|
|
|
|
for path in utils.get_files(storage, ignore_patterns):
|
2011-01-16 07:38:00 +08:00
|
|
|
yield path, storage
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AppDirectoriesFinder(BaseFinder):
|
|
|
|
"""
|
2011-01-05 20:41:40 +08:00
|
|
|
A static files finder that looks in the directory of each app as
|
2014-01-25 06:32:03 +08:00
|
|
|
specified in the source_dir attribute.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2014-01-25 06:32:03 +08:00
|
|
|
storage_class = FileSystemStorage
|
|
|
|
source_dir = 'static'
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
def __init__(self, app_names=None, *args, **kwargs):
|
2011-01-16 07:38:00 +08:00
|
|
|
# The list of apps that are handled
|
|
|
|
self.apps = []
|
2013-12-19 22:57:23 +08:00
|
|
|
# Mapping of app names to storage instances
|
2019-02-05 19:22:08 +08:00
|
|
|
self.storages = {}
|
2014-01-25 06:32:03 +08:00
|
|
|
app_configs = apps.get_app_configs()
|
|
|
|
if app_names:
|
|
|
|
app_names = set(app_names)
|
|
|
|
app_configs = [ac for ac in app_configs if ac.name in app_names]
|
|
|
|
for app_config in app_configs:
|
|
|
|
app_storage = self.storage_class(
|
|
|
|
os.path.join(app_config.path, self.source_dir))
|
2011-01-16 07:38:00 +08:00
|
|
|
if os.path.isdir(app_storage.location):
|
2014-01-25 06:32:03 +08:00
|
|
|
self.storages[app_config.name] = app_storage
|
|
|
|
if app_config.name not in self.apps:
|
|
|
|
self.apps.append(app_config.name)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def list(self, ignore_patterns):
|
|
|
|
"""
|
|
|
|
List all files in all app storages.
|
|
|
|
"""
|
2017-01-07 19:11:46 +08:00
|
|
|
for storage in self.storages.values():
|
2011-09-21 23:58:32 +08:00
|
|
|
if storage.exists(''): # check if storage location exists
|
2010-10-20 09:33:24 +08:00
|
|
|
for path in utils.get_files(storage, ignore_patterns):
|
2011-01-16 07:38:00 +08:00
|
|
|
yield path, storage
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def find(self, path, all=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Look for files in the app directories.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
|
|
|
matches = []
|
|
|
|
for app in self.apps:
|
2013-11-16 22:55:50 +08:00
|
|
|
app_location = self.storages[app].location
|
|
|
|
if app_location not in searched_locations:
|
|
|
|
searched_locations.append(app_location)
|
2011-01-16 07:38:00 +08:00
|
|
|
match = self.find_in_app(app, path)
|
|
|
|
if match:
|
2010-10-20 09:33:24 +08:00
|
|
|
if not all:
|
2011-01-16 07:38:00 +08:00
|
|
|
return match
|
|
|
|
matches.append(match)
|
2010-10-20 09:33:24 +08:00
|
|
|
return matches
|
|
|
|
|
|
|
|
def find_in_app(self, app, path):
|
|
|
|
"""
|
2011-01-05 20:41:40 +08:00
|
|
|
Find a requested static file in an app's static locations.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2015-05-14 02:51:18 +08:00
|
|
|
storage = self.storages.get(app)
|
2011-01-16 07:38:00 +08:00
|
|
|
if storage:
|
|
|
|
# only try to find a file if the source dir actually exists
|
|
|
|
if storage.exists(path):
|
|
|
|
matched_path = storage.path(path)
|
|
|
|
if matched_path:
|
|
|
|
return matched_path
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseStorageFinder(BaseFinder):
|
|
|
|
"""
|
|
|
|
A base static files finder to be used to extended
|
|
|
|
with an own storage class.
|
|
|
|
"""
|
|
|
|
storage = None
|
|
|
|
|
|
|
|
def __init__(self, storage=None, *args, **kwargs):
|
|
|
|
if storage is not None:
|
|
|
|
self.storage = storage
|
|
|
|
if self.storage is None:
|
|
|
|
raise ImproperlyConfigured("The staticfiles storage finder %r "
|
|
|
|
"doesn't have a storage class "
|
|
|
|
"assigned." % self.__class__)
|
2017-11-07 11:11:39 +08:00
|
|
|
# Make sure we have a storage instance here.
|
2010-10-20 09:33:24 +08:00
|
|
|
if not isinstance(self.storage, (Storage, LazyObject)):
|
|
|
|
self.storage = self.storage()
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def find(self, path, all=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Look for files in the default file storage, if it's local.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.storage.path('')
|
|
|
|
except NotImplementedError:
|
|
|
|
pass
|
|
|
|
else:
|
2013-11-16 22:55:50 +08:00
|
|
|
if self.storage.location not in searched_locations:
|
|
|
|
searched_locations.append(self.storage.location)
|
2010-10-20 09:33:24 +08:00
|
|
|
if self.storage.exists(path):
|
|
|
|
match = self.storage.path(path)
|
|
|
|
if all:
|
|
|
|
match = [match]
|
|
|
|
return match
|
|
|
|
return []
|
|
|
|
|
|
|
|
def list(self, ignore_patterns):
|
|
|
|
"""
|
|
|
|
List all files of the storage.
|
|
|
|
"""
|
|
|
|
for path in utils.get_files(self.storage, ignore_patterns):
|
2011-01-16 07:38:00 +08:00
|
|
|
yield path, self.storage
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-09-21 23:58:32 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
class DefaultStorageFinder(BaseStorageFinder):
|
|
|
|
"""
|
|
|
|
A static files finder that uses the default storage backend.
|
|
|
|
"""
|
|
|
|
storage = default_storage
|
|
|
|
|
2011-09-21 23:58:32 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2011-09-21 23:58:32 +08:00
|
|
|
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__)
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def find(path, all=False):
|
|
|
|
"""
|
2011-01-05 20:41:40 +08:00
|
|
|
Find a static file with the given path using all enabled finders.
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
If ``all`` is ``False`` (default), return the first matching
|
2011-01-05 20:41:40 +08:00
|
|
|
absolute path (or ``None`` if no match). Otherwise return a list.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2013-11-16 22:55:50 +08:00
|
|
|
searched_locations[:] = []
|
2010-10-20 09:33:24 +08:00
|
|
|
matches = []
|
|
|
|
for finder in get_finders():
|
|
|
|
result = finder.find(path, all=all)
|
|
|
|
if not all and result:
|
|
|
|
return result
|
|
|
|
if not isinstance(result, (list, tuple)):
|
|
|
|
result = [result]
|
|
|
|
matches.extend(result)
|
|
|
|
if matches:
|
|
|
|
return matches
|
|
|
|
# No match.
|
2013-05-27 10:47:50 +08:00
|
|
|
return [] if all else None
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-09-21 23:58:32 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
def get_finders():
|
|
|
|
for finder_path in settings.STATICFILES_FINDERS:
|
|
|
|
yield get_finder(finder_path)
|
|
|
|
|
2011-09-21 23:58:32 +08:00
|
|
|
|
2017-01-19 04:30:21 +08:00
|
|
|
@functools.lru_cache(maxsize=None)
|
2013-11-02 04:15:41 +08:00
|
|
|
def get_finder(import_path):
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Import the staticfiles finder class described by import_path, where
|
2010-10-20 09:33:24 +08:00
|
|
|
import_path is the full Python path to the class.
|
|
|
|
"""
|
2014-01-21 04:15:14 +08:00
|
|
|
Finder = import_string(import_path)
|
2010-10-20 09:33:24 +08:00
|
|
|
if not issubclass(Finder, BaseFinder):
|
|
|
|
raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' %
|
|
|
|
(Finder, BaseFinder))
|
|
|
|
return Finder()
|