Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for DELETE on subresources in list.

  • Loading branch information...
commit f9428cedc618fb7cc662488e06f57c3836047a8e 1 parent 29d8b20
Mitar mitar authored
12 tastypie_mongoengine/resources.py
View
@@ -568,9 +568,15 @@ def obj_update(self, bundle, request=None, **kwargs):
return bundle
def obj_delete(self, request=None, **kwargs):
- index = int(kwargs['index'])
- self.obj_get(request, **kwargs)
- getattr(self.instance, self.attribute).pop(index)
+ obj = kwargs.pop('_obj', None)
+
+ if not getattr(obj, 'pk', None):
+ try:
+ obj = self.obj_get(request, **kwargs)
+ except (queryset.DoesNotExist, exceptions.ObjectDoesNotExist):
+ raise NotFound("A model instance matching the provided arguments could not be found.")
+
+ getattr(self.instance, self.attribute).pop(int(obj.pk))
self.instance.save()
def get_resource_uri(self, bundle_or_obj):
49 tests/test_project/test_app/tests/test_basic.py
View
@@ -334,6 +334,8 @@ def test_basic(self):
self.assertEqual(response.status_code, 404)
def test_embeddedlist(self):
+ # Testing POST
+
response = self.c.post(self.resourceListURI('embeddedlistfieldtest'), '{"embeddedlist": [{"name": "Embedded person 1"}, {"name": "Embedded person 2", "optional": "Optional"}]}', content_type='application/json')
self.assertEqual(response.status_code, 201)
@@ -421,6 +423,8 @@ def test_embeddedlist(self):
self.assertEqual(len(response['embeddedlist']), 4)
+ # Testing PUT
+
response = self.c.put(embedded4_uri, '{"name": "Embedded person 4a", "optional": "Foobar PUT"}', content_type='application/json')
self.assertEqual(response.status_code, 204)
@@ -477,6 +481,8 @@ def test_embeddedlist(self):
self.assertEqual(len(response['embeddedlist']), 4)
+ # Testing PATCH
+
response = self.c.patch(embedded1_uri, '{"name": "Embedded person 1 PATCHED"}', content_type='application/json')
self.assertEqual(response.status_code, 202)
@@ -488,6 +494,49 @@ def test_embeddedlist(self):
self.assertEqual(response['optional'], None)
self.assertEqual(response['resource_uri'], embedded1_uri)
+ # Testing DELETE
+
+ response = self.c.delete(embedded4_uri)
+ self.assertEqual(response.status_code, 204)
+
+ response = self.c.get(embedded4_uri)
+ self.assertEqual(response.status_code, 404)
+
+ response = self.c.get(mainresource_uri)
+ self.assertEqual(response.status_code, 200)
+ response = json.loads(response.content)
+
+ self.assertEqual(len(response['embeddedlist']), 3)
+
+ response = self.c.get(embedded2_uri)
+ self.assertEqual(response.status_code, 200)
+ response = json.loads(response.content)
+
+ self.assertEqual(response['name'], 'Embedded person 2')
+ self.assertEqual(response['optional'], 'Optional')
+ self.assertEqual(response['resource_uri'], embedded2_uri)
+
+ response = self.c.delete(embedded2_uri)
+ self.assertEqual(response.status_code, 204)
+
+ response = self.c.get(embedded2_uri)
+ self.assertEqual(response.status_code, 200)
+ response = json.loads(response.content)
+
+ # Content from embedded3_uri moves in place of embedded2_uri
+ self.assertEqual(response['name'], 'Embedded person 3')
+ self.assertEqual(response['optional'], None)
+ self.assertEqual(response['resource_uri'], embedded2_uri)
+
+ response = self.c.get(embedded3_uri)
+ self.assertEqual(response.status_code, 404)
+
+ response = self.c.get(mainresource_uri)
+ self.assertEqual(response.status_code, 200)
+ response = json.loads(response.content)
+
+ self.assertEqual(len(response['embeddedlist']), 2)
+
def test_polymorphic(self):
response = self.c.post(self.resourceListURI('person'), '{"name": "Person 1"}', content_type='application/json; type=person')
self.assertEqual(response.status_code, 201)
Please sign in to comment.
Something went wrong with that request. Please try again.