Ticket #4339: override_existing_files_patch.diff
File override_existing_files_patch.diff, 2.5 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, override=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)) … … 371 371 372 372 # If the filename already exists, keep adding an underscore to the name of 373 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:] 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:] 381 382 382 383 # Write the file to disk. 383 384 setattr(self, field.attname, filename) -
db/models/fields/__init__.py
638 638 setattr(cls, 'get_%s_filename' % self.name, curry(cls._get_FIELD_filename, field=self)) 639 639 setattr(cls, 'get_%s_url' % self.name, curry(cls._get_FIELD_url, field=self)) 640 640 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)) 642 642 dispatcher.connect(self.delete_file, signal=signals.post_delete, sender=cls) 643 643 644 644 def delete_file(self, instance):