def test_delete_thumbnails(self): metadata_backend = metadata.get_backend() get_thumbnail = metadata_backend.get_thumbnail instance = TestModel.objects.create() with open('thumbnails/tests/tests.png', 'rb') as image_file: instance.avatar = File(image_file) instance.save() with open('thumbnails/tests/tests.png', 'rb') as image_file: instance.profile_picture = File(image_file) instance.save() instance = TestModel.objects.get(id=instance.id) instance.avatar.thumbnails.create(size='small') instance.avatar.thumbnails.create(size='large') instance.profile_picture.thumbnails.create(size='small') instance.profile_picture.thumbnails.create(size='large') self.assertTrue(get_thumbnail(instance.avatar.name, 'small')) self.assertTrue(get_thumbnail(instance.avatar.name, 'large')) self.assertTrue(get_thumbnail(instance.profile_picture.name, 'small')) self.assertTrue(get_thumbnail(instance.profile_picture.name, 'large')) call_command('delete_thumbnails', path_to_model='tests.TestModel', field_name='avatar', size='small') # Ensure only the relevant thumbnails are deleted self.assertFalse(get_thumbnail(instance.avatar.name, 'small')) self.assertTrue(get_thumbnail(instance.avatar.name, 'large')) self.assertTrue(get_thumbnail(instance.profile_picture.name, 'small')) self.assertTrue(get_thumbnail(instance.profile_picture.name, 'large'))
def handle(self, path_to_model, field_name, size, *args, **kwargs): if not path_to_model: raise ValueError('--model argument is required') if not field_name: raise ValueError('--field argument is required') if not size: raise ValueError('--size argument is required') app_label, model_name = path_to_model.rsplit('.', 1) model = apps.get_model(app_label, model_name) # Get model instances which has non empty fields exclude_args = {'%s__isnull' % field_name: True, field_name: ''} instances = model.objects.exclude(**exclude_args) metadata_backend = metadata.get_backend() for instance in instances: field = getattr(instance, field_name) images.delete(field.name, size, metadata_backend, field.thumbnails.storage)
def handle(self, path_to_model, field_name, size, *args, **kwargs): if not path_to_model: raise ValueError('--model argument is required') if not field_name: raise ValueError('--field argument is required') if not size: raise ValueError('--size argument is required') app_label, model_name = path_to_model.rsplit('.', 1) model = get_model(app_label, model_name) # Get model instances which has non empty fields exclude_args = { '%s__isnull' % field_name: True, field_name: '' } instances = model.objects.exclude(**exclude_args) metadata_backend = metadata.get_backend() storage_backend = storage.get_backend() for instance in instances: field = getattr(instance, field_name) images.delete(field.name, size, metadata_backend, storage_backend)