Code

Ticket #10258: 10258-on-9160.diff

File 10258-on-9160.diff, 4.7 KB (added by Thilo, 5 years ago)

Patch that applies when patch from #9610 has been applied before. Apart from that its identical to 10258.2.diff

Line 
1diff -urNx '*.pyc' -x .svn trunk+9160/django/core/files/storage.py working/django/core/files/storage.py
2--- trunk+9160/django/core/files/storage.py     2009-05-07 18:23:38.000000000 +0200
3+++ working/django/core/files/storage.py        2009-05-07 17:42:09.000000000 +0200
4@@ -1,6 +1,7 @@
5 import os
6 import errno
7 import urlparse
8+import itertools
9 
10 from django.conf import settings
11 from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
12@@ -65,16 +66,19 @@
13         """
14 
15         dir_name, file_name = os.path.split(name)
16-        # If the filename already exists, keep adding an underscore to the name
17-        # of the file until the filename doesn't exist.
18-        while self.exists(name):
19+        # If the filename already exists, add a number to the filename
20+        # until an available name is found.
21+        if self.exists(name):
22             try:
23                 dot_index = file_name.rindex('.')
24             except ValueError: # filename has no dot
25-                file_name += '_'
26+                template = "%s_%%d" % (file_name,)
27             else:
28-                file_name = file_name[:dot_index] + '_' + file_name[dot_index:]
29-            name = os.path.join(dir_name, file_name)
30+                template = "%s_%%d%s" % (file_name[:dot_index], file_name[dot_index:],)
31+            # Iterate indefinitely until a filename is found.
32+            count = itertools.count(1)
33+            while self.exists(name):
34+                name = os.path.join(dir_name, template % count.next())
35         return name
36 
37     def path(self, name):
38diff -urNx '*.pyc' -x .svn trunk+9160/tests/modeltests/files/models.py working/tests/modeltests/files/models.py
39--- trunk+9160/tests/modeltests/files/models.py 2009-05-07 17:44:53.000000000 +0200
40+++ working/tests/modeltests/files/models.py    2009-05-07 17:14:45.000000000 +0200
41@@ -88,12 +88,18 @@
42 >>> '-'.join(obj1.normal.chunks(chunk_size=2))
43 'co-nt-en-t'
44 
45-# Save another file with the same name.
46+# Save more files with the same name; check for _[x] appendix.
47+
48+>>> objs = [ Storage() for i in range(10) ]
49+>>> count = 1
50+>>> for o in objs:
51+...     o.normal.save('django_test.txt', ContentFile('Ten files, same content'))
52+>>> map(lambda o: o.normal, objs)
53+[<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>]
54+>>> for o in objs: o.delete()
55 
56 >>> obj2 = Storage()
57 >>> obj2.normal.save('django_test.txt', ContentFile('more content'))
58->>> obj2.normal
59-<FieldFile: tests/django_test_.txt>
60 >>> obj2.normal.size
61 12
62 
63@@ -102,7 +108,7 @@
64 >>> cache.set('obj1', obj1)
65 >>> cache.set('obj2', obj2)
66 >>> cache.get('obj2').normal
67-<FieldFile: tests/django_test_.txt>
68+<FieldFile: tests/django_test_1.txt>
69 
70 # Deleting an object deletes the file it uses, if there are no other objects
71 # still using that file.
72@@ -110,7 +116,7 @@
73 >>> obj2.delete()
74 >>> obj2.normal.save('django_test.txt', ContentFile('more content'))
75 >>> obj2.normal
76-<FieldFile: tests/django_test_.txt>
77+<FieldFile: tests/django_test_1.txt>
78 
79 # Default values allow an object to access a single file.
80 
81diff -urNx '*.pyc' -x .svn trunk+9160/tests/regressiontests/file_storage/tests.py working/tests/regressiontests/file_storage/tests.py
82--- trunk+9160/tests/regressiontests/file_storage/tests.py      2009-05-07 18:23:38.000000000 +0200
83+++ working/tests/regressiontests/file_storage/tests.py 2009-05-07 17:43:03.000000000 +0200
84@@ -121,9 +121,9 @@
85         name = self.save_file('conflict')
86         self.thread.join()
87         self.assert_(self.storage.exists('conflict'))
88-        self.assert_(self.storage.exists('conflict_'))
89+        self.assert_(self.storage.exists('conflict_1'))
90         self.storage.delete('conflict')
91-        self.storage.delete('conflict_')
92+        self.storage.delete('conflict_1')
93 
94 class FileStoragePermissions(TestCase):
95     def setUp(self):
96@@ -141,7 +141,6 @@
97         actual_mode = os.stat(self.storage.path(name))[0] & 0777
98         self.assertEqual(actual_mode, 0666)
99 
100-
101 class FileStorageDirectoryNameParsing(TestCase):
102     """Regression test for #9610: Using a dotted pathname would mangle
103     the directory name instead of the filename if the file already
104@@ -158,4 +157,4 @@
105         self.storage.save('dotted.path/test', ContentFile("2"))
106 
107         self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
108-        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_')))
109+        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_1')))