diff --git a/django/core/files/storage/filesystem.py b/django/core/files/storage/filesystem.py index 4916a0b0e83..85fc4eff9f3 100644 --- a/django/core/files/storage/filesystem.py +++ b/django/core/files/storage/filesystem.py @@ -12,10 +12,11 @@ from django.utils.encoding import filepath_to_uri 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 @@ class FileSystemStorage(Storage): 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 00000000000..663a163beae --- /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