Skip to content

Commit

Permalink
Better use of bundle.request.
Browse files Browse the repository at this point in the history
  • Loading branch information
vbabiy authored and toastdriven committed Feb 28, 2012
1 parent 0fc8f9b commit 30cf082
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tastypie/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ def obj_update(self, bundle, request=None, **kwargs):
# Attempt to hydrate data from kwargs before doing a lookup for the object.
# This step is needed so certain values (like datetime) will pass model validation.
try:
bundle.obj = self.get_object_list(request).model()
bundle.obj = self.get_object_list(bundle.request).model()
bundle.data.update(kwargs)
bundle = self.full_hydrate(bundle)
lookup_kwargs = kwargs.copy()
Expand All @@ -1830,7 +1830,7 @@ def obj_update(self, bundle, request=None, **kwargs):
lookup_kwargs = kwargs

try:
bundle.obj = self.obj_get(request, **lookup_kwargs)
bundle.obj = self.obj_get(bundle.request, **lookup_kwargs)
except ObjectDoesNotExist:
raise NotFound("A model instance matching the provided arguments could not be found.")

Expand Down
30 changes: 30 additions & 0 deletions tests/core/tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,16 @@ class Meta:
queryset = Note.objects.all()


class AlwaysUserNoteResource(NoteResource):
class Meta:
resource_name = 'noteish'
queryset = Note.objects.filter(is_active=True)
authorization = Authorization()

def get_object_list(self, request):
return super(AlwaysUserNoteResource, self).get_object_list(request).filter(author=request.user)


class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
Expand Down Expand Up @@ -2437,6 +2447,26 @@ def test_obj_update(self):
self.assertRaises(NotFound, note.obj_update, note_bundle, pk=1, created='2010-03-31T20:05:00')
self.assertEqual(Note.objects.all().count(), 6)

# Assign based on the ``request.user``, which helps ensure that
# the correct ``request`` is being passed along.
request = HttpRequest()
request.user = User.objects.get(username='johndoe')
self.assertEqual(AlwaysUserNoteResource().get_object_list(request).count(), 2)
note = AlwaysUserNoteResource()
note_obj = note.obj_get(request, pk=1)
note_bundle = note.build_bundle(obj=note_obj)
note_bundle = note.full_dehydrate(note_bundle)
note_bundle.data['title'] = 'Whee!'
note_bundle.request = request
note.obj_update(note_bundle, pk=1)
self.assertEqual(Note.objects.all().count(), 6)
numero_uno = Note.objects.get(pk=1)
self.assertEqual(numero_uno.title, u'Whee!')
self.assertEqual(numero_uno.slug, u'yet-another-another-new-post')
self.assertEqual(numero_uno.content, u'WHEEEEEE!')
self.assertEqual(numero_uno.is_active, True)
self.assertEqual(numero_uno.author.pk, request.user.pk)

def test_obj_delete(self):
self.assertEqual(Note.objects.all().count(), 6)
note = NoteResource()
Expand Down

0 comments on commit 30cf082

Please sign in to comment.