def test_delete(self):
        """
        Ensure that ``delete`` works properly.
        """
        thumbnail = images.create(self.source_name, 'small',
                                  self.metadata_backend, self.storage_backend)

        images.delete(self.source_name, 'small', self.metadata_backend,
                      self.storage_backend)
        self.assertFalse(self.storage_backend.exists(thumbnail.name))
        self.assertEqual(
            self.metadata_backend.get_thumbnail(self.source_name, 'small'),
            None)

        # key with format is not defined in settings anymore
        thumbnail = images.create(self.source_name, 'source_with_format',
                                  self.metadata_backend, self.storage_backend)

        thumbnail_size_data = {
            'small': {
                'PROCESSORS': [{
                    'PATH': 'thumbnails.processors.resize',
                    'width': 10,
                    'height': 10
                }]
            },
        }
        with patch('thumbnails.images.conf.SIZES', thumbnail_size_data):
            # ensure error not happening, and its files and metadata are still removed
            images.delete(self.source_name, 'source_with_format',
                          self.metadata_backend, self.storage_backend)
            self.assertFalse(self.storage_backend.exists(thumbnail.name))
            self.assertIsNone(
                self.metadata_backend.get_thumbnail(self.source_name,
                                                    'source_with_format'))
    def test_delete_with_no_thumbnails_file(self):
        avatar_path = self.instance.avatar.path
        avatar_folder = os.path.join(
            self.instance.avatar.storage.temporary_location, conf.BASE_DIR,
            'avatars')
        self.assertTrue(os.path.exists(avatar_path))

        self.instance.avatar.thumbnails.small

        size = 'small'
        thumbnail_name = os.path.basename(
            images.get_thumbnail_name(self.instance.avatar.name, size))
        thumbnail_path = os.path.join(avatar_folder, thumbnail_name)
        self.assertTrue(os.path.exists(thumbnail_path))

        # test delete non existence thumbnail file, should not raise error
        os.remove(thumbnail_path)
        self.assertFalse(os.path.exists(thumbnail_path))

        images.delete(self.source_name, size, self.metadata_backend,
                      self.storage_backend)

        # thumbnails and their metadata are also deleted
        self.assertEqual(len(os.listdir(avatar_folder)), 0)
        self.assertIsNone(
            self.metadata_backend.get_thumbnail(self.source_name, size))
Пример #3
0
    def test_delete(self):
        """
        Ensure that ``delete`` works properly.
        """
        thumbnail = images.create(self.source_name, 'small',
                                  self.metadata_backend, self.storage_backend)

        images.delete(self.source_name, 'small', self.metadata_backend,
                      self.storage_backend)
        self.assertFalse(self.storage_backend.exists(thumbnail.name))
        self.assertEqual(
            self.metadata_backend.get_thumbnail(self.source_name, 'small'),
            None)
Пример #4
0
    def test_delete(self):
        """
        Ensure that ``delete`` works properly.
        """
        thumbnail = images.create(self.source_name, 'small',
                                  self.metadata_backend, self.storage_backend)

        images.delete(self.source_name, 'small',
                      self.metadata_backend, self.storage_backend)
        self.assertFalse(self.storage_backend.exists(thumbnail.name))
        self.assertEqual(
            self.metadata_backend.get_thumbnail(self.source_name, 'small'),
            None
        )
    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)