-
Notifications
You must be signed in to change notification settings - Fork 80
fix: Django52 use STORAGES instead of DEFAULT_FILE_STORAGE and STATICFILES_STORAGE #2844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7ab9ce3
2ae19b9
475f37b
e403c34
3d3bd4d
98dd8e0
4328721
4743ef3
1f49a22
ef8a1e9
36022e0
dd3301a
1408832
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -456,7 +456,14 @@ | |||||||||
| FILE_STORAGE_BACKEND = {} | ||||||||||
| EXTRA_APPS = [] | ||||||||||
| SESSION_EXPIRE_AT_BROWSER_CLOSE = False | ||||||||||
| STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage" | ||||||||||
| STORAGES = { | ||||||||||
| "default": { | ||||||||||
| "BACKEND": "django.core.files.storage.FileSystemStorage", | ||||||||||
| }, | ||||||||||
| "staticfiles": { | ||||||||||
| "BACKEND": "django.contrib.staticfiles.storage.ManifestStaticFilesStorage", | ||||||||||
| }, | ||||||||||
| } | ||||||||||
| CACHES = { | ||||||||||
| "default": { | ||||||||||
| "BACKEND": "django.core.cache.backends.locmem.LocMemCache", | ||||||||||
|
|
@@ -474,7 +481,9 @@ | |||||||||
| } | ||||||||||
| API_ROOT = None | ||||||||||
| MEDIA_STORAGE_BACKEND = { | ||||||||||
| "DEFAULT_FILE_STORAGE": "django.core.files.storage.FileSystemStorage", | ||||||||||
| "STORAGES": { | ||||||||||
| "default": {"BACKEND": STORAGES["default"]["BACKEND"]}, | ||||||||||
| }, | ||||||||||
|
||||||||||
| "STORAGES": { | |
| "default": {"BACKEND": STORAGES["default"]["BACKEND"]}, | |
| }, | |
| "STORAGES": STORAGES |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,10 +43,16 @@ | |
| EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend" | ||
| EMAIL_FILE_PATH = "/tmp/credentials-emails" | ||
|
|
||
| DEFAULT_FILE_STORAGE = os.environ.get("DEFAULT_FILE_STORAGE", "django.core.files.storage.FileSystemStorage") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defaultfile_storage = os.environ.get("DEFAULT_FILE_STORAGE", "django.core.files.storage.FileSystemStorage")
if defaultfile_storage:
STORAGES["default"]["BACKEND"] = defaultfile_storage |
||
| MEDIA_URL = os.environ.get("MEDIA_URL", "/media/") | ||
| STORAGES = { | ||
| "default": { | ||
| "BACKEND": "django.core.files.storage.FileSystemStorage", | ||
| }, | ||
| "staticfiles": { | ||
| "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", | ||
| }, | ||
| } | ||
|
||
|
|
||
| STATICFILES_STORAGE = os.environ.get("STATICFILES_STORAGE", "django.contrib.staticfiles.storage.StaticFilesStorage") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. staticfiles_storage = os.environ.get('STATICFILES_STORAGE', 'django.contrib.staticfiles.storage.StaticFilesStorage')
if staticfiles_storage:
STORAGES["staticfiles"]["BACKEND"] = staticfiles_storage |
||
| MEDIA_URL = os.environ.get("MEDIA_URL", "/media/") | ||
| STATIC_URL = os.environ.get("STATIC_URL", "/static/") | ||
|
|
||
| # OAuth2 variables specific to social-auth/SSO login use case. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,10 +42,32 @@ | |
| if value: | ||
| vars()[key].update(value) | ||
|
|
||
| if "DEFAULT_FILE_STORAGE" in config_from_yaml: | ||
| STORAGES["default"] = { | ||
| "BACKEND": config_from_yaml.pop("DEFAULT_FILE_STORAGE"), | ||
| } | ||
|
|
||
| if "STATICFILES_STORAGE" in config_from_yaml: | ||
| STORAGES["staticfiles"] = { | ||
| "BACKEND": config_from_yaml.pop("STATICFILES_STORAGE"), | ||
| } | ||
|
|
||
| vars().update(config_from_yaml) | ||
|
|
||
| # Load the files storage backend settings for django storages | ||
| vars().update(FILE_STORAGE_BACKEND) | ||
| if "DEFAULT_FILE_STORAGE" in FILE_STORAGE_BACKEND: | ||
| default_file_storage = FILE_STORAGE_BACKEND.pop("DEFAULT_FILE_STORAGE") | ||
| vars().update(FILE_STORAGE_BACKEND) | ||
|
||
|
|
||
| FILE_STORAGE_BACKEND.update( | ||
| { | ||
| "STORAGES": { | ||
| "default": {"BACKEND": default_file_storage}, | ||
| } | ||
| } | ||
| ) | ||
| else: | ||
| vars().update(FILE_STORAGE_BACKEND) | ||
|
|
||
| # make sure this happens after the configuration file overrides so format string can be overridden | ||
| LOGGING = get_logger_config(format_string=LOGGING_FORMAT_STRING) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,16 @@ | |
|
|
||
| # Local Directories | ||
| TEST_ROOT = path("test_root") | ||
| DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage" | ||
|
|
||
| STORAGES = { | ||
| "default": { | ||
| "BACKEND": "django.core.files.storage.FileSystemStorage", | ||
| }, | ||
| "staticfiles": { | ||
| "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", | ||
| }, | ||
| } | ||
|
|
||
|
||
| MEDIA_ROOT = str(TEST_ROOT / "uploads") | ||
| MEDIA_URL = "/static/uploads/" | ||
|
|
||
|
|
@@ -49,7 +58,6 @@ | |
| "JWT_AUDIENCE": SOCIAL_AUTH_EDX_OAUTH2_KEY, | ||
| } | ||
| ) | ||
| STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage" | ||
|
|
||
| # Verifiable Credentials | ||
| ENABLE_VERIFIABLE_CREDENTIALS = True | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import builtins | ||
| import importlib | ||
| import io | ||
| import sys | ||
| import types | ||
|
|
||
|
|
||
| def test_production_storage_from_yaml(monkeypatch): | ||
| """Test that YAML config correctly populates STORAGES without leaking globals.""" | ||
|
|
||
| def fake_get_env_setting(key): | ||
| if key == "CREDENTIALS_CFG": | ||
| return "/fake/path/config.yaml" | ||
| return "" | ||
|
|
||
| class FakeSettingsType: | ||
| PRODUCTION = "production" | ||
|
|
||
| fake_yaml_content = """ | ||
| DEFAULT_FILE_STORAGE: storages.backends.s3boto3.S3Boto3Storage | ||
| STATICFILES_STORAGE: storage.ManifestStaticFilesStorage | ||
| MEDIA_ROOT: /tmp/media | ||
| MEDIA_URL: /media/ | ||
| """ | ||
|
|
||
| sys.modules.pop("credentials.settings.production", None) | ||
|
|
||
| monkeypatch.setitem( | ||
| sys.modules, | ||
| "credentials.settings.utils", | ||
| types.SimpleNamespace( | ||
| get_env_setting=fake_get_env_setting, | ||
| get_logger_config=lambda *a, **kw: {}, | ||
| ), | ||
| ) | ||
| monkeypatch.setitem( | ||
| sys.modules, | ||
| "credentials.apps.plugins.constants", | ||
| types.SimpleNamespace( | ||
| PROJECT_TYPE="credentials.djangoapp", | ||
| SettingsType=FakeSettingsType, | ||
| ), | ||
| ) | ||
| monkeypatch.setitem( | ||
| sys.modules, | ||
| "edx_django_utils.plugins", | ||
| types.SimpleNamespace(add_plugins=lambda *a, **kw: None), | ||
| ) | ||
|
|
||
| monkeypatch.setattr(builtins, "open", lambda *a, **kw: io.StringIO(fake_yaml_content)) | ||
|
|
||
| prod = importlib.import_module("credentials.settings.production") | ||
|
|
||
| assert not hasattr(prod, "DEFAULT_FILE_STORAGE") | ||
| assert not hasattr(prod, "STATICFILES_STORAGE") | ||
|
|
||
| assert "default" in prod.STORAGES | ||
| assert prod.STORAGES["default"]["BACKEND"] == "storages.backends.s3boto3.S3Boto3Storage" | ||
| assert "staticfiles" in prod.STORAGES | ||
| assert prod.STORAGES["staticfiles"]["BACKEND"] == "storage.ManifestStaticFilesStorage" |
Uh oh!
There was an error while loading. Please reload this page.