Ticket #4339: override_existing_files_patch.diff

File override_existing_files_patch.diff, 2.5 KB (added by elaatifi@…, 18 years ago)
  • db/models/base.py

     
    361361    def _get_FIELD_size(self, field):
    362362        return os.path.getsize(self._get_FIELD_filename(field))
    363363
    364     def _save_FIELD_file(self, field, filename, raw_contents, save=True):
     364    def _save_FIELD_file(self, field, filename, raw_contents, save=True, override=False):
    365365        directory = field.get_directory_name()
    366366        try: # Create the date-based directory if it doesn't exist.
    367367            os.makedirs(os.path.join(settings.MEDIA_ROOT, directory))
     
    371371
    372372        # If the filename already exists, keep adding an underscore to the name of
    373373        # the file until the filename doesn't exist.
    374         while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)):
    375             try:
    376                 dot_index = filename.rindex('.')
    377             except ValueError: # filename has no dot
    378                 filename += '_'
    379             else:
    380                 filename = filename[:dot_index] + '_' + filename[dot_index:]
     374        if not override:
     375            while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)):
     376                try:
     377                    dot_index = filename.rindex('.')
     378                except ValueError: # filename has no dot
     379                    filename += '_'
     380                else:
     381                    filename = filename[:dot_index] + '_' + filename[dot_index:]
    381382
    382383        # Write the file to disk.
    383384        setattr(self, field.attname, filename)
  • db/models/fields/__init__.py

     
    638638        setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self))
    639639        setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self))
    640640        setattr(cls, 'get_%s_size' % self.name, curry(cls._get_FIELD_size, field=self))
    641         setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True: instance._save_FIELD_file(self, filename, raw_contents, save))
     641        setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True, override=False: instance._save_FIELD_file(self, filename, raw_contents, save, override))
    642642        dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls)
    643643
    644644    def delete_file(self, instance):
Back to Top