Skip to content

Commit

Permalink
Add api.delete_derived_by_transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
YomesInc authored and Amir Tocker committed Dec 2, 2017
1 parent 1262d97 commit eb88bd2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cloudinary/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ def delete_derived_resources(derived_resource_ids, **options):
return call_api("delete", uri, params, **options)


def delete_derived_by_transformation(public_ids, transformations, resource_type='image', type='upload', invalidate=None, **options):
"""
Delete derived resources of public ids, identified by transformations
:param public_ids: the base resources
:type public_ids: list of string
:param transformations: the transformation of derived resources, optionally including the format
:type transformations: list of (dict or string)
:param invalidate: (optional) True to invalidate the resources after deletion
:type invalidate: bool
:return: a list of the public ids for which derived resources were deleted
:rtype: dict
"""
uri = ["resources", resource_type, type]
if not isinstance(public_ids, list):
public_ids = [public_ids]
params = [("public_ids[]", public_id) for public_id in public_ids]
params.append(("transformations", utils.build_eager(transformations)))
params.append(("keep_original", True))
if invalidate is not None:
params.append(('invalidate', invalidate))
return call_api("delete", uri, params, **options)


def tags(**options):
resource_type = options.pop("resource_type", "image")
uri = ["tags", resource_type]
Expand Down
34 changes: 34 additions & 0 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ def test08_delete_derived(self, mocker):
self.assertTrue(get_uri(args).endswith('/derived_resources'))
self.assertIn(('derived_resource_ids[]', API_TEST_ID), get_params(args))

@patch('urllib3.request.RequestMethods.request')
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
def test08a_delete_derived_by_transformation(self, mocker):
""" should allow deleting derived resource by transformations """
public_resource_id = 'public_resource_id'
public_resource_id2 = 'public_resource_id2'
transformation = {"crop": "scale", "width": 100}
transformation2 = {"crop": "scale", "width": 200}

mocker.return_value = MOCK_RESPONSE
api.delete_derived_by_transformation(public_resource_id, transformation)
method, url, params = mocker.call_args[0][0:3]
self.assertEqual('DELETE', method)
self.assertTrue(url.endswith('/resources/image/upload'))
self.assertIn(('public_ids[]', public_resource_id), params)
self.assertIn(('transformations', utils.build_eager([transformation])), params)
self.assertIn(('keep_original', True), params)

mocker.return_value = MOCK_RESPONSE
api.delete_derived_by_transformation(
[public_resource_id, public_resource_id2],
[transformation, transformation2], resource_type='raw', type='fetch', invalidate=True, foo='bar')
method, url, params = mocker.call_args[0][0:3]
self.assertEqual('DELETE', method)
self.assertTrue(url.endswith('/resources/raw/fetch'))
self.assertIn(('public_ids[]', public_resource_id), params)
self.assertIn(('public_ids[]', public_resource_id2), params)
self.assertIn(
('transformations', utils.build_eager([transformation, transformation2])),
params)
self.assertIn(('keep_original', True), params)
self.assertIn(('invalidate', True), params)


@patch('urllib3.request.RequestMethods.request')
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
def test09_delete_resources(self, mocker):
Expand Down

0 comments on commit eb88bd2

Please sign in to comment.