Skip to content

Commit 6812335

Browse files
authored
feat!: Upgrading storages with new STORAGES. (#4674)
* feat!: Upgrading storages with STORAGES.
1 parent fa03d89 commit 6812335

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import importlib
2+
import sys
3+
import textwrap
4+
5+
6+
def test_production_media_storage(monkeypatch, tmp_path):
7+
"""Test that MEDIA_STORAGE_BACKEND YAML block overrides default storage + media settings."""
8+
9+
# Create a temporary YAML file to act as DISCOVERY_CFG
10+
fake_config = tmp_path / "config.yaml"
11+
fake_yaml_content = textwrap.dedent("""
12+
MEDIA_STORAGE_BACKEND:
13+
AWS_S3_OBJECT_PARAMETERS:
14+
CacheControl: max-age=31536
15+
AWS_QUERYSTRING_AUTH: false
16+
AWS_QUERYSTRING_EXPIRE: false
17+
AWS_S3_CUSTOM_DOMAIN: cdn.org
18+
AWS_STORAGE_BUCKET_NAME: tests
19+
DEFAULT_FILE_STORAGE: storages.backends.s3boto3.S3Boto3Storage
20+
MEDIA_ROOT: media
21+
MEDIA_URL: https://cdn.org/media/
22+
""")
23+
fake_config.write_text(fake_yaml_content)
24+
25+
# Patch environment variable so production.py sees the config
26+
monkeypatch.setenv("DISCOVERY_CFG", str(fake_config))
27+
28+
# Remove production module if already imported
29+
sys.modules.pop("course_discovery.settings.production", None)
30+
# Import production settings fresh
31+
prod = importlib.import_module("course_discovery.settings.production")
32+
33+
# Assert MEDIA_STORAGE_BACKEND unpacked correctly
34+
assert "default" in prod.STORAGES
35+
assert prod.STORAGES["default"]["BACKEND"] == "storages.backends.s3boto3.S3Boto3Storage"
36+
assert prod.MEDIA_URL == "https://cdn.org/media/"
37+
assert prod.MEDIA_ROOT == "media"
38+
39+
# Assert all AWS keys are present
40+
assert prod.AWS_STORAGE_BUCKET_NAME == "tests"
41+
assert prod.AWS_S3_CUSTOM_DOMAIN == "cdn.org"
42+
assert prod.AWS_QUERYSTRING_AUTH is False
43+
assert prod.AWS_QUERYSTRING_EXPIRE is False
44+
assert prod.AWS_S3_OBJECT_PARAMETERS == {"CacheControl": "max-age=31536"}

course_discovery/settings/base.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,18 @@
580580
EMAIL_USE_TLS = False
581581
EXTRA_APPS = []
582582
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
583-
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
583+
584+
STORAGES = {
585+
"default": {
586+
"BACKEND": "django.core.files.storage.FileSystemStorage",
587+
},
588+
"staticfiles": {
589+
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
590+
,
591+
},
592+
}
593+
594+
584595
CACHES = {
585596
'default': {
586597
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
@@ -591,7 +602,7 @@
591602
}
592603
API_ROOT = None
593604
MEDIA_STORAGE_BACKEND = {
594-
'DEFAULT_FILE_STORAGE': 'django.core.files.storage.FileSystemStorage',
605+
"STORAGES": STORAGES,
595606
'MEDIA_ROOT': MEDIA_ROOT,
596607
'MEDIA_URL': MEDIA_URL
597608
}

course_discovery/settings/production.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141

4242
vars().update(config_from_yaml)
4343

44+
MEDIA_STORAGE_BACKEND = config_from_yaml.get("MEDIA_STORAGE_BACKEND", {})
45+
default_backend = MEDIA_STORAGE_BACKEND.pop("DEFAULT_FILE_STORAGE", None)
46+
static_backend = MEDIA_STORAGE_BACKEND.pop("STATICFILES_STORAGE", None)
47+
48+
if default_backend:
49+
STORAGES["default"]["BACKEND"] = default_backend
50+
51+
if static_backend:
52+
STORAGES["staticfiles"]["BACKEND"] = static_backend
53+
4454
# Unpack media storage settings.
4555
# It's important we unpack here because of https://github.com/openedx/configuration/pull/3307
4656
vars().update(MEDIA_STORAGE_BACKEND)

0 commit comments

Comments
 (0)