diff -urNx '*.pyc' -x .svn trunk+9160/django/core/files/storage.py working/django/core/files/storage.py
old
|
new
|
|
1 | 1 | import os |
2 | 2 | import errno |
3 | 3 | import urlparse |
| 4 | import itertools |
4 | 5 | |
5 | 6 | from django.conf import settings |
6 | 7 | from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation |
… |
… |
|
65 | 66 | """ |
66 | 67 | |
67 | 68 | dir_name, file_name = os.path.split(name) |
68 | | # If the filename already exists, keep adding an underscore to the name |
69 | | # of the file until the filename doesn't exist. |
70 | | while self.exists(name): |
| 69 | # If the filename already exists, add a number to the filename |
| 70 | # until an available name is found. |
| 71 | if self.exists(name): |
71 | 72 | try: |
72 | 73 | dot_index = file_name.rindex('.') |
73 | 74 | except ValueError: # filename has no dot |
74 | | file_name += '_' |
| 75 | template = "%s_%%d" % (file_name,) |
75 | 76 | else: |
76 | | file_name = file_name[:dot_index] + '_' + file_name[dot_index:] |
77 | | name = os.path.join(dir_name, file_name) |
| 77 | template = "%s_%%d%s" % (file_name[:dot_index], file_name[dot_index:],) |
| 78 | # Iterate indefinitely until a filename is found. |
| 79 | count = itertools.count(1) |
| 80 | while self.exists(name): |
| 81 | name = os.path.join(dir_name, template % count.next()) |
78 | 82 | return name |
79 | 83 | |
80 | 84 | def path(self, name): |
diff -urNx '*.pyc' -x .svn trunk+9160/tests/modeltests/files/models.py working/tests/modeltests/files/models.py
old
|
new
|
|
88 | 88 | >>> '-'.join(obj1.normal.chunks(chunk_size=2)) |
89 | 89 | 'co-nt-en-t' |
90 | 90 | |
91 | | # Save another file with the same name. |
| 91 | # Save more files with the same name; check for _[x] appendix. |
| 92 | |
| 93 | >>> objs = [ Storage() for i in range(10) ] |
| 94 | >>> count = 1 |
| 95 | >>> for o in objs: |
| 96 | ... o.normal.save('django_test.txt', ContentFile('Ten files, same content')) |
| 97 | >>> map(lambda o: o.normal, objs) |
| 98 | [<FieldFile: tests/django_test_1.txt>, <FieldFile: tests/django_test_2.txt>, <FieldFile: tests/django_test_3.txt>, <FieldFile: tests/django_test_4.txt>, <FieldFile: tests/django_test_5.txt>, <FieldFile: tests/django_test_6.txt>, <FieldFile: tests/django_test_7.txt>, <FieldFile: tests/django_test_8.txt>, <FieldFile: tests/django_test_9.txt>, <FieldFile: tests/django_test_10.txt>] |
| 99 | >>> for o in objs: o.delete() |
92 | 100 | |
93 | 101 | >>> obj2 = Storage() |
94 | 102 | >>> obj2.normal.save('django_test.txt', ContentFile('more content')) |
95 | | >>> obj2.normal |
96 | | <FieldFile: tests/django_test_.txt> |
97 | 103 | >>> obj2.normal.size |
98 | 104 | 12 |
99 | 105 | |
… |
… |
|
102 | 108 | >>> cache.set('obj1', obj1) |
103 | 109 | >>> cache.set('obj2', obj2) |
104 | 110 | >>> cache.get('obj2').normal |
105 | | <FieldFile: tests/django_test_.txt> |
| 111 | <FieldFile: tests/django_test_1.txt> |
106 | 112 | |
107 | 113 | # Deleting an object deletes the file it uses, if there are no other objects |
108 | 114 | # still using that file. |
… |
… |
|
110 | 116 | >>> obj2.delete() |
111 | 117 | >>> obj2.normal.save('django_test.txt', ContentFile('more content')) |
112 | 118 | >>> obj2.normal |
113 | | <FieldFile: tests/django_test_.txt> |
| 119 | <FieldFile: tests/django_test_1.txt> |
114 | 120 | |
115 | 121 | # Default values allow an object to access a single file. |
116 | 122 | |
diff -urNx '*.pyc' -x .svn trunk+9160/tests/regressiontests/file_storage/tests.py working/tests/regressiontests/file_storage/tests.py
old
|
new
|
|
121 | 121 | name = self.save_file('conflict') |
122 | 122 | self.thread.join() |
123 | 123 | self.assert_(self.storage.exists('conflict')) |
124 | | self.assert_(self.storage.exists('conflict_')) |
| 124 | self.assert_(self.storage.exists('conflict_1')) |
125 | 125 | self.storage.delete('conflict') |
126 | | self.storage.delete('conflict_') |
| 126 | self.storage.delete('conflict_1') |
127 | 127 | |
128 | 128 | class FileStoragePermissions(TestCase): |
129 | 129 | def setUp(self): |
… |
… |
|
141 | 141 | actual_mode = os.stat(self.storage.path(name))[0] & 0777 |
142 | 142 | self.assertEqual(actual_mode, 0666) |
143 | 143 | |
144 | | |
145 | 144 | class FileStorageDirectoryNameParsing(TestCase): |
146 | 145 | """Regression test for #9610: Using a dotted pathname would mangle |
147 | 146 | the directory name instead of the filename if the file already |
… |
… |
|
158 | 157 | self.storage.save('dotted.path/test', ContentFile("2")) |
159 | 158 | |
160 | 159 | self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path'))) |
161 | | self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_'))) |
| 160 | self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_1'))) |