Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit a08267bf6ad365f523c5c3c71ba995a3251f3a9e 1 parent cf30712
Malcolm Tredinnick malcolmt authored
Showing with 22 additions and 8 deletions.
  1. +22 −8 django/core/files/storage.py
30 django/core/files/storage.py
View
@@ -4,11 +4,12 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
+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
-from django.core.files import locks, File
-from django.core.files.move import file_move_safe
__all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
@@ -40,7 +41,7 @@ def save(self, name, content):
# 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 @@ def url(self, name):
"""
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 @@ def _save(self, name, content):
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 @@ def url(self, name):
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()
Please sign in to comment.
Something went wrong with that request. Please try again.