1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #34773 -- Fixed syncing DEFAULT_FILE_STORAGE/STATICFILES_STORAGE settings with STORAGES.

Thanks Petr Dlouhý for the report.

Bug in 32940d390a.
This commit is contained in:
Mariusz Felisiak 2023-08-18 18:11:24 +02:00 committed by GitHub
parent d25f389211
commit 6b965c6000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 133 additions and 12 deletions

View File

@ -222,6 +222,9 @@ class Settings:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"DEFAULT_FILE_STORAGE/STORAGES are mutually exclusive." "DEFAULT_FILE_STORAGE/STORAGES are mutually exclusive."
) )
self.STORAGES[DEFAULT_STORAGE_ALIAS] = {
"BACKEND": self.DEFAULT_FILE_STORAGE
}
warnings.warn(DEFAULT_FILE_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning) warnings.warn(DEFAULT_FILE_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning)
if self.is_overridden("STATICFILES_STORAGE"): if self.is_overridden("STATICFILES_STORAGE"):
@ -229,7 +232,22 @@ class Settings:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"STATICFILES_STORAGE/STORAGES are mutually exclusive." "STATICFILES_STORAGE/STORAGES are mutually exclusive."
) )
self.STORAGES[STATICFILES_STORAGE_ALIAS] = {
"BACKEND": self.STATICFILES_STORAGE
}
warnings.warn(STATICFILES_STORAGE_DEPRECATED_MSG, RemovedInDjango51Warning) 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): def is_overridden(self, setting):
return setting in self._explicit_settings return setting in self._explicit_settings
@ -276,13 +294,27 @@ class UserSettingsHolder:
super().__setattr__(name, value) super().__setattr__(name, value)
# RemovedInDjango51Warning. # RemovedInDjango51Warning.
if name == "STORAGES": 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( self.STORAGES.setdefault(
DEFAULT_STORAGE_ALIAS, DEFAULT_STORAGE_ALIAS,
{"BACKEND": "django.core.files.storage.FileSystemStorage"}, {"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( self.STORAGES.setdefault(
STATICFILES_STORAGE_ALIAS, STATICFILES_STORAGE_ALIAS,
{"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"}, {
"BACKEND": (
"django.contrib.staticfiles.storage.StaticFilesStorage"
),
},
) )
def __delattr__(self, name): def __delattr__(self, name):

View File

@ -12,3 +12,7 @@ Bugfixes
* Fixed a regression in Django 4.2 that caused an incorrect validation of * Fixed a regression in Django 4.2 that caused an incorrect validation of
``CheckConstraints`` on ``__isnull`` lookups against ``JSONField`` ``CheckConstraints`` on ``__isnull`` lookups against ``JSONField``
(:ticket:`34754`). (:ticket:`34754`).
* Fixed a bug in Django 4.2 where the deprecated ``DEFAULT_FILE_STORAGE`` and
``STATICFILES_STORAGE`` settings were not synced with ``STORAGES``
(:ticket:`34773`).

View File

@ -39,8 +39,36 @@ class StaticfilesStorageDeprecationTests(TestCase):
) )
sys.modules["fake_settings_module"] = settings_module sys.modules["fake_settings_module"] = settings_module
try: try:
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg): with self.assertWarnsMessage(RemovedInDjango51Warning, self.msg):
Settings("fake_settings_module") fake_settings = Settings("fake_settings_module")
self.assertEqual(
fake_settings.STORAGES[STATICFILES_STORAGE_ALIAS],
{
"BACKEND": (
"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
),
},
)
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: finally:
del sys.modules["fake_settings_module"] del sys.modules["fake_settings_module"]
@ -101,6 +129,26 @@ class StaticfilesStorageDeprecationTests(TestCase):
) )
self.assertIsInstance(staticfiles_storage, 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): class DefaultStorageDeprecationTests(TestCase):
msg = DEFAULT_FILE_STORAGE_DEPRECATED_MSG msg = DEFAULT_FILE_STORAGE_DEPRECATED_MSG
@ -118,8 +166,30 @@ class DefaultStorageDeprecationTests(TestCase):
settings_module.DEFAULT_FILE_STORAGE = "django.core.files.storage.Storage" settings_module.DEFAULT_FILE_STORAGE = "django.core.files.storage.Storage"
sys.modules["fake_settings_module"] = settings_module sys.modules["fake_settings_module"] = settings_module
try: try:
with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg): with self.assertWarnsMessage(RemovedInDjango51Warning, self.msg):
Settings("fake_settings_module") fake_settings = Settings("fake_settings_module")
self.assertEqual(
fake_settings.STORAGES[DEFAULT_STORAGE_ALIAS],
{"BACKEND": "django.core.files.storage.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: finally:
del sys.modules["fake_settings_module"] del sys.modules["fake_settings_module"]
@ -163,3 +233,18 @@ class DefaultStorageDeprecationTests(TestCase):
self.assertIsInstance(storages[DEFAULT_STORAGE_ALIAS], Storage) self.assertIsInstance(storages[DEFAULT_STORAGE_ALIAS], Storage)
self.assertIsInstance(empty_storages[DEFAULT_STORAGE_ALIAS], Storage) self.assertIsInstance(empty_storages[DEFAULT_STORAGE_ALIAS], Storage)
self.assertIsInstance(default_storage, 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)