Ticket #5655: 5655.patch

File 5655.patch, 1.6 KB (added by Chris Beaven, 17 years ago)
  • django/db/models/base.py

     
    380380        return os.path.getsize(self._get_FIELD_filename(field))
    381381
    382382    def _save_FIELD_file(self, field, filename, raw_contents, save=True):
    383         directory = field.get_directory_name()
    384         try: # Create the date-based directory if it doesn't exist.
     383        filename = field.get_filename(filename)
     384
     385        # Create the directory if it doesn't exist.
     386        directory = os.path.split(filename)[0]
     387        try:
    385388            os.makedirs(os.path.join(settings.MEDIA_ROOT, directory))
    386389        except OSError: # Directory probably already exists.
    387390            pass
    388         filename = field.get_filename(filename)
    389391
    390         # If the filename already exists, keep adding an underscore to the name of
    391         # the file until the filename doesn't exist.
     392        # If the filename already exists, keep adding an underscore to the name
     393        # of the file until the filename doesn't exist.
    392394        while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)):
    393             try:
    394                 dot_index = filename.rindex('.')
    395             except ValueError: # filename has no dot
    396                 filename += '_'
    397             else:
    398                 filename = filename[:dot_index] + '_' + filename[dot_index:]
     395            filename = '%s_%s' % os.path.splitext(filename)
    399396
    400397        # Write the file to disk.
    401398        setattr(self, field.attname, filename)
Back to Top