Skip to content

Commit eb88bd2

Browse files
YomesIncAmir Tocker
authored andcommitted
Add api.delete_derived_by_transformation
1 parent 1262d97 commit eb88bd2

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

cloudinary/api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ def delete_derived_resources(derived_resource_ids, **options):
159159
return call_api("delete", uri, params, **options)
160160

161161

162+
def delete_derived_by_transformation(public_ids, transformations, resource_type='image', type='upload', invalidate=None, **options):
163+
"""
164+
Delete derived resources of public ids, identified by transformations
165+
166+
:param public_ids: the base resources
167+
:type public_ids: list of string
168+
:param transformations: the transformation of derived resources, optionally including the format
169+
:type transformations: list of (dict or string)
170+
:param invalidate: (optional) True to invalidate the resources after deletion
171+
:type invalidate: bool
172+
:return: a list of the public ids for which derived resources were deleted
173+
:rtype: dict
174+
"""
175+
uri = ["resources", resource_type, type]
176+
if not isinstance(public_ids, list):
177+
public_ids = [public_ids]
178+
params = [("public_ids[]", public_id) for public_id in public_ids]
179+
params.append(("transformations", utils.build_eager(transformations)))
180+
params.append(("keep_original", True))
181+
if invalidate is not None:
182+
params.append(('invalidate', invalidate))
183+
return call_api("delete", uri, params, **options)
184+
185+
162186
def tags(**options):
163187
resource_type = options.pop("resource_type", "image")
164188
uri = ["tags", resource_type]

tests/api_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,40 @@ def test08_delete_derived(self, mocker):
181181
self.assertTrue(get_uri(args).endswith('/derived_resources'))
182182
self.assertIn(('derived_resource_ids[]', API_TEST_ID), get_params(args))
183183

184+
@patch('urllib3.request.RequestMethods.request')
185+
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
186+
def test08a_delete_derived_by_transformation(self, mocker):
187+
""" should allow deleting derived resource by transformations """
188+
public_resource_id = 'public_resource_id'
189+
public_resource_id2 = 'public_resource_id2'
190+
transformation = {"crop": "scale", "width": 100}
191+
transformation2 = {"crop": "scale", "width": 200}
192+
193+
mocker.return_value = MOCK_RESPONSE
194+
api.delete_derived_by_transformation(public_resource_id, transformation)
195+
method, url, params = mocker.call_args[0][0:3]
196+
self.assertEqual('DELETE', method)
197+
self.assertTrue(url.endswith('/resources/image/upload'))
198+
self.assertIn(('public_ids[]', public_resource_id), params)
199+
self.assertIn(('transformations', utils.build_eager([transformation])), params)
200+
self.assertIn(('keep_original', True), params)
201+
202+
mocker.return_value = MOCK_RESPONSE
203+
api.delete_derived_by_transformation(
204+
[public_resource_id, public_resource_id2],
205+
[transformation, transformation2], resource_type='raw', type='fetch', invalidate=True, foo='bar')
206+
method, url, params = mocker.call_args[0][0:3]
207+
self.assertEqual('DELETE', method)
208+
self.assertTrue(url.endswith('/resources/raw/fetch'))
209+
self.assertIn(('public_ids[]', public_resource_id), params)
210+
self.assertIn(('public_ids[]', public_resource_id2), params)
211+
self.assertIn(
212+
('transformations', utils.build_eager([transformation, transformation2])),
213+
params)
214+
self.assertIn(('keep_original', True), params)
215+
self.assertIn(('invalidate', True), params)
216+
217+
184218
@patch('urllib3.request.RequestMethods.request')
185219
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
186220
def test09_delete_resources(self, mocker):

0 commit comments

Comments
 (0)