Ticket #10258: 10258.2.diff

File 10258.2.diff, 3.9 KB (added by Thilo, 6 years ago)

Added one more doctest which uploads 10 files and checks for their names.

  • django/core/files/storage.py

     
    11import os
    22import errno
    33import urlparse
     4import itertools
    45
    56from django.conf import settings
    67from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
     
    6364        Returns a filename that's free on the target storage system, and
    6465        available for new content to be written to.
    6566        """
    66         # If the filename already exists, keep adding an underscore to the name
    67         # of the file until the filename doesn't exist.
    68         while self.exists(name):
     67        # If the filename already exists, add a number to the filename
     68        # until an available name is found.
     69        if self.exists(name):
     70            # Build a filename template out of the (existing) name
    6971            try:
    7072                dot_index = name.rindex('.')
    7173            except ValueError: # filename has no dot
    72                 name += '_'
     74                template = "%s_%%d" % (name,)
    7375            else:
    74                 name = name[:dot_index] + '_' + name[dot_index:]
     76                template = "%s_%%d%s" % (name[:dot_index], name[dot_index:],)
     77            # Iterate indefinitely until a filename is found.
     78            count = itertools.count(1)
     79            while self.exists(name):
     80                name = template % count.next()
    7581        return name
    7682
    7783    def path(self, name):
  • tests/modeltests/files/models.py

     
    8888>>> '-'.join(obj1.normal.chunks(chunk_size=2))
    8989'co-nt-en-t'
    9090
    91 # Save another file with the same name.
     91# Save more files with the same name; check for _[x] appendix.
    9292
     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()
     100
    93101>>> obj2 = Storage()
    94102>>> obj2.normal.save('django_test.txt', ContentFile('more content'))
    95 >>> obj2.normal
    96 <FieldFile: tests/django_test_.txt>
    97103>>> obj2.normal.size
    9810412
    99105
     
    102108>>> cache.set('obj1', obj1)
    103109>>> cache.set('obj2', obj2)
    104110>>> cache.get('obj2').normal
    105 <FieldFile: tests/django_test_.txt>
     111<FieldFile: tests/django_test_1.txt>
    106112
    107113# Deleting an object deletes the file it uses, if there are no other objects
    108114# still using that file.
     
    110116>>> obj2.delete()
    111117>>> obj2.normal.save('django_test.txt', ContentFile('more content'))
    112118>>> obj2.normal
    113 <FieldFile: tests/django_test_.txt>
     119<FieldFile: tests/django_test_1.txt>
    114120
    115121# Default values allow an object to access a single file.
    116122
  • tests/regressiontests/file_storage/tests.py

     
    121121        name = self.save_file('conflict')
    122122        self.thread.join()
    123123        self.assert_(self.storage.exists('conflict'))
    124         self.assert_(self.storage.exists('conflict_'))
     124        self.assert_(self.storage.exists('conflict_1'))
    125125        self.storage.delete('conflict')
    126         self.storage.delete('conflict_')
     126        self.storage.delete('conflict_1')
    127127
    128128class FileStoragePermissions(TestCase):
    129129    def setUp(self):
Back to Top