From 99b4f90ec6b96e8c2a9f22b360d4eb5589763034 Mon Sep 17 00:00:00 2001 From: Francesco Panico Date: Fri, 11 Nov 2022 07:04:18 +0100 Subject: [PATCH] Refs #34110 -- Added StorageSettingsMixin. --- django/core/files/storage/filesystem.py | 18 ++---------------- django/core/files/storage/mixins.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 django/core/files/storage/mixins.py diff --git a/django/core/files/storage/filesystem.py b/django/core/files/storage/filesystem.py index 4916a0b0e83f..85fc4eff9f3a 100644 --- a/django/core/files/storage/filesystem.py +++ b/django/core/files/storage/filesystem.py @@ -12,10 +12,11 @@ from django.utils.functional import cached_property from .base import Storage +from .mixins import StorageSettingsMixin @deconstructible(path="django.core.files.storage.FileSystemStorage") -class FileSystemStorage(Storage): +class FileSystemStorage(Storage, StorageSettingsMixin): """ Standard filesystem storage """ @@ -37,21 +38,6 @@ def __init__( self._directory_permissions_mode = directory_permissions_mode setting_changed.connect(self._clear_cached_properties) - def _clear_cached_properties(self, setting, **kwargs): - """Reset setting based property values.""" - if setting == "MEDIA_ROOT": - self.__dict__.pop("base_location", None) - self.__dict__.pop("location", None) - elif setting == "MEDIA_URL": - self.__dict__.pop("base_url", None) - elif setting == "FILE_UPLOAD_PERMISSIONS": - self.__dict__.pop("file_permissions_mode", None) - elif setting == "FILE_UPLOAD_DIRECTORY_PERMISSIONS": - self.__dict__.pop("directory_permissions_mode", None) - - def _value_or_setting(self, value, setting): - return setting if value is None else value - @cached_property def base_location(self): return self._value_or_setting(self._location, settings.MEDIA_ROOT) diff --git a/django/core/files/storage/mixins.py b/django/core/files/storage/mixins.py new file mode 100644 index 000000000000..663a163beae1 --- /dev/null +++ b/django/core/files/storage/mixins.py @@ -0,0 +1,15 @@ +class StorageSettingsMixin: + def _clear_cached_properties(self, setting, **kwargs): + """Reset setting based property values.""" + if setting == "MEDIA_ROOT": + self.__dict__.pop("base_location", None) + self.__dict__.pop("location", None) + elif setting == "MEDIA_URL": + self.__dict__.pop("base_url", None) + elif setting == "FILE_UPLOAD_PERMISSIONS": + self.__dict__.pop("file_permissions_mode", None) + elif setting == "FILE_UPLOAD_DIRECTORY_PERMISSIONS": + self.__dict__.pop("directory_permissions_mode", None) + + def _value_or_setting(self, value, setting): + return setting if value is None else value