Ticket #4339: overwrite_existing_files.diff
File overwrite_existing_files.diff, 3.3 KB (added by , 18 years ago) |
---|
-
db/models/base.py
361 361 def _get_FIELD_size(self, field): 362 362 return os.path.getsize(self._get_FIELD_filename(field)) 363 363 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): 365 365 directory = field.get_directory_name() 366 366 try: # Create the date-based directory if it doesn't exist. 367 367 os.makedirs(os.path.join(settings.MEDIA_ROOT, directory)) 368 368 except OSError: # Directory probably already exists. 369 369 pass 370 370 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:] 371 382 372 # If the filename already exists, keep adding an underscore to the name of373 # 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 dot378 filename += '_'379 else:380 filename = filename[:dot_index] + '_' + filename[dot_index:]381 382 383 # Write the file to disk. 383 384 setattr(self, field.attname, filename) 384 385 -
db/models/fields/__init__.py
593 593 return super(EmailField, self).formfield(**defaults) 594 594 595 595 class 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): 597 597 self.upload_to = upload_to 598 self.overwrite = overwrite 598 599 Field.__init__(self, verbose_name, name, **kwargs) 599 600 600 601 def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True): … … 638 639 setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self)) 639 640 setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self)) 640 641 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)) 642 643 dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls) 643 644 644 645 def delete_file(self, instance):