def _delete_image(self, name, instance, **kwargs): field = getattr(instance, name) if field: field.delete_thumbnails() field.storage.delete(field.path) clean_dir(os.path.dirname(field.storage.path(field)), settings.MEDIA_ROOT)
def _rename_image(self, name, instance, **kwargs): field = getattr(instance, name) old_file = getattr(instance, name + '_old') new_file = getattr(instance, name) if old_file and new_file != old_file: old_file.delete_thumbnails() src, dest, new_filename = self.__get_paths(instance, field) if src and src != dest and os.path.exists(src): if old_file: old_file.delete_thumbnails() if not os.path.exists(os.path.dirname(dest)): os.makedirs(os.path.dirname(dest)) os.rename(src, dest) setattr(instance, name, new_filename) self._store_old_value(name, instance) instance.save() if old_file and old_file != new_filename and os.path.exists( field.storage.path(old_file)): old_file.delete_thumbnails() field.storage.delete(old_file) clean_dir(os.path.dirname(field.storage.path(old_file)), settings.MEDIA_ROOT)
def delete_file(self): if self.attachment: name = self.attachment.name storage = self.attachment.storage storage.delete(name) clean_dir(os.path.dirname(storage.path(name)), settings.MEDIA_ROOT) self.attachment = ''
def _rename_image(self, name, instance, **kwargs): field = getattr(instance, name) old_file = getattr(instance, name + '_old') new_file = getattr(instance, name) if old_file and new_file != old_file: old_file.delete_thumbnails() src, dest, new_filename = self.__get_paths(instance, field) if src and src != dest and os.path.exists(src): if old_file: old_file.delete_thumbnails() if not os.path.exists(os.path.dirname(dest)): os.makedirs(os.path.dirname(dest)) os.rename(src, dest) setattr(instance, name, new_filename) self._store_old_value(name, instance) instance.save() if old_file and old_file != new_filename and os.path.exists(field.storage.path(old_file)): old_file.delete_thumbnails() field.storage.delete(old_file) clean_dir(os.path.dirname(field.storage.path(old_file)), settings.MEDIA_ROOT)