From a08267bf6ad365f523c5c3c71ba995a3251f3a9e Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 2 Mar 2009 04:48:47 +0000 Subject: [PATCH] Removed some import-time dependencies on Django's settings. Now you can import the file storage stuff and still call settings.configure() afterwards. There is still one import-time usage of settings in django.contrib.comments, but that's unavoidable. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9946 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/files/storage.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) 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()