Skip to content

Commit

Permalink
Added request (if available) to all bundles to make request-depen…
Browse files Browse the repository at this point in the history
…dent changes easier through the code.
  • Loading branch information
toastdriven committed Jun 16, 2011
1 parent 2229e75 commit 30cf7f9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/resources.rst
Expand Up @@ -715,7 +715,7 @@ Mostly a hook, this uses class assigned to ``throttle`` from
``build_bundle``
----------------

.. method:: Resource.build_bundle(self, obj=None, data=None)
.. method:: Resource.build_bundle(self, obj=None, data=None, request=None)

Given either an object, a data dictionary or both, builds a ``Bundle``
for use throughout the ``dehydrate/hydrate`` cycle.
Expand Down
6 changes: 5 additions & 1 deletion tastypie/bundle.py
@@ -1,3 +1,6 @@
from django.http import HttpRequest


# In a separate file to avoid circular imports...
class Bundle(object):
"""
Expand All @@ -7,9 +10,10 @@ class Bundle(object):
Necessary because the ``dehydrate/hydrate`` cycle needs to access data at
different points.
"""
def __init__(self, obj=None, data=None):
def __init__(self, obj=None, data=None, request=None):
self.obj = obj
self.data = data or {}
self.request = request or HttpRequest()

def __repr__(self):
return "<Bundle for obj: '%s' and with data: '%s'>" % (self.obj, self.data)
12 changes: 6 additions & 6 deletions tastypie/fields.py
Expand Up @@ -495,7 +495,7 @@ def dehydrate_related(self, bundle, related_resource):
# ZOMG extra data and big payloads.
return related_resource.full_dehydrate(related_resource.instance)

def build_related_resource(self, value):
def build_related_resource(self, value, request=None):
"""
Used to ``hydrate`` the data provided. If just a URL is provided,
the related resource is attempted to be loaded. If a
Expand All @@ -514,7 +514,7 @@ def build_related_resource(self, value):
elif hasattr(value, 'items'):
# Try to hydrate the data provided.
value = dict_strip_unicode_keys(value)
self.fk_bundle = Bundle(data=value)
self.fk_bundle = Bundle(data=value, request=request)

# We need to check to see if updates are allowed on the FK
# resource. If not, we'll just return a populated bundle instead
Expand Down Expand Up @@ -568,7 +568,7 @@ def dehydrate(self, bundle):
return None

self.fk_resource = self.get_related_resource(foreign_obj)
fk_bundle = Bundle(obj=foreign_obj)
fk_bundle = Bundle(obj=foreign_obj, request=bundle.request)
return self.dehydrate_related(fk_bundle, self.fk_resource)

def hydrate(self, bundle):
Expand All @@ -577,7 +577,7 @@ def hydrate(self, bundle):
if value is None:
return value

return self.build_related_resource(value)
return self.build_related_resource(value, request=bundle.request)

class ForeignKey(ToOneField):
"""
Expand Down Expand Up @@ -635,7 +635,7 @@ def dehydrate(self, bundle):
# ``Manager`` there.
for m2m in the_m2ms.all():
m2m_resource = self.get_related_resource(m2m)
m2m_bundle = Bundle(obj=m2m)
m2m_bundle = Bundle(obj=m2m, request=bundle.request)
self.m2m_resources.append(m2m_resource)
m2m_dehydrated.append(self.dehydrate_related(m2m_bundle, m2m_resource))

Expand All @@ -659,7 +659,7 @@ def hydrate_m2m(self, bundle):
if value is None:
continue

m2m_hydrated.append(self.build_related_resource(value))
m2m_hydrated.append(self.build_related_resource(value, request=bundle.request))

return m2m_hydrated

Expand Down
12 changes: 6 additions & 6 deletions tastypie/resources.py
Expand Up @@ -529,7 +529,7 @@ def log_throttled_access(self, request):
request_method = request.method.lower()
self._meta.throttle.accessed(self._meta.authentication.get_identifier(request), url=request.get_full_path(), request_method=request_method)

def build_bundle(self, obj=None, data=None):
def build_bundle(self, obj=None, data=None, request=None):
"""
Given either an object, a data dictionary or both, builds a ``Bundle``
for use throughout the ``dehydrate/hydrate`` cycle.
Expand All @@ -541,7 +541,7 @@ def build_bundle(self, obj=None, data=None):
if obj is None:
obj = self._meta.object_class()

return Bundle(obj, data)
return Bundle(obj=obj, data=data, request=request)

def build_filters(self, filters=None):
"""
Expand Down Expand Up @@ -1037,7 +1037,7 @@ def put_list(self, request, **kwargs):
bundles_seen = []

for object_data in deserialized['objects']:
bundle = self.build_bundle(data=dict_strip_unicode_keys(object_data))
bundle = self.build_bundle(data=dict_strip_unicode_keys(object_data), request=request)

# Attempt to be transactional, deleting any previously created
# objects if validation fails.
Expand Down Expand Up @@ -1065,7 +1065,7 @@ def put_detail(self, request, **kwargs):
"""
deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
deserialized = self.alter_deserialized_detail_data(request, deserialized)
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized))
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized), request=request)
self.is_valid(bundle, request)

try:
Expand All @@ -1085,8 +1085,8 @@ def post_list(self, request, **kwargs):
If a new resource is created, return ``HttpCreated`` (201 Created).
"""
deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
deserialized = self.alter_deserialized_list_data(request, deserialized)
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized))
deserialized = self.alter_deserialized_detail_data(request, deserialized)
bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized), request=request)
self.is_valid(bundle, request)
updated_bundle = self.obj_create(bundle, request=request)
return HttpCreated(location=self.get_resource_uri(updated_bundle))
Expand Down
7 changes: 7 additions & 0 deletions tests/core/tests/resources.py
Expand Up @@ -1438,6 +1438,13 @@ def test_build_bundle(self):
populated_bundle = resource.build_bundle(data={'title': 'Foo'})
self.assertTrue(isinstance(populated_bundle, Bundle))
self.assertEqual(populated_bundle.data, {'title': 'Foo'})

req = HttpRequest()
req.GET = {'foo': 'bar'}
populated_bundle_with_request = resource.build_bundle(data={'title': 'Foo'}, request=req)
self.assertTrue(isinstance(populated_bundle_with_request, Bundle))
self.assertEqual(populated_bundle_with_request.data, {'title': 'Foo'})
self.assertEqual(populated_bundle_with_request.request.GET['foo'], 'bar')

def test_obj_get_list(self):
resource = NoteResource()
Expand Down

0 comments on commit 30cf7f9

Please sign in to comment.