diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2010-02-23 22:39:22 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2010-02-23 22:39:22 +0000 |
| commit | 5366aa96fef0ce55fc425cf273c7debe74d99305 (patch) | |
| tree | af4e584bfc0fcdc4b7bb8e86d1f398e41e01d169 /django | |
| parent | 43b47a87d3ae075937b7dc181f8284576e5d73ac (diff) | |
Fixed #10258: handle duplicate file names better.
Instead of just continually appending "_" to duplicate file names, Django's
default storage now appends `_1`, `_2`, `_3`, etc.
Thanks to ianschenck and Thilo.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12552 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/files/storage.py | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 96c0b54623..d312681c28 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -1,6 +1,7 @@ import os import errno import urlparse +import itertools from django.conf import settings from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation @@ -65,13 +66,14 @@ class Storage(object): """ dir_name, file_name = os.path.split(name) file_root, file_ext = os.path.splitext(file_name) - # If the filename already exists, keep adding an underscore (before the - # file extension, if one exists) to the filename until the generated + # If the filename already exists, add an underscore and a number (before + # the file extension, if one exists) to the filename until the generated # filename doesn't exist. + count = itertools.count(1) while self.exists(name): - file_root += '_' # file_ext includes the dot. - name = os.path.join(dir_name, file_root + file_ext) + name = os.path.join(dir_name, "%s_%s%s" % (file_root, count.next(), file_ext)) + return name def path(self, name): |
