Ticket #4339: overwrite_existing_files.diff

File overwrite_existing_files.diff, 3.3 KB (added by elaatifi@…, 12 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, overwrite=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))
    368368        except OSError: # Directory probably already exists.
    369369            pass
    370370        filename = field.get_filename(filename)
     371       
     372        if not overwrite:
     373            # If the filename already exists, keep adding an underscore to the name of
     374            # the file until the filename doesn't exist.
     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:]
    371382
    372         # If the filename already exists, keep adding an underscore to the name of
    373         # 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:]
    381 
    382383        # Write the file to disk.
    383384        setattr(self, field.attname, filename)
    384385
  • db/models/fields/__init__.py

     
    593593        return super(EmailField, self).formfield(**defaults)
    594594
    595595class FileField(Field):
    596     def __init__(self, verbose_name=None, name=None, upload_to='', **kwargs):
     596    def __init__(self, verbose_name=None, name=None, upload_to='', overwrite=False, **kwargs):
    597597        self.upload_to = upload_to
     598        self.overwrite = overwrite
    598599        Field.__init__(self, verbose_name, name, **kwargs)
    599600
    600601    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
     
    638639        setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self))
    639640        setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self))
    640641        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))
     642        setattr(cls, 'save_%s_file' % self.name, lambda instance, filename, raw_contents, save=True, overwrite=self.overwrite: instance._save_FIELD_file(self, filename, raw_contents, save, overwrite))
    642643        dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls)
    643644
    644645    def delete_file(self, instance):
Back to Top