Skip to content

Commit

Permalink
Fixed Issue where patch_list does not include request in the bundle w…
Browse files Browse the repository at this point in the history
…hen creating a list of new objects

Thanks ryanisnan.
  • Loading branch information
Issac Kelly committed Jun 4, 2012
1 parent 1367d7a commit 552aadf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Expand Up @@ -56,7 +56,7 @@ Contributors:
* Taylor Mitchell (tmitchell) for finding and patching a bug in the paginator query encoding.
* D.B. Tsai (dbtsai) & DuJour for funding:
* The work to make non-pk URIs work better

* Ryan West (ryanisnan) for finding and patching a bug in patch_list where requests were not included in the bundle.

Thanks to Tav for providing validate_jsonp.py, placed in public domain.

Expand Down
4 changes: 2 additions & 2 deletions tastypie/resources.py
Expand Up @@ -1339,13 +1339,13 @@ def patch_list(self, request, **kwargs):
# The object referenced by resource_uri doesn't exist,
# so this is a create-by-PUT equivalent.
data = self.alter_deserialized_detail_data(request, data)
bundle = self.build_bundle(data=dict_strip_unicode_keys(data))
bundle = self.build_bundle(data=dict_strip_unicode_keys(data), request=request)
self.obj_create(bundle, request=request)
else:
# There's no resource URI, so this is a create call just
# like a POST to the list resource.
data = self.alter_deserialized_detail_data(request, data)
bundle = self.build_bundle(data=dict_strip_unicode_keys(data))
bundle = self.build_bundle(data=dict_strip_unicode_keys(data), request=request)
self.obj_create(bundle, request=request)

if len(deserialized.get('deleted_objects', [])) and 'delete' not in self._meta.detail_allowed_methods:
Expand Down
22 changes: 22 additions & 0 deletions tests/core/tests/resources.py
Expand Up @@ -794,6 +794,11 @@ def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):

return '/api/v1/notes/%s/' % bundle_or_obj.obj.id

class DetailedNoteResourceWithHydrate(DetailedNoteResource):
def hydrate(self, bundle):
bundle.data['user'] = bundle.request.user # This should fail using TastyPie 0.9.11 if triggered in patch_list
return bundle

class RequiredFKNoteResource(ModelResource):
editor = fields.ForeignKey(UserResource, 'editor')

Expand Down Expand Up @@ -1857,6 +1862,23 @@ def test_patch_list_bad_resource_uri(self):
new_note = Note.objects.get(slug='invalid-uri')
self.assertEqual(new_note.content, "This is an invalid resource_uri")

def test_patch_list_with_request_data(self):
"""
Verify that request data is accessible in a Resource's hydrate method after patch_list.
"""
resource = DetailedNoteResourceWithHydrate()
request = HttpRequest()
request.user = User.objects.get(username='johndoe')
request.GET = {'format': 'json'}
request.method = 'PATCH'
request._read_started = False # Not sure what this line does, copied from above

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-again-again", "title": "The Cat Is Back", "updated": "2010-04-03 20:05:00"}]}'
resp = resource.patch_list(request)
self.assertEqual(resp.status_code, 202)
self.assertEqual(resp.content, '')
self.assertEqual(Note.objects.filter(author=request.user, slug="cat-again-again").count(), 1) # Validate that request.user was successfully passed in

def test_patch_detail(self):
self.assertEqual(Note.objects.count(), 6)
resource = NoteResource()
Expand Down

0 comments on commit 552aadf

Please sign in to comment.