Load swift config as global instead of monkey path

This commit is contained in:
Arzhel Younsi 2024-06-26 15:58:40 +00:00
parent ad40f433da
commit 8256fefc93

View File

@ -206,11 +206,6 @@ DATABASES = {
if STORAGE_BACKEND is not None: if STORAGE_BACKEND is not None:
DEFAULT_FILE_STORAGE = STORAGE_BACKEND DEFAULT_FILE_STORAGE = STORAGE_BACKEND
def _setting(name, default=None):
if name in STORAGE_CONFIG:
return STORAGE_CONFIG[name]
return globals().get(name, default)
# django-storages # django-storages
if STORAGE_BACKEND.startswith('storages.'): if STORAGE_BACKEND.startswith('storages.'):
try: try:
@ -224,11 +219,14 @@ if STORAGE_BACKEND is not None:
raise e raise e
# Monkey-patch django-storages to fetch settings from STORAGE_CONFIG # Monkey-patch django-storages to fetch settings from STORAGE_CONFIG
def _setting(name, default=None):
if name in STORAGE_CONFIG:
return STORAGE_CONFIG[name]
return globals().get(name, default)
storages.utils.setting = _setting storages.utils.setting = _setting
# django-storage-swift # django-storage-swift
elif STORAGE_BACKEND == 'swift.storage.SwiftStorage': elif STORAGE_BACKEND == 'swift.storage.SwiftStorage':
try: try:
import swift.utils # type: ignore import swift.utils # type: ignore
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
@ -239,9 +237,10 @@ if STORAGE_BACKEND is not None:
) )
raise e raise e
# Monkey-patch django-storage-swift to fetch settings from STORAGE_CONFIG # Load all SWIFT_* settings from the user configuration
swift.utils.setting = _setting for param, value in STORAGE_CONFIG.items():
if param.startswith('SWIFT_'):
globals()[param] = value
if STORAGE_CONFIG and STORAGE_BACKEND is None: if STORAGE_CONFIG and STORAGE_BACKEND is None:
warnings.warn( warnings.warn(