Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored March 02, 2009

Showing 1 changed file with 22 additions and 8 deletions. Show diff stats Hide diff stats

  1. 30  django/core/files/storage.py
30  django/core/files/storage.py
@@ -4,11 +4,12 @@
4 4
 
5 5
 from django.conf import settings
6 6
 from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
  7
+from django.core.files import locks, File
  8
+from django.core.files.move import file_move_safe
7 9
 from django.utils.encoding import force_unicode
  10
+from django.utils.functional import LazyObject
8 11
 from django.utils.text import get_valid_filename
9 12
 from django.utils._os import safe_join
10  
-from django.core.files import locks, File
11  
-from django.core.files.move import file_move_safe
12 13
 
13 14
 __all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
14 15
 
@@ -40,7 +41,7 @@ def save(self, name, content):
40 41
         # Get the proper name for the file, as it will actually be saved.
41 42
         if name is None:
42 43
             name = content.name
43  
-        
  44
+
44 45
         name = self.get_available_name(name)
45 46
         name = self._save(name, content)
46 47
 
@@ -116,12 +117,20 @@ def url(self, name):
116 117
         """
117 118
         raise NotImplementedError()
118 119
 
  120
+    # Needed by django.utils.functional.LazyObject (via DefaultStorage).
  121
+    def get_all_members(self):
  122
+        return self.__members__
  123
+
119 124
 class FileSystemStorage(Storage):
120 125
     """
121 126
     Standard filesystem storage
122 127
     """
123 128
 
124  
-    def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL):
  129
+    def __init__(self, location=None, base_url=None):
  130
+        if location is None:
  131
+            location = settings.MEDIA_ROOT
  132
+        if base_url is None:
  133
+            base_url = settings.MEDIA_URL
125 134
         self.location = os.path.abspath(location)
126 135
         self.base_url = base_url
127 136
 
@@ -172,10 +181,10 @@ def _save(self, name, content):
172 181
             else:
173 182
                 # OK, the file save worked. Break out of the loop.
174 183
                 break
175  
-        
  184
+
176 185
         if settings.FILE_UPLOAD_PERMISSIONS is not None:
177 186
             os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS)
178  
-        
  187
+
179 188
         return name
180 189
 
181 190
     def delete(self, name):
@@ -212,7 +221,9 @@ def url(self, name):
212 221
             raise ValueError("This file is not accessible via a URL.")
213 222
         return urlparse.urljoin(self.base_url, name).replace('\\', '/')
214 223
 
215  
-def get_storage_class(import_path):
  224
+def get_storage_class(import_path=None):
  225
+    if import_path is None:
  226
+        import_path = settings.DEFAULT_FILE_STORAGE
216 227
     try:
217 228
         dot = import_path.rindex('.')
218 229
     except ValueError:
@@ -227,5 +238,8 @@ def get_storage_class(import_path):
227 238
     except AttributeError:
228 239
         raise ImproperlyConfigured('Storage module "%s" does not define a "%s" class.' % (module, classname))
229 240
 
230  
-DefaultStorage = get_storage_class(settings.DEFAULT_FILE_STORAGE)
  241
+class DefaultStorage(LazyObject):
  242
+    def _setup(self):
  243
+        self._wrapped = get_storage_class()()
  244
+
231 245
 default_storage = DefaultStorage()

0 notes on commit a08267b

Please sign in to comment.
Something went wrong with that request. Please try again.