Skip to content

Commit

Permalink
Refs #34110 -- Added StorageSettingsMixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
panicofr authored and felixxm committed Nov 11, 2022
1 parent 032c09c commit 99b4f90
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
18 changes: 2 additions & 16 deletions django/core/files/storage/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions django/core/files/storage/mixins.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 99b4f90

Please sign in to comment.