Skip to content

Commit

Permalink
Changed Resource.patch_list to respect always_return data.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberdelia authored and toastdriven committed Feb 14, 2013
1 parent 7265edf commit 21c55d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tastypie/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,8 @@ def patch_list(self, request, **kwargs):
if len(deserialized[collection_name]) and 'put' not in self._meta.detail_allowed_methods:
raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed())

bundles_seen = []

for data in deserialized[collection_name]:
# If there's a resource_uri then this is either an
# update-in-place or a create-via-PUT.
Expand Down Expand Up @@ -1539,7 +1541,10 @@ def patch_list(self, request, **kwargs):
bundle = self.build_bundle(data=dict_strip_unicode_keys(data), request=request)
self.obj_create(bundle=bundle)

bundles_seen.append(bundle)

deleted_collection = deserialized.get(deleted_collection_name, [])

if deleted_collection:
if 'delete' not in self._meta.detail_allowed_methods:
raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed())
Expand All @@ -1549,7 +1554,13 @@ def patch_list(self, request, **kwargs):
bundle = self.build_bundle(obj=obj, request=request)
self.obj_delete(bundle=bundle)

return http.HttpAccepted()
if not self._meta.always_return_data:
return http.HttpAccepted()
else:
to_be_serialized = {}
to_be_serialized['objects'] = [self.full_dehydrate(bundle) for bundle in bundles_seen]
to_be_serialized = self.alter_list_data_to_serialize(request, to_be_serialized)
return self.create_response(request, to_be_serialized, response_class=http.HttpAccepted)

def patch_detail(self, request, **kwargs):
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/core/tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,20 @@ def test_patch_list(self):
updated_note = Note.objects.get(pk=2)
self.assertEqual(updated_note.content, "This is note 2.")

def test_patch_list_return_data(self):
always_resource = AlwaysDataNoteResource()
request = HttpRequest()
request.GET = {'format': 'json'}
request.method = 'PATCH'
request._read_started = False

self.assertEqual(Note.objects.count(), 6)
request._raw_post_data = request._body = '{"objects": [{"content": "The cat is back. The dog coughed him up out back.", "created": "2010-04-03 20:05:00", "is_active": true, "slug": "cat-is-back-again", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00"}, {"resource_uri": "/api/v1/notes/2/", "content": "This is note 2."}], "deleted_objects": ["/api/v1/notes/1/"]}'

resp = always_resource.patch_list(request)
self.assertEqual(resp.status_code, 202)
self.assertTrue(resp.content.startswith('{"objects": ['))

def test_patch_list_bad_resource_uri(self):
resource = NoteResource()
request = HttpRequest()
Expand Down

0 comments on commit 21c55d9

Please sign in to comment.