-
Notifications
You must be signed in to change notification settings - Fork 115
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
[PULP-210] Update deprecated django storage config #6058
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Marked django's `DEFAULT_FILE_STORAGE` and `STATIC_FILE_STORAGE` settings to be | ||
removed in pulpcore 3.85. Users should upgrade to use the | ||
[`STORAGES`](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES) | ||
setting instead. | ||
|
||
The [django-upgrade](https://github.com/adamchainz/django-upgrade?tab=readme-ov-file#django-42) | ||
tool can handle simple cases. If cloud storages are being used, refer to django-storages | ||
to adapt their specific storage options. E.g: | ||
|
||
* <https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html> | ||
* <https://django-storages.readthedocs.io/en/latest/backends/azure.html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Started using `settings.STORAGES` internally instead of `settings.DEFAULT_FILE_STORAGE` and `settings.STATICFILES_STORAGE`, | ||
which was deprecated in Django 4.2. | ||
|
||
For compatibility, plugins must replace access to: | ||
|
||
* `settings.DEFAULT_FILE_STORAGE` with `settings.STORAGES["default"]["BACKEND"]` | ||
* `settings.STATICFILES_STORAGE` with `settings.STORAGES["staticfiles"]["BACKEND"]` | ||
|
||
See the new storage structure in [Django docs](https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-STORAGES). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
from pathlib import Path | ||
|
||
from cryptography.fernet import Fernet | ||
from django.core.files.storage import storages | ||
from django.conf import global_settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.db import connection | ||
|
||
|
@@ -56,7 +58,28 @@ | |
STATIC_URL = "/assets/" | ||
STATIC_ROOT = DEPLOY_ROOT / STATIC_URL.strip("/") | ||
|
||
DEFAULT_FILE_STORAGE = "pulpcore.app.models.storage.FileSystem" | ||
# begin compatilibity layer for DEFAULT_FILE_STORAGE | ||
# Remove on pulpcore=3.85 or pulpcore=4.0 | ||
|
||
# - What is this? | ||
# We shouldnt use STORAGES or DEFAULT_FILE_STORAGE directly because those are | ||
# mutually exclusive by django, which constraints users to use whatever we use. | ||
# This is a hack/workaround to set Pulp's default while still enabling users to choose | ||
# the legacy or the new storage setting. | ||
_DEFAULT_FILE_STORAGE = "pulpcore.app.models.storage.FileSystem" | ||
_STORAGES = { | ||
"default": { | ||
"BACKEND": "pulpcore.app.models.storage.FileSystem", | ||
}, | ||
"staticfiles": { | ||
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", | ||
}, | ||
} | ||
|
||
setattr(global_settings, "DEFAULT_FILE_STORAGE", _DEFAULT_FILE_STORAGE) | ||
setattr(global_settings, "STORAGES", _STORAGES) | ||
Comment on lines
+79
to
+80
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. Setting them both here seems to contradict that they are, as you say, "mutually exclusive". 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. The "global setting" settings is the only place where django won't complain about exclusivity. It will perform this check on a different context. 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. Sounds weird, but I'll take your word that it works. |
||
# end DEFAULT_FILE_STORAGE deprecation layer | ||
|
||
REDIRECT_TO_OBJECT_STORAGE = True | ||
|
||
WORKING_DIRECTORY = DEPLOY_ROOT / "tmp" | ||
|
@@ -371,16 +394,18 @@ | |
from dynaconf import DjangoDynaconf, Validator # noqa | ||
|
||
# Validators | ||
storage_keys = ("STORAGES.default.BACKEND", "DEFAULT_FILE_STORAGE") | ||
storage_validator = ( | ||
Validator("REDIRECT_TO_OBJECT_STORAGE", eq=False) | ||
| Validator("DEFAULT_FILE_STORAGE", eq="pulpcore.app.models.storage.FileSystem") | ||
| Validator("DEFAULT_FILE_STORAGE", eq="storages.backends.azure_storage.AzureStorage") | ||
| Validator("DEFAULT_FILE_STORAGE", eq="storages.backends.s3boto3.S3Boto3Storage") | ||
| Validator("DEFAULT_FILE_STORAGE", eq="storages.backends.gcloud.GoogleCloudStorage") | ||
| Validator(*storage_keys, eq="pulpcore.app.models.storage.FileSystem") | ||
| Validator(*storage_keys, eq="storages.backends.azure_storage.AzureStorage") | ||
| Validator(*storage_keys, eq="storages.backends.s3boto3.S3Boto3Storage") | ||
| Validator(*storage_keys, eq="storages.backends.gcloud.GoogleCloudStorage") | ||
) | ||
storage_validator.messages["combined"] = ( | ||
"'REDIRECT_TO_OBJECT_STORAGE=True' is only supported with the local file, S3, GCP or Azure" | ||
"storage backend configured in DEFAULT_FILE_STORAGE." | ||
"'REDIRECT_TO_OBJECT_STORAGE=True' is only supported with the local file, S3, GCP or Azure " | ||
"storage backend configured in STORAGES['default']['BACKEND'] " | ||
"(deprecated DEFAULT_FILE_STORAGE)." | ||
) | ||
|
||
cache_enabled_validator = Validator("CACHE_ENABLED", eq=True) | ||
|
@@ -485,7 +510,14 @@ def otel_middleware_hook(settings): | |
], | ||
post_hooks=otel_middleware_hook, | ||
) | ||
# HERE ENDS DYNACONF EXTENSION LOAD (No more code below this line) | ||
|
||
# begin compatilibity layer for DEFAULT_FILE_STORAGE | ||
# Remove on pulpcore=3.85 or pulpcore=4.0 | ||
|
||
# Ensures the cached property storage.backends uses the the right value | ||
storages._backends = settings.STORAGES.copy() | ||
storages.backends | ||
Comment on lines
+518
to
+519
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. Is this the "magic" to prevent django storages to see both items we set before? 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. No, Django in fact has both set in their default Line 518 does exactly what they do here. All this magic is to give users a larger time span to upgrade. |
||
# end compatibility layer | ||
|
||
_logger = getLogger(__name__) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh dear, this test was mixing both "imports"...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was