diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 8451485915..a1b843d5a1 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -4,11 +4,12 @@ import urlparse from django.conf import settings from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation -from django.utils.encoding import force_unicode -from django.utils.text import get_valid_filename -from django.utils._os import safe_join from django.core.files import locks, File from django.core.files.move import file_move_safe +from django.utils.encoding import force_unicode +from django.utils.functional import LazyObject +from django.utils.text import get_valid_filename +from django.utils._os import safe_join __all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage') @@ -40,7 +41,7 @@ class Storage(object): # Get the proper name for the file, as it will actually be saved. if name is None: name = content.name - + name = self.get_available_name(name) name = self._save(name, content) @@ -116,12 +117,20 @@ class Storage(object): """ raise NotImplementedError() + # Needed by django.utils.functional.LazyObject (via DefaultStorage). + def get_all_members(self): + return self.__members__ + class FileSystemStorage(Storage): """ Standard filesystem storage """ - def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL): + def __init__(self, location=None, base_url=None): + if location is None: + location = settings.MEDIA_ROOT + if base_url is None: + base_url = settings.MEDIA_URL self.location = os.path.abspath(location) self.base_url = base_url @@ -172,10 +181,10 @@ class FileSystemStorage(Storage): else: # OK, the file save worked. Break out of the loop. break - + if settings.FILE_UPLOAD_PERMISSIONS is not None: os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS) - + return name def delete(self, name): @@ -212,7 +221,9 @@ class FileSystemStorage(Storage): raise ValueError("This file is not accessible via a URL.") return urlparse.urljoin(self.base_url, name).replace('\\', '/') -def get_storage_class(import_path): +def get_storage_class(import_path=None): + if import_path is None: + import_path = settings.DEFAULT_FILE_STORAGE try: dot = import_path.rindex('.') except ValueError: @@ -227,5 +238,8 @@ def get_storage_class(import_path): except AttributeError: raise ImproperlyConfigured('Storage module "%s" does not define a "%s" class.' % (module, classname)) -DefaultStorage = get_storage_class(settings.DEFAULT_FILE_STORAGE) +class DefaultStorage(LazyObject): + def _setup(self): + self._wrapped = get_storage_class()() + default_storage = DefaultStorage()