/src/thirdparty/staticfiles/storage.py

https://github.com/zodiak/django_ondev · Python · 95 lines · 57 code · 19 blank · 19 comment · 5 complexity · bd1256d2b1f0bb865ccb35f54b0dd307 MD5 · raw file

  1. import os
  2. import warnings
  3. from datetime import datetime
  4. from django.core.files.storage import FileSystemStorage
  5. from django.core.exceptions import ImproperlyConfigured
  6. from django.utils.importlib import import_module
  7. from django.utils.functional import LazyObject
  8. from staticfiles.conf import settings
  9. class TimeAwareFileSystemStorage(FileSystemStorage):
  10. def accessed_time(self, name):
  11. return datetime.fromtimestamp(os.path.getatime(self.path(name)))
  12. def created_time(self, name):
  13. return datetime.fromtimestamp(os.path.getctime(self.path(name)))
  14. def modified_time(self, name):
  15. return datetime.fromtimestamp(os.path.getmtime(self.path(name)))
  16. class DefaultStorage(LazyObject):
  17. def _setup(self):
  18. self._wrapped = TimeAwareFileSystemStorage()
  19. default_storage = DefaultStorage()
  20. class StaticFilesStorage(TimeAwareFileSystemStorage):
  21. """
  22. Standard file system storage for static files.
  23. The defaults for ``location`` and ``base_url`` are
  24. ``STATIC_ROOT`` and ``STATIC_URL``.
  25. """
  26. def __init__(self, location=None, base_url=None, *args, **kwargs):
  27. if location is None:
  28. location = settings.STATIC_ROOT
  29. if base_url is None:
  30. base_url = settings.STATIC_URL
  31. if not location:
  32. raise ImproperlyConfigured("You're using the staticfiles app "
  33. "without having set the STATIC_ROOT setting. Set it to "
  34. "the absolute path of the directory that holds static files.")
  35. # check for None since we might use a root URL (``/``)
  36. if base_url is None:
  37. raise ImproperlyConfigured("You're using the staticfiles app "
  38. "without having set the STATIC_URL setting. Set it to "
  39. "URL that handles the files served from STATIC_ROOT.")
  40. super(StaticFilesStorage, self).__init__(location, base_url, *args, **kwargs)
  41. class StaticFileStorage(StaticFilesStorage):
  42. def __init__(self, *args, **kwargs):
  43. warnings.warn(
  44. "The storage backend 'staticfiles.storage.StaticFileStorage' "
  45. "was renamed to 'staticfiles.storage.StaticFilesStorage'.",
  46. PendingDeprecationWarning)
  47. super(StaticFileStorage, self).__init__(*args, **kwargs)
  48. class AppStaticStorage(TimeAwareFileSystemStorage):
  49. """
  50. A file system storage backend that takes an app module and works
  51. for the ``static`` directory of it.
  52. """
  53. prefix = None
  54. source_dir = 'static'
  55. def __init__(self, app, *args, **kwargs):
  56. """
  57. Returns a static file storage if available in the given app.
  58. """
  59. # app is the actual app module
  60. self.app_module = app
  61. # We special case the admin app here since it has its static files
  62. # in 'media' for historic reasons.
  63. if self.app_module == 'django.contrib.admin':
  64. self.prefix = 'admin'
  65. self.source_dir = 'media'
  66. mod = import_module(self.app_module)
  67. mod_path = os.path.dirname(mod.__file__)
  68. location = os.path.join(mod_path, self.source_dir)
  69. super(AppStaticStorage, self).__init__(location, *args, **kwargs)
  70. class LegacyAppMediaStorage(AppStaticStorage):
  71. """
  72. A legacy app storage backend that provides a migration path for the
  73. default directory name in previous versions of staticfiles, "media".
  74. """
  75. source_dir = 'media'