Ticket #5485: base.py.diff

File base.py.diff, 1.5 KB (added by emperorcezar, 17 years ago)
  • django/db/models/base.py

     
    380380            pass
    381381        filename = field.get_filename(filename)
    382382
    383         # If the filename already exists, keep adding an underscore to the name of
     383        # If the filename already exists, add an underscore and number to
    384384        # the file until the filename doesn't exist.
     385        counter = 2
    385386        while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)):
    386387            try:
    387388                dot_index = filename.rindex('.')
    388389            except ValueError: # filename has no dot
    389                 filename += '_'
     390                try:
     391                    number_index = filename.rindex('_')
     392                except ValueError:
     393                    filename += '_'+str(counter) # There was no underscore
     394                else:
     395                    filename = filename[:number_index] + '_' + str(counter)
    390396            else:
    391                 filename = filename[:dot_index] + '_' + filename[dot_index:]
     397                try:
     398                    number_index = filename.rindex('_')
     399                except ValueError:
     400                    filename = filename[:dot_index] + '_' + str(counter) + filename[dot_index:]
     401                else:
     402                    filename = filename[:number_index] + '_' + str(counter) + filename[dot_index:]
     403            counter += 1
    392404
    393405        # Write the file to disk.
    394406        setattr(self, field.attname, filename)
Back to Top