Skip to content

Commit 2ab3cf0

Browse files
committed
fix: storage compatibility issue for django42
1 parent fbe3db2 commit 2ab3cf0

File tree

4 files changed

+83
-19
lines changed

4 files changed

+83
-19
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import importlib
2+
import sys
3+
import unittest
4+
from unittest import mock
5+
6+
7+
class TestStorageSettings(unittest.TestCase):
8+
def setUp(self):
9+
self.module_path = "credentials.settings.base"
10+
11+
def tearDown(self):
12+
if self.module_path in sys.modules:
13+
del sys.modules[self.module_path]
14+
15+
@mock.patch("django.VERSION", new=(4, 2))
16+
def test_django_42_or_higher(self):
17+
if self.module_path in sys.modules:
18+
del sys.modules[self.module_path]
19+
20+
imported = importlib.import_module(self.module_path)
21+
22+
# Verify correct settings for Django 4.2+
23+
self.assertTrue(hasattr(imported, "STORAGES"))
24+
self.assertEqual(imported.STORAGES["default"]["BACKEND"], "django.core.files.storage.FileSystemStorage")
25+
self.assertEqual(
26+
imported.STORAGES["staticfiles"]["BACKEND"], "django.contrib.staticfiles.storage.StaticFilesStorage"
27+
)
28+
self.assertFalse(hasattr(imported, "DEFAULT_FILE_STORAGE"))
29+
self.assertFalse(hasattr(imported, "STATICFILES_STORAGE"))
30+
31+
@mock.patch("django.VERSION", new=(4, 1))
32+
def test_django_below_42(self):
33+
if self.module_path in sys.modules:
34+
del sys.modules[self.module_path]
35+
36+
imported = importlib.import_module(self.module_path)
37+
38+
# Verify correct settings for Django < 4.2
39+
self.assertTrue(hasattr(imported, "DEFAULT_FILE_STORAGE"))
40+
self.assertTrue(hasattr(imported, "STATICFILES_STORAGE"))
41+
self.assertEqual(imported.DEFAULT_FILE_STORAGE, "django.core.files.storage.FileSystemStorage")
42+
self.assertEqual(imported.STATICFILES_STORAGE, "django.contrib.staticfiles.storage.StaticFilesStorage")
43+
self.assertFalse(hasattr(imported, "STORAGES"))

credentials/settings/base.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from os.path import abspath, dirname, join
44

55
from corsheaders.defaults import default_headers as corsheaders_default_headers
6+
import django
67
from django.conf.global_settings import LANGUAGES_BIDI
78
from edx_toggles.toggles import WaffleSwitch
89
from edx_django_utils.plugins import get_plugin_apps, add_plugins
@@ -230,7 +231,6 @@
230231
TIME_ZONE = "UTC"
231232
TIME_ZONE_CLASS = timezone.utc
232233

233-
# Using zoneinfo from the standard library instead of pytz
234234
# https://docs.djangoproject.com/en/4.2/releases/4.0/#zoneinfo-default-timezone-implementation
235235

236236
USE_I18N = True
@@ -461,14 +461,25 @@
461461
FILE_STORAGE_BACKEND = {}
462462
EXTRA_APPS = []
463463
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
464-
STORAGES = {
465-
"default": {
466-
"BACKEND": "django.core.files.storage.FileSystemStorage",
467-
},
468-
"staticfiles": {
469-
"BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
470-
},
471-
}
464+
465+
DEFAULT_FILE_STORAGE_BACKEND = "django.core.files.storage.FileSystemStorage"
466+
STATICFILES_STORAGE_BACKEND = "django.contrib.staticfiles.storage.StaticFilesStorage"
467+
468+
if django.VERSION >= (4, 2):
469+
# Use new STORAGES setting (available from Django 4.2)
470+
STORAGES = {
471+
"default": {
472+
"BACKEND": DEFAULT_FILE_STORAGE_BACKEND,
473+
},
474+
"staticfiles": {
475+
"BACKEND": STATICFILES_STORAGE_BACKEND,
476+
},
477+
}
478+
else:
479+
# For Django < 4.2 or packages still using legacy settings
480+
DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE_BACKEND
481+
STATICFILES_STORAGE = STATICFILES_STORAGE_BACKEND
482+
472483
CACHES = {
473484
"default": {
474485
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",

credentials/settings/test.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,25 @@
3535

3636
# Local Directories
3737
TEST_ROOT = path("test_root")
38-
STORAGES = {
39-
**STORAGES,
40-
"default": {
41-
"BACKEND": "django.core.files.storage.FileSystemStorage",
42-
},
43-
"staticfiles": {
44-
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
45-
},
46-
}
38+
39+
DEFAULT_FILE_STORAGE_BACKEND = "django.core.files.storage.FileSystemStorage"
40+
STATICFILES_STORAGE_BACKEND = "django.contrib.staticfiles.storage.StaticFilesStorage"
41+
42+
if django.VERSION >= (4, 2):
43+
# Use new STORAGES setting (available from Django 4.2)
44+
STORAGES = {
45+
"default": {
46+
"BACKEND": DEFAULT_FILE_STORAGE_BACKEND,
47+
},
48+
"staticfiles": {
49+
"BACKEND": STATICFILES_STORAGE_BACKEND,
50+
},
51+
}
52+
else:
53+
# For Django < 4.2 or packages still using legacy settings
54+
DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE_BACKEND
55+
STATICFILES_STORAGE = STATICFILES_STORAGE_BACKEND
56+
4757
MEDIA_ROOT = str(TEST_ROOT / "uploads")
4858
MEDIA_URL = "/static/uploads/"
4959

requirements/base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ python-dateutil==2.9.0.post0
330330
python-memcached==1.59
331331
# via -r requirements/base.in
332332
python-slugify==8.0.4
333-
# via -r requirements/base.in
333+
# via code-annotations
334334
python3-openid==3.2.0
335335
# via social-auth-core
336336
qrcode==7.4.2

0 commit comments

Comments
 (0)