Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

finders.py 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import functools
  2. import os
  3. from collections import OrderedDict
  4. from django.apps import apps
  5. from django.conf import settings
  6. from django.contrib.staticfiles import utils
  7. from django.core.checks import Error
  8. from django.core.exceptions import ImproperlyConfigured
  9. from django.core.files.storage import (
  10. FileSystemStorage, Storage, default_storage,
  11. )
  12. from django.utils._os import safe_join
  13. from django.utils.functional import LazyObject, empty
  14. from django.utils.module_loading import import_string
  15. # To keep track on which directories the finder has searched the static files.
  16. searched_locations = []
  17. class BaseFinder:
  18. """
  19. A base file finder to be used for custom staticfiles finder classes.
  20. """
  21. def check(self, **kwargs):
  22. raise NotImplementedError(
  23. 'subclasses may provide a check() method to verify the finder is '
  24. 'configured correctly.'
  25. )
  26. def find(self, path, all=False):
  27. """
  28. Given a relative file path, find an absolute file path.
  29. If the ``all`` parameter is False (default) return only the first found
  30. file path; if True, return a list of all found files paths.
  31. """
  32. raise NotImplementedError('subclasses of BaseFinder must provide a find() method')
  33. def list(self, ignore_patterns):
  34. """
  35. Given an optional list of paths to ignore, return a two item iterable
  36. consisting of the relative path and storage instance.
  37. """
  38. raise NotImplementedError('subclasses of BaseFinder must provide a list() method')
  39. class FileSystemFinder(BaseFinder):
  40. """
  41. A static files finder that uses the ``STATICFILES_DIRS`` setting
  42. to locate files.
  43. """
  44. def __init__(self, app_names=None, *args, **kwargs):
  45. # List of locations with static files
  46. self.locations = []
  47. # Maps dir paths to an appropriate storage instance
  48. self.storages = OrderedDict()
  49. for root in settings.STATICFILES_DIRS:
  50. if isinstance(root, (list, tuple)):
  51. prefix, root = root
  52. else:
  53. prefix = ''
  54. if (prefix, root) not in self.locations:
  55. self.locations.append((prefix, root))
  56. for prefix, root in self.locations:
  57. filesystem_storage = FileSystemStorage(location=root)
  58. filesystem_storage.prefix = prefix
  59. self.storages[root] = filesystem_storage
  60. super().__init__(*args, **kwargs)
  61. def check(self, **kwargs):
  62. errors = []
  63. if not isinstance(settings.STATICFILES_DIRS, (list, tuple)):
  64. errors.append(Error(
  65. 'The STATICFILES_DIRS setting is not a tuple or list.',
  66. hint='Perhaps you forgot a trailing comma?',
  67. id='staticfiles.E001',
  68. ))
  69. for root in settings.STATICFILES_DIRS:
  70. if isinstance(root, (list, tuple)):
  71. _, root = root
  72. if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
  73. errors.append(Error(
  74. 'The STATICFILES_DIRS setting should not contain the '
  75. 'STATIC_ROOT setting.',
  76. id='staticfiles.E002',
  77. ))
  78. return errors
  79. def find(self, path, all=False):
  80. """
  81. Look for files in the extra locations as defined in STATICFILES_DIRS.
  82. """
  83. matches = []
  84. for prefix, root in self.locations:
  85. if root not in searched_locations:
  86. searched_locations.append(root)
  87. matched_path = self.find_location(root, path, prefix)
  88. if matched_path:
  89. if not all:
  90. return matched_path
  91. matches.append(matched_path)
  92. return matches
  93. def find_location(self, root, path, prefix=None):
  94. """
  95. Find a requested static file in a location and return the found
  96. absolute path (or ``None`` if no match).
  97. """
  98. if prefix:
  99. prefix = '%s%s' % (prefix, os.sep)
  100. if not path.startswith(prefix):
  101. return None
  102. path = path[len(prefix):]
  103. path = safe_join(root, path)
  104. if os.path.exists(path):
  105. return path
  106. def list(self, ignore_patterns):
  107. """
  108. List all files in all locations.
  109. """
  110. for prefix, root in self.locations:
  111. storage = self.storages[root]
  112. for path in utils.get_files(storage, ignore_patterns):
  113. yield path, storage
  114. class AppDirectoriesFinder(BaseFinder):
  115. """
  116. A static files finder that looks in the directory of each app as
  117. specified in the source_dir attribute.
  118. """
  119. storage_class = FileSystemStorage
  120. source_dir = 'static'
  121. def __init__(self, app_names=None, *args, **kwargs):
  122. # The list of apps that are handled
  123. self.apps = []
  124. # Mapping of app names to storage instances
  125. self.storages = OrderedDict()
  126. app_configs = apps.get_app_configs()
  127. if app_names:
  128. app_names = set(app_names)
  129. app_configs = [ac for ac in app_configs if ac.name in app_names]
  130. for app_config in app_configs:
  131. app_storage = self.storage_class(
  132. os.path.join(app_config.path, self.source_dir))
  133. if os.path.isdir(app_storage.location):
  134. self.storages[app_config.name] = app_storage
  135. if app_config.name not in self.apps:
  136. self.apps.append(app_config.name)
  137. super().__init__(*args, **kwargs)
  138. def list(self, ignore_patterns):
  139. """
  140. List all files in all app storages.
  141. """
  142. for storage in self.storages.values():
  143. if storage.exists(''): # check if storage location exists
  144. for path in utils.get_files(storage, ignore_patterns):
  145. yield path, storage
  146. def find(self, path, all=False):
  147. """
  148. Look for files in the app directories.
  149. """
  150. matches = []
  151. for app in self.apps:
  152. app_location = self.storages[app].location
  153. if app_location not in searched_locations:
  154. searched_locations.append(app_location)
  155. match = self.find_in_app(app, path)
  156. if match:
  157. if not all:
  158. return match
  159. matches.append(match)
  160. return matches
  161. def find_in_app(self, app, path):
  162. """
  163. Find a requested static file in an app's static locations.
  164. """
  165. storage = self.storages.get(app)
  166. if storage:
  167. # only try to find a file if the source dir actually exists
  168. if storage.exists(path):
  169. matched_path = storage.path(path)
  170. if matched_path:
  171. return matched_path
  172. class BaseStorageFinder(BaseFinder):
  173. """
  174. A base static files finder to be used to extended
  175. with an own storage class.
  176. """
  177. storage = None
  178. def __init__(self, storage=None, *args, **kwargs):
  179. if storage is not None:
  180. self.storage = storage
  181. if self.storage is None:
  182. raise ImproperlyConfigured("The staticfiles storage finder %r "
  183. "doesn't have a storage class "
  184. "assigned." % self.__class__)
  185. # Make sure we have a storage instance here.
  186. if not isinstance(self.storage, (Storage, LazyObject)):
  187. self.storage = self.storage()
  188. super().__init__(*args, **kwargs)
  189. def find(self, path, all=False):
  190. """
  191. Look for files in the default file storage, if it's local.
  192. """
  193. try:
  194. self.storage.path('')
  195. except NotImplementedError:
  196. pass
  197. else:
  198. if self.storage.location not in searched_locations:
  199. searched_locations.append(self.storage.location)
  200. if self.storage.exists(path):
  201. match = self.storage.path(path)
  202. if all:
  203. match = [match]
  204. return match
  205. return []
  206. def list(self, ignore_patterns):
  207. """
  208. List all files of the storage.
  209. """
  210. for path in utils.get_files(self.storage, ignore_patterns):
  211. yield path, self.storage
  212. class DefaultStorageFinder(BaseStorageFinder):
  213. """
  214. A static files finder that uses the default storage backend.
  215. """
  216. storage = default_storage
  217. def __init__(self, *args, **kwargs):
  218. super().__init__(*args, **kwargs)
  219. base_location = getattr(self.storage, 'base_location', empty)
  220. if not base_location:
  221. raise ImproperlyConfigured("The storage backend of the "
  222. "staticfiles finder %r doesn't have "
  223. "a valid location." % self.__class__)
  224. def find(path, all=False):
  225. """
  226. Find a static file with the given path using all enabled finders.
  227. If ``all`` is ``False`` (default), return the first matching
  228. absolute path (or ``None`` if no match). Otherwise return a list.
  229. """
  230. searched_locations[:] = []
  231. matches = []
  232. for finder in get_finders():
  233. result = finder.find(path, all=all)
  234. if not all and result:
  235. return result
  236. if not isinstance(result, (list, tuple)):
  237. result = [result]
  238. matches.extend(result)
  239. if matches:
  240. return matches
  241. # No match.
  242. return [] if all else None
  243. def get_finders():
  244. for finder_path in settings.STATICFILES_FINDERS:
  245. yield get_finder(finder_path)
  246. @functools.lru_cache(maxsize=None)
  247. def get_finder(import_path):
  248. """
  249. Import the staticfiles finder class described by import_path, where
  250. import_path is the full Python path to the class.
  251. """
  252. Finder = import_string(import_path)
  253. if not issubclass(Finder, BaseFinder):
  254. raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' %
  255. (Finder, BaseFinder))
  256. return Finder()