~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/core/files/storage.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
from django.conf import settings
6
6
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
7
 
from django.utils.encoding import force_unicode
 
7
from django.core.files import locks, File
 
8
from django.core.files.move import file_move_safe
 
9
from django.utils.encoding import force_unicode, smart_str
 
10
from django.utils.functional import LazyObject
 
11
from django.utils.importlib import import_module
8
12
from django.utils.text import get_valid_filename
9
13
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
14
 
13
15
__all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
14
16
 
40
42
        # Get the proper name for the file, as it will actually be saved.
41
43
        if name is None:
42
44
            name = content.name
43
 
        
 
45
 
44
46
        name = self.get_available_name(name)
45
47
        name = self._save(name, content)
46
48
 
61
63
        Returns a filename that's free on the target storage system, and
62
64
        available for new content to be written to.
63
65
        """
64
 
        # If the filename already exists, keep adding an underscore to the name
65
 
        # of the file until the filename doesn't exist.
 
66
        dir_name, file_name = os.path.split(name)
 
67
        file_root, file_ext = os.path.splitext(file_name)
 
68
        # If the filename already exists, keep adding an underscore (before the
 
69
        # file extension, if one exists) to the filename until the generated
 
70
        # filename doesn't exist.
66
71
        while self.exists(name):
67
 
            try:
68
 
                dot_index = name.rindex('.')
69
 
            except ValueError: # filename has no dot
70
 
                name += '_'
71
 
            else:
72
 
                name = name[:dot_index] + '_' + name[dot_index:]
 
72
            file_root += '_'
 
73
            # file_ext includes the dot.
 
74
            name = os.path.join(dir_name, file_root + file_ext)
73
75
        return name
74
76
 
75
77
    def path(self, name):
116
118
        """
117
119
        raise NotImplementedError()
118
120
 
 
121
    # Needed by django.utils.functional.LazyObject (via DefaultStorage).
 
122
    def get_all_members(self):
 
123
        return self.__members__
 
124
 
119
125
class FileSystemStorage(Storage):
120
126
    """
121
127
    Standard filesystem storage
122
128
    """
123
129
 
124
 
    def __init__(self, location=settings.MEDIA_ROOT, base_url=settings.MEDIA_URL):
 
130
    def __init__(self, location=None, base_url=None):
 
131
        if location is None:
 
132
            location = settings.MEDIA_ROOT
 
133
        if base_url is None:
 
134
            base_url = settings.MEDIA_URL
125
135
        self.location = os.path.abspath(location)
126
136
        self.base_url = base_url
127
137
 
172
182
            else:
173
183
                # OK, the file save worked. Break out of the loop.
174
184
                break
175
 
        
 
185
 
176
186
        if settings.FILE_UPLOAD_PERMISSIONS is not None:
177
187
            os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS)
178
 
        
 
188
 
179
189
        return name
180
190
 
181
191
    def delete(self, name):
202
212
            path = safe_join(self.location, name)
203
213
        except ValueError:
204
214
            raise SuspiciousOperation("Attempted access to '%s' denied." % name)
205
 
        return os.path.normpath(path)
 
215
        return smart_str(os.path.normpath(path))
206
216
 
207
217
    def size(self, name):
208
218
        return os.path.getsize(self.path(name))
212
222
            raise ValueError("This file is not accessible via a URL.")
213
223
        return urlparse.urljoin(self.base_url, name).replace('\\', '/')
214
224
 
215
 
def get_storage_class(import_path):
 
225
def get_storage_class(import_path=None):
 
226
    if import_path is None:
 
227
        import_path = settings.DEFAULT_FILE_STORAGE
216
228
    try:
217
229
        dot = import_path.rindex('.')
218
230
    except ValueError:
219
231
        raise ImproperlyConfigured("%s isn't a storage module." % import_path)
220
232
    module, classname = import_path[:dot], import_path[dot+1:]
221
233
    try:
222
 
        mod = __import__(module, {}, {}, [''])
 
234
        mod = import_module(module)
223
235
    except ImportError, e:
224
236
        raise ImproperlyConfigured('Error importing storage module %s: "%s"' % (module, e))
225
237
    try:
227
239
    except AttributeError:
228
240
        raise ImproperlyConfigured('Storage module "%s" does not define a "%s" class.' % (module, classname))
229
241
 
230
 
DefaultStorage = get_storage_class(settings.DEFAULT_FILE_STORAGE)
 
242
class DefaultStorage(LazyObject):
 
243
    def _setup(self):
 
244
        self._wrapped = get_storage_class()()
 
245
 
231
246
default_storage = DefaultStorage()