mirror of https://github.com/django/django.git
Refs #26029 -- Removed DEFAULT_FILE_STORAGE and STATICFILES_STORAGE settings.
This also removes django.core.files.storage.get_storage_class(). Per deprecation timeline.
This commit is contained in:
parent
3a3e737694
commit
f72f420f17
|
@ -16,21 +16,12 @@ from pathlib import Path
|
|||
import django
|
||||
from django.conf import global_settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.deprecation import RemovedInDjango51Warning
|
||||
from django.utils.functional import LazyObject, empty
|
||||
|
||||
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
|
||||
DEFAULT_STORAGE_ALIAS = "default"
|
||||
STATICFILES_STORAGE_ALIAS = "staticfiles"
|
||||
|
||||
DEFAULT_FILE_STORAGE_DEPRECATED_MSG = (
|
||||
"The DEFAULT_FILE_STORAGE setting is deprecated. Use STORAGES instead."
|
||||
)
|
||||
|
||||
STATICFILES_STORAGE_DEPRECATED_MSG = (
|
||||
"The STATICFILES_STORAGE setting is deprecated. Use STORAGES instead."
|
||||
)
|
||||
|
||||
|
||||
class SettingsReference(str):
|
||||
"""
|
||||
|
@ -155,22 +146,6 @@ class LazySettings(LazyObject):
|
|||
if not filename.startswith(os.path.dirname(django.__file__)):
|
||||
warnings.warn(message, category, stacklevel=2)
|
||||
|
||||
# RemovedInDjango51Warning.
|
||||
@property
|
||||
def DEFAULT_FILE_STORAGE(self):
|
||||
self._show_deprecation_warning(
|
||||
DEFAULT_FILE_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning
|
||||
)
|
||||
return self.__getattr__("DEFAULT_FILE_STORAGE")
|
||||
|
||||
# RemovedInDjango51Warning.
|
||||
@property
|
||||
def STATICFILES_STORAGE(self):
|
||||
self._show_deprecation_warning(
|
||||
STATICFILES_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning
|
||||
)
|
||||
return self.__getattr__("STATICFILES_STORAGE")
|
||||
|
||||
|
||||
class Settings:
|
||||
def __init__(self, settings_module):
|
||||
|
@ -217,40 +192,6 @@ class Settings:
|
|||
os.environ["TZ"] = self.TIME_ZONE
|
||||
time.tzset()
|
||||
|
||||
if self.is_overridden("DEFAULT_FILE_STORAGE"):
|
||||
if self.is_overridden("STORAGES"):
|
||||
raise ImproperlyConfigured(
|
||||
"DEFAULT_FILE_STORAGE/STORAGES are mutually exclusive."
|
||||
)
|
||||
self.STORAGES = {
|
||||
**self.STORAGES,
|
||||
DEFAULT_STORAGE_ALIAS: {"BACKEND": self.DEFAULT_FILE_STORAGE},
|
||||
}
|
||||
warnings.warn(DEFAULT_FILE_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning)
|
||||
|
||||
if self.is_overridden("STATICFILES_STORAGE"):
|
||||
if self.is_overridden("STORAGES"):
|
||||
raise ImproperlyConfigured(
|
||||
"STATICFILES_STORAGE/STORAGES are mutually exclusive."
|
||||
)
|
||||
self.STORAGES = {
|
||||
**self.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {"BACKEND": self.STATICFILES_STORAGE},
|
||||
}
|
||||
warnings.warn(STATICFILES_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning)
|
||||
# RemovedInDjango51Warning.
|
||||
if self.is_overridden("STORAGES"):
|
||||
setattr(
|
||||
self,
|
||||
"DEFAULT_FILE_STORAGE",
|
||||
self.STORAGES.get(DEFAULT_STORAGE_ALIAS, {}).get("BACKEND"),
|
||||
)
|
||||
setattr(
|
||||
self,
|
||||
"STATICFILES_STORAGE",
|
||||
self.STORAGES.get(STATICFILES_STORAGE_ALIAS, {}).get("BACKEND"),
|
||||
)
|
||||
|
||||
def is_overridden(self, setting):
|
||||
return setting in self._explicit_settings
|
||||
|
||||
|
@ -283,41 +224,7 @@ class UserSettingsHolder:
|
|||
|
||||
def __setattr__(self, name, value):
|
||||
self._deleted.discard(name)
|
||||
if name == "DEFAULT_FILE_STORAGE":
|
||||
self.STORAGES[DEFAULT_STORAGE_ALIAS] = {
|
||||
"BACKEND": self.DEFAULT_FILE_STORAGE
|
||||
}
|
||||
warnings.warn(DEFAULT_FILE_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning)
|
||||
if name == "STATICFILES_STORAGE":
|
||||
self.STORAGES[STATICFILES_STORAGE_ALIAS] = {
|
||||
"BACKEND": self.STATICFILES_STORAGE
|
||||
}
|
||||
warnings.warn(STATICFILES_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning)
|
||||
super().__setattr__(name, value)
|
||||
# RemovedInDjango51Warning.
|
||||
if name == "STORAGES":
|
||||
if default_file_storage := self.STORAGES.get(DEFAULT_STORAGE_ALIAS):
|
||||
super().__setattr__(
|
||||
"DEFAULT_FILE_STORAGE", default_file_storage.get("BACKEND")
|
||||
)
|
||||
else:
|
||||
self.STORAGES.setdefault(
|
||||
DEFAULT_STORAGE_ALIAS,
|
||||
{"BACKEND": "django.core.files.storage.FileSystemStorage"},
|
||||
)
|
||||
if staticfiles_storage := self.STORAGES.get(STATICFILES_STORAGE_ALIAS):
|
||||
super().__setattr__(
|
||||
"STATICFILES_STORAGE", staticfiles_storage.get("BACKEND")
|
||||
)
|
||||
else:
|
||||
self.STORAGES.setdefault(
|
||||
STATICFILES_STORAGE_ALIAS,
|
||||
{
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.StaticFilesStorage"
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
def __delattr__(self, name):
|
||||
self._deleted.add(name)
|
||||
|
|
|
@ -268,9 +268,6 @@ SECRET_KEY = ""
|
|||
# secret key rotation.
|
||||
SECRET_KEY_FALLBACKS = []
|
||||
|
||||
# Default file storage mechanism that holds media.
|
||||
DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage"
|
||||
|
||||
STORAGES = {
|
||||
"default": {
|
||||
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
||||
|
@ -626,9 +623,6 @@ FIXTURE_DIRS = []
|
|||
# A list of locations of additional static files
|
||||
STATICFILES_DIRS = []
|
||||
|
||||
# The default file storage backend used during the build process
|
||||
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
|
||||
|
||||
# List of finder classes that know how to find static files in
|
||||
# various locations.
|
||||
STATICFILES_FINDERS = [
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
import warnings
|
||||
|
||||
from django.conf import DEFAULT_STORAGE_ALIAS, settings
|
||||
from django.utils.deprecation import RemovedInDjango51Warning
|
||||
from django.conf import DEFAULT_STORAGE_ALIAS
|
||||
from django.utils.functional import LazyObject
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
from .base import Storage
|
||||
from .filesystem import FileSystemStorage
|
||||
|
@ -16,22 +12,11 @@ __all__ = (
|
|||
"Storage",
|
||||
"DefaultStorage",
|
||||
"default_storage",
|
||||
"get_storage_class",
|
||||
"InvalidStorageError",
|
||||
"StorageHandler",
|
||||
"storages",
|
||||
)
|
||||
|
||||
GET_STORAGE_CLASS_DEPRECATED_MSG = (
|
||||
"django.core.files.storage.get_storage_class is deprecated in favor of "
|
||||
"using django.core.files.storage.storages."
|
||||
)
|
||||
|
||||
|
||||
def get_storage_class(import_path=None):
|
||||
warnings.warn(GET_STORAGE_CLASS_DEPRECATED_MSG, RemovedInDjango51Warning)
|
||||
return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
|
||||
|
||||
|
||||
class DefaultStorage(LazyObject):
|
||||
def _setup(self):
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from django.conf import DEFAULT_STORAGE_ALIAS, STATICFILES_STORAGE_ALIAS, settings
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.module_loading import import_string
|
||||
|
@ -19,15 +19,6 @@ class StorageHandler:
|
|||
def backends(self):
|
||||
if self._backends is None:
|
||||
self._backends = settings.STORAGES.copy()
|
||||
# RemovedInDjango51Warning.
|
||||
if settings.is_overridden("DEFAULT_FILE_STORAGE"):
|
||||
self._backends[DEFAULT_STORAGE_ALIAS] = {
|
||||
"BACKEND": settings.DEFAULT_FILE_STORAGE
|
||||
}
|
||||
if settings.is_overridden("STATICFILES_STORAGE"):
|
||||
self._backends[STATICFILES_STORAGE_ALIAS] = {
|
||||
"BACKEND": settings.STATICFILES_STORAGE
|
||||
}
|
||||
return self._backends
|
||||
|
||||
def __getitem__(self, alias):
|
||||
|
|
|
@ -13,7 +13,6 @@ from django.dispatch import Signal, receiver
|
|||
from django.utils import timezone
|
||||
from django.utils.formats import FORMAT_SETTINGS, reset_format_cache
|
||||
from django.utils.functional import empty
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
template_rendered = Signal()
|
||||
|
||||
|
@ -161,21 +160,6 @@ def localize_settings_changed(*, setting, **kwargs):
|
|||
reset_format_cache()
|
||||
|
||||
|
||||
# RemovedInDjango51Warning.
|
||||
@receiver(setting_changed)
|
||||
def file_storage_changed(*, setting, **kwargs):
|
||||
if setting == "DEFAULT_FILE_STORAGE":
|
||||
from django.conf import DEFAULT_STORAGE_ALIAS
|
||||
from django.core.files.storage import default_storage, storages
|
||||
|
||||
try:
|
||||
del storages.backends
|
||||
except AttributeError:
|
||||
pass
|
||||
storages._storages[DEFAULT_STORAGE_ALIAS] = import_string(kwargs["value"])()
|
||||
default_storage._wrapped = empty
|
||||
|
||||
|
||||
@receiver(setting_changed)
|
||||
def complex_setting_changed(*, enter, setting, **kwargs):
|
||||
if enter and setting in COMPLEX_OVERRIDE_SETTINGS:
|
||||
|
@ -199,7 +183,6 @@ def root_urlconf_changed(*, setting, **kwargs):
|
|||
@receiver(setting_changed)
|
||||
def static_storage_changed(*, setting, **kwargs):
|
||||
if setting in {
|
||||
"STATICFILES_STORAGE",
|
||||
"STATIC_ROOT",
|
||||
"STATIC_URL",
|
||||
}:
|
||||
|
@ -207,17 +190,6 @@ def static_storage_changed(*, setting, **kwargs):
|
|||
|
||||
staticfiles_storage._wrapped = empty
|
||||
|
||||
# RemovedInDjango51Warning.
|
||||
if setting == "STATICFILES_STORAGE":
|
||||
from django.conf import STATICFILES_STORAGE_ALIAS
|
||||
from django.core.files.storage import storages
|
||||
|
||||
try:
|
||||
del storages.backends
|
||||
except AttributeError:
|
||||
pass
|
||||
storages._storages[STATICFILES_STORAGE_ALIAS] = import_string(kwargs["value"])()
|
||||
|
||||
|
||||
@receiver(setting_changed)
|
||||
def static_finders_changed(*, setting, **kwargs):
|
||||
|
|
|
@ -27,7 +27,6 @@ following settings:
|
|||
* :setting:`STATIC_ROOT`
|
||||
* :setting:`STATIC_URL`
|
||||
* :setting:`STATICFILES_DIRS`
|
||||
* :setting:`STATICFILES_STORAGE`
|
||||
* :setting:`STATICFILES_FINDERS`
|
||||
|
||||
Management Commands
|
||||
|
|
|
@ -25,22 +25,6 @@ Django provides convenient ways to access the default storage class:
|
|||
:data:`~django.core.files.storage.default_storage` is an instance of the
|
||||
:class:`~django.core.files.storage.DefaultStorage`.
|
||||
|
||||
.. function:: get_storage_class(import_path=None)
|
||||
|
||||
Returns a class or module which implements the storage API.
|
||||
|
||||
When called without the ``import_path`` parameter ``get_storage_class``
|
||||
will return the default storage system as defined by ``default`` key in
|
||||
:setting:`STORAGES`. If ``import_path`` is provided, ``get_storage_class``
|
||||
will attempt to import the class or module from the given path and will
|
||||
return it if successful. An exception will be raised if the import is
|
||||
unsuccessful.
|
||||
|
||||
.. deprecated:: 4.2
|
||||
|
||||
The ``get_storage_class()`` function is deprecated. Use
|
||||
:data:`storages` instead
|
||||
|
||||
The ``FileSystemStorage`` class
|
||||
===============================
|
||||
|
||||
|
|
|
@ -1329,22 +1329,6 @@ Default exception reporter filter class to be used if none has been assigned to
|
|||
the :class:`~django.http.HttpRequest` instance yet.
|
||||
See :ref:`Filtering error reports<filtering-error-reports>`.
|
||||
|
||||
.. setting:: DEFAULT_FILE_STORAGE
|
||||
|
||||
``DEFAULT_FILE_STORAGE``
|
||||
------------------------
|
||||
|
||||
Default: ``'``:class:`django.core.files.storage.FileSystemStorage`\ ``'``
|
||||
|
||||
Default file storage class to be used for any file-related operations that don't
|
||||
specify a particular storage system. See :doc:`/topics/files`.
|
||||
|
||||
.. deprecated:: 4.2
|
||||
|
||||
This setting is deprecated. Starting with Django 4.2, default file storage
|
||||
engine can be configured with the :setting:`STORAGES` setting under the
|
||||
``default`` key.
|
||||
|
||||
.. setting:: DEFAULT_FROM_EMAIL
|
||||
|
||||
``DEFAULT_FROM_EMAIL``
|
||||
|
@ -3548,27 +3532,6 @@ This would allow you to refer to the local file
|
|||
|
||||
<a href="{% static 'downloads/polls_20101022.tar.gz' %}">
|
||||
|
||||
.. setting:: STATICFILES_STORAGE
|
||||
|
||||
``STATICFILES_STORAGE``
|
||||
-----------------------
|
||||
|
||||
Default: ``'django.contrib.staticfiles.storage.StaticFilesStorage'``
|
||||
|
||||
The file storage engine to use when collecting static files with the
|
||||
:djadmin:`collectstatic` management command.
|
||||
|
||||
A ready-to-use instance of the storage backend defined in this setting
|
||||
can be found under ``staticfiles`` key in ``django.core.files.storage.storages``.
|
||||
|
||||
For an example, see :ref:`staticfiles-from-cdn`.
|
||||
|
||||
.. deprecated:: 4.2
|
||||
|
||||
This setting is deprecated. Starting with Django 4.2, static files storage
|
||||
engine can be configured with the :setting:`STORAGES` setting under the
|
||||
``staticfiles`` key.
|
||||
|
||||
.. setting:: STATICFILES_FINDERS
|
||||
|
||||
``STATICFILES_FINDERS``
|
||||
|
@ -3660,7 +3623,6 @@ Error reporting
|
|||
|
||||
File uploads
|
||||
------------
|
||||
* :setting:`DEFAULT_FILE_STORAGE`
|
||||
* :setting:`FILE_UPLOAD_HANDLERS`
|
||||
* :setting:`FILE_UPLOAD_MAX_MEMORY_SIZE`
|
||||
* :setting:`FILE_UPLOAD_PERMISSIONS`
|
||||
|
|
|
@ -466,7 +466,7 @@ more details.
|
|||
|
||||
The :mod:`staticfiles<django.contrib.staticfiles>` contrib app has a new
|
||||
``static`` template tag to refer to files saved with the
|
||||
:setting:`STATICFILES_STORAGE` storage backend. It uses the storage backend's
|
||||
``STATICFILES_STORAGE`` storage backend. It uses the storage backend's
|
||||
``url`` method and therefore supports advanced features such as :ref:`serving
|
||||
files from a cloud service<staticfiles-from-cdn>`.
|
||||
|
||||
|
|
|
@ -280,3 +280,7 @@ to remove usage of these features.
|
|||
|
||||
* Support for passing positional arguments to ``Signer`` and
|
||||
``TimestampSigner`` is removed.
|
||||
|
||||
* The ``DEFAULT_FILE_STORAGE`` and ``STATICFILES_STORAGE`` settings is removed.
|
||||
|
||||
* The ``django.core.files.storage.get_storage_class()`` function is removed.
|
||||
|
|
|
@ -1497,15 +1497,15 @@ when settings are changed.
|
|||
|
||||
Django itself uses this signal to reset various data:
|
||||
|
||||
============================================================================ ========================
|
||||
Overridden settings Data reset
|
||||
============================================================================ ========================
|
||||
USE_TZ, TIME_ZONE Databases timezone
|
||||
TEMPLATES Template engines
|
||||
SERIALIZATION_MODULES Serializers cache
|
||||
LOCALE_PATHS, LANGUAGE_CODE Default translation and loaded translations
|
||||
DEFAULT_FILE_STORAGE, STATICFILES_STORAGE, STATIC_ROOT, STATIC_URL, STORAGES Storages configuration
|
||||
============================================================================ ========================
|
||||
================================= ========================
|
||||
Overridden settings Data reset
|
||||
================================= ========================
|
||||
USE_TZ, TIME_ZONE Databases timezone
|
||||
TEMPLATES Template engines
|
||||
SERIALIZATION_MODULES Serializers cache
|
||||
LOCALE_PATHS, LANGUAGE_CODE Default translation and loaded translations
|
||||
STATIC_ROOT, STATIC_URL, STORAGES Storages configuration
|
||||
================================= ========================
|
||||
|
||||
Isolating apps
|
||||
--------------
|
||||
|
|
|
@ -1,262 +0,0 @@
|
|||
import sys
|
||||
from types import ModuleType
|
||||
|
||||
from django.conf import (
|
||||
DEFAULT_FILE_STORAGE_DEPRECATED_MSG,
|
||||
DEFAULT_STORAGE_ALIAS,
|
||||
STATICFILES_STORAGE_ALIAS,
|
||||
STATICFILES_STORAGE_DEPRECATED_MSG,
|
||||
Settings,
|
||||
settings,
|
||||
)
|
||||
from django.contrib.staticfiles.storage import (
|
||||
ManifestStaticFilesStorage,
|
||||
staticfiles_storage,
|
||||
)
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.files.storage import Storage, StorageHandler, default_storage, storages
|
||||
from django.test import TestCase, ignore_warnings
|
||||
from django.utils.deprecation import RemovedInDjango51Warning
|
||||
|
||||
|
||||
class StaticfilesStorageDeprecationTests(TestCase):
|
||||
msg = STATICFILES_STORAGE_DEPRECATED_MSG
|
||||
|
||||
def test_override_settings_warning(self):
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
|
||||
with self.settings(
|
||||
STATICFILES_STORAGE=(
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
):
|
||||
pass
|
||||
|
||||
def test_settings_init(self):
|
||||
old_staticfiles_storage = settings.STORAGES.get(STATICFILES_STORAGE_ALIAS)
|
||||
settings_module = ModuleType("fake_settings_module")
|
||||
settings_module.USE_TZ = True
|
||||
settings_module.STATICFILES_STORAGE = (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
sys.modules["fake_settings_module"] = settings_module
|
||||
try:
|
||||
with self.assertWarnsMessage(RemovedInDjango51Warning, self.msg):
|
||||
fake_settings = Settings("fake_settings_module")
|
||||
self.assertEqual(
|
||||
fake_settings.STORAGES[STATICFILES_STORAGE_ALIAS],
|
||||
{
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
),
|
||||
},
|
||||
)
|
||||
# settings.STORAGES is not mutated.
|
||||
self.assertEqual(
|
||||
settings.STORAGES.get(STATICFILES_STORAGE_ALIAS),
|
||||
old_staticfiles_storage,
|
||||
)
|
||||
finally:
|
||||
del sys.modules["fake_settings_module"]
|
||||
|
||||
def test_settings_storages_init(self):
|
||||
settings_module = ModuleType("fake_settings_module")
|
||||
settings_module.USE_TZ = True
|
||||
settings_module.STORAGES = {
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
}
|
||||
}
|
||||
sys.modules["fake_settings_module"] = settings_module
|
||||
try:
|
||||
fake_settings = Settings("fake_settings_module")
|
||||
self.assertEqual(
|
||||
fake_settings.STATICFILES_STORAGE,
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
|
||||
)
|
||||
finally:
|
||||
del sys.modules["fake_settings_module"]
|
||||
|
||||
def test_access_warning(self):
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
|
||||
settings.STATICFILES_STORAGE
|
||||
# Works a second time.
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
|
||||
settings.STATICFILES_STORAGE
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_access(self):
|
||||
with self.settings(
|
||||
STATICFILES_STORAGE=(
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
):
|
||||
self.assertEqual(
|
||||
settings.STATICFILES_STORAGE,
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
|
||||
)
|
||||
# Works a second time.
|
||||
self.assertEqual(
|
||||
settings.STATICFILES_STORAGE,
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
|
||||
)
|
||||
|
||||
def test_use_both_error(self):
|
||||
msg = "STATICFILES_STORAGE/STORAGES are mutually exclusive."
|
||||
settings_module = ModuleType("fake_settings_module")
|
||||
settings_module.USE_TZ = True
|
||||
settings_module.STATICFILES_STORAGE = (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
settings_module.STORAGES = {}
|
||||
sys.modules["fake_settings_module"] = settings_module
|
||||
try:
|
||||
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
||||
Settings("fake_settings_module")
|
||||
finally:
|
||||
del sys.modules["fake_settings_module"]
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_storage(self):
|
||||
empty_storages = StorageHandler()
|
||||
with self.settings(
|
||||
STATICFILES_STORAGE=(
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
):
|
||||
self.assertIsInstance(
|
||||
storages[STATICFILES_STORAGE_ALIAS],
|
||||
ManifestStaticFilesStorage,
|
||||
)
|
||||
self.assertIsInstance(
|
||||
empty_storages[STATICFILES_STORAGE_ALIAS],
|
||||
ManifestStaticFilesStorage,
|
||||
)
|
||||
self.assertIsInstance(staticfiles_storage, ManifestStaticFilesStorage)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_staticfiles_storage(self):
|
||||
with self.settings(
|
||||
STORAGES={
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
)
|
||||
}
|
||||
}
|
||||
):
|
||||
self.assertEqual(
|
||||
settings.STATICFILES_STORAGE,
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
|
||||
)
|
||||
self.assertIsInstance(
|
||||
storages[STATICFILES_STORAGE_ALIAS],
|
||||
ManifestStaticFilesStorage,
|
||||
)
|
||||
|
||||
|
||||
class DefaultStorageDeprecationTests(TestCase):
|
||||
msg = DEFAULT_FILE_STORAGE_DEPRECATED_MSG
|
||||
|
||||
def test_override_settings_warning(self):
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
|
||||
with self.settings(
|
||||
DEFAULT_FILE_STORAGE=("django.core.files.storage.Storage")
|
||||
):
|
||||
pass
|
||||
|
||||
def test_settings_init(self):
|
||||
old_default_storage = settings.STORAGES.get(DEFAULT_STORAGE_ALIAS)
|
||||
settings_module = ModuleType("fake_settings_module")
|
||||
settings_module.USE_TZ = True
|
||||
settings_module.DEFAULT_FILE_STORAGE = "django.core.files.storage.Storage"
|
||||
sys.modules["fake_settings_module"] = settings_module
|
||||
try:
|
||||
with self.assertWarnsMessage(RemovedInDjango51Warning, self.msg):
|
||||
fake_settings = Settings("fake_settings_module")
|
||||
self.assertEqual(
|
||||
fake_settings.STORAGES[DEFAULT_STORAGE_ALIAS],
|
||||
{"BACKEND": "django.core.files.storage.Storage"},
|
||||
)
|
||||
# settings.STORAGES is not mutated.
|
||||
self.assertEqual(
|
||||
settings.STORAGES.get(DEFAULT_STORAGE_ALIAS),
|
||||
old_default_storage,
|
||||
)
|
||||
finally:
|
||||
del sys.modules["fake_settings_module"]
|
||||
|
||||
def test_settings_storages_init(self):
|
||||
settings_module = ModuleType("fake_settings_module")
|
||||
settings_module.USE_TZ = True
|
||||
settings_module.STORAGES = {
|
||||
DEFAULT_STORAGE_ALIAS: {
|
||||
"BACKEND": "django.core.files.storage.Storage",
|
||||
}
|
||||
}
|
||||
sys.modules["fake_settings_module"] = settings_module
|
||||
try:
|
||||
fake_settings = Settings("fake_settings_module")
|
||||
self.assertEqual(
|
||||
fake_settings.DEFAULT_FILE_STORAGE,
|
||||
"django.core.files.storage.Storage",
|
||||
)
|
||||
finally:
|
||||
del sys.modules["fake_settings_module"]
|
||||
|
||||
def test_access_warning(self):
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
|
||||
settings.DEFAULT_FILE_STORAGE
|
||||
# Works a second time.
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
|
||||
settings.DEFAULT_FILE_STORAGE
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_access(self):
|
||||
with self.settings(DEFAULT_FILE_STORAGE="django.core.files.storage.Storage"):
|
||||
self.assertEqual(
|
||||
settings.DEFAULT_FILE_STORAGE,
|
||||
"django.core.files.storage.Storage",
|
||||
)
|
||||
# Works a second time.
|
||||
self.assertEqual(
|
||||
settings.DEFAULT_FILE_STORAGE,
|
||||
"django.core.files.storage.Storage",
|
||||
)
|
||||
|
||||
def test_use_both_error(self):
|
||||
msg = "DEFAULT_FILE_STORAGE/STORAGES are mutually exclusive."
|
||||
settings_module = ModuleType("fake_settings_module")
|
||||
settings_module.USE_TZ = True
|
||||
settings_module.DEFAULT_FILE_STORAGE = "django.core.files.storage.Storage"
|
||||
settings_module.STORAGES = {}
|
||||
sys.modules["fake_settings_module"] = settings_module
|
||||
try:
|
||||
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
||||
Settings("fake_settings_module")
|
||||
finally:
|
||||
del sys.modules["fake_settings_module"]
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_storage(self):
|
||||
empty_storages = StorageHandler()
|
||||
with self.settings(DEFAULT_FILE_STORAGE="django.core.files.storage.Storage"):
|
||||
self.assertIsInstance(storages[DEFAULT_STORAGE_ALIAS], Storage)
|
||||
self.assertIsInstance(empty_storages[DEFAULT_STORAGE_ALIAS], Storage)
|
||||
self.assertIsInstance(default_storage, Storage)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_default_file_storage(self):
|
||||
with self.settings(
|
||||
STORAGES={
|
||||
DEFAULT_STORAGE_ALIAS: {
|
||||
"BACKEND": "django.core.files.storage.Storage",
|
||||
}
|
||||
}
|
||||
):
|
||||
self.assertEqual(
|
||||
settings.DEFAULT_FILE_STORAGE,
|
||||
"django.core.files.storage.Storage",
|
||||
)
|
||||
self.assertIsInstance(storages[DEFAULT_STORAGE_ALIAS], Storage)
|
|
@ -15,18 +15,9 @@ from django.conf import DEFAULT_STORAGE_ALIAS, STATICFILES_STORAGE_ALIAS
|
|||
from django.core.cache import cache
|
||||
from django.core.exceptions import SuspiciousFileOperation
|
||||
from django.core.files.base import ContentFile, File
|
||||
from django.core.files.storage import (
|
||||
GET_STORAGE_CLASS_DEPRECATED_MSG,
|
||||
FileSystemStorage,
|
||||
InvalidStorageError,
|
||||
)
|
||||
from django.core.files.storage import FileSystemStorage, InvalidStorageError
|
||||
from django.core.files.storage import Storage as BaseStorage
|
||||
from django.core.files.storage import (
|
||||
StorageHandler,
|
||||
default_storage,
|
||||
get_storage_class,
|
||||
storages,
|
||||
)
|
||||
from django.core.files.storage import StorageHandler, default_storage, storages
|
||||
from django.core.files.uploadedfile import (
|
||||
InMemoryUploadedFile,
|
||||
SimpleUploadedFile,
|
||||
|
@ -35,11 +26,10 @@ from django.core.files.uploadedfile import (
|
|||
from django.db.models import FileField
|
||||
from django.db.models.fields.files import FileDescriptor
|
||||
from django.test import LiveServerTestCase, SimpleTestCase, TestCase, override_settings
|
||||
from django.test.utils import ignore_warnings, requires_tz_support
|
||||
from django.test.utils import requires_tz_support
|
||||
from django.urls import NoReverseMatch, reverse_lazy
|
||||
from django.utils import timezone
|
||||
from django.utils._os import symlinks_supported
|
||||
from django.utils.deprecation import RemovedInDjango51Warning
|
||||
|
||||
from .models import (
|
||||
Storage,
|
||||
|
@ -52,51 +42,6 @@ from .models import (
|
|||
FILE_SUFFIX_REGEX = "[A-Za-z0-9]{7}"
|
||||
|
||||
|
||||
class GetStorageClassTests(SimpleTestCase):
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_get_filesystem_storage(self):
|
||||
"""
|
||||
get_storage_class returns the class for a storage backend name/path.
|
||||
"""
|
||||
self.assertEqual(
|
||||
get_storage_class("django.core.files.storage.FileSystemStorage"),
|
||||
FileSystemStorage,
|
||||
)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_get_invalid_storage_module(self):
|
||||
"""
|
||||
get_storage_class raises an error if the requested import don't exist.
|
||||
"""
|
||||
with self.assertRaisesMessage(ImportError, "No module named 'storage'"):
|
||||
get_storage_class("storage.NonexistentStorage")
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_get_nonexistent_storage_class(self):
|
||||
"""
|
||||
get_storage_class raises an error if the requested class don't exist.
|
||||
"""
|
||||
with self.assertRaises(ImportError):
|
||||
get_storage_class("django.core.files.storage.NonexistentStorage")
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango51Warning)
|
||||
def test_get_nonexistent_storage_module(self):
|
||||
"""
|
||||
get_storage_class raises an error if the requested module don't exist.
|
||||
"""
|
||||
with self.assertRaisesMessage(
|
||||
ImportError, "No module named 'django.core.files.nonexistent_storage'"
|
||||
):
|
||||
get_storage_class(
|
||||
"django.core.files.nonexistent_storage.NonexistentStorage"
|
||||
)
|
||||
|
||||
def test_deprecation_warning(self):
|
||||
msg = GET_STORAGE_CLASS_DEPRECATED_MSG
|
||||
with self.assertRaisesMessage(RemovedInDjango51Warning, msg):
|
||||
get_storage_class("django.core.files.storage.FileSystemStorage")
|
||||
|
||||
|
||||
class FileSystemStorageTests(unittest.TestCase):
|
||||
def test_deconstruction(self):
|
||||
path, args, kwargs = temp_storage.deconstruct()
|
||||
|
|
|
@ -137,11 +137,6 @@ class FindersCheckTests(CollectionTestCase):
|
|||
class StoragesCheckTests(SimpleTestCase):
|
||||
@override_settings(STORAGES={})
|
||||
def test_error_empty_storages(self):
|
||||
# DEFAULT_STORAGE_ALIAS and STATICFILES_STORAGE_ALIAS need to be
|
||||
# popped from STORAGES since UserSettingsHolder has code to maintain
|
||||
# backward compatibility until 5.1 is out.
|
||||
settings.STORAGES.clear() # RemovedInDjango51Warning
|
||||
assert settings.STORAGES == {} # RemovedInDjango51Warning
|
||||
errors = check_storages(None)
|
||||
self.assertEqual(errors, [E005])
|
||||
|
||||
|
@ -156,11 +151,6 @@ class StoragesCheckTests(SimpleTestCase):
|
|||
}
|
||||
)
|
||||
def test_error_missing_staticfiles(self):
|
||||
# Check out the previous comment about UserSettingsHolder compat code.
|
||||
settings.STORAGES.pop(STATICFILES_STORAGE_ALIAS) # RemovedInDjango51Warning
|
||||
assert (
|
||||
STATICFILES_STORAGE_ALIAS not in settings.STORAGES
|
||||
) # RemovedInDjango51Warning
|
||||
errors = check_storages(None)
|
||||
self.assertEqual(errors, [E005])
|
||||
|
||||
|
@ -172,10 +162,5 @@ class StoragesCheckTests(SimpleTestCase):
|
|||
}
|
||||
)
|
||||
def test_staticfiles_no_errors(self):
|
||||
# Check out the previous comment about UserSettingsHolder compat code.
|
||||
settings.STORAGES.pop(DEFAULT_STORAGE_ALIAS) # RemovedInDjango51Warning
|
||||
assert (
|
||||
DEFAULT_STORAGE_ALIAS not in settings.STORAGES
|
||||
) # RemovedInDjango51Warning
|
||||
errors = check_storages(None)
|
||||
self.assertEqual(errors, [])
|
||||
|
|
|
@ -142,11 +142,12 @@ class TestConfiguration(StaticFilesTestCase):
|
|||
storage.staticfiles_storage._wrapped = empty
|
||||
with self.settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.StaticFilesStorage"
|
||||
)
|
||||
}
|
||||
},
|
||||
}
|
||||
):
|
||||
command = collectstatic.Command()
|
||||
|
@ -155,9 +156,10 @@ class TestConfiguration(StaticFilesTestCase):
|
|||
storage.staticfiles_storage._wrapped = empty
|
||||
with self.settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.DummyStorage"
|
||||
}
|
||||
},
|
||||
}
|
||||
):
|
||||
command = collectstatic.Command()
|
||||
|
@ -250,6 +252,7 @@ class TestCollectionVerbosity(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
|
@ -264,6 +267,7 @@ class TestCollectionVerbosity(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": (
|
||||
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
|
@ -297,6 +301,7 @@ class TestCollectionClear(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.PathNotImplementedStorage"
|
||||
},
|
||||
|
@ -416,6 +421,7 @@ class TestCollectionDryRun(TestNoFilesCreated, CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
|
||||
},
|
||||
|
@ -544,6 +550,7 @@ class TestCollectionOverwriteWarning(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.DummyStorage"
|
||||
},
|
||||
|
@ -571,6 +578,7 @@ class TestCollectionNonLocalStorage(TestNoFilesCreated, CollectionTestCase):
|
|||
class TestCollectionNeverCopyStorage(CollectionTestCase):
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.NeverCopyRemoteStorage"
|
||||
},
|
||||
|
@ -642,9 +650,10 @@ class TestCollectionLinks(TestDefaults, CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.PathNotImplementedStorage"
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
def test_no_remote_link(self):
|
||||
|
|
|
@ -361,6 +361,7 @@ class TestHashedFiles:
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.ExtraPatternsStorage",
|
||||
},
|
||||
|
@ -396,6 +397,7 @@ class TestExtraPatternsStorage(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
|
||||
},
|
||||
|
@ -561,6 +563,7 @@ class TestCollectionManifestStorage(TestHashedFiles, CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.NoneHashStorage",
|
||||
},
|
||||
|
@ -576,6 +579,7 @@ class TestCollectionNoneHashStorage(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.NoPostProcessReplacedPathStorage",
|
||||
},
|
||||
|
@ -592,6 +596,7 @@ class TestCollectionNoPostProcessReplacedPaths(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.storage.SimpleStorage",
|
||||
},
|
||||
|
@ -630,6 +635,7 @@ class JSModuleImportAggregationManifestStorage(storage.ManifestStaticFilesStorag
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": (
|
||||
"staticfiles_tests.test_storage."
|
||||
|
@ -811,6 +817,7 @@ class TestStaticFilePermissions(CollectionTestCase):
|
|||
FILE_UPLOAD_PERMISSIONS=0o655,
|
||||
FILE_UPLOAD_DIRECTORY_PERMISSIONS=0o765,
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "staticfiles_tests.test_storage.CustomStaticFilesStorage",
|
||||
},
|
||||
|
@ -835,6 +842,7 @@ class TestStaticFilePermissions(CollectionTestCase):
|
|||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
**settings.STORAGES,
|
||||
STATICFILES_STORAGE_ALIAS: {
|
||||
"BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue