Sending request into serialize functions

Signed-off-by: Jens Alm <jens.alm@mac.com>
This commit is contained in:
Jens Alm 2011-08-14 01:08:19 +02:00
parent 0542382489
commit c59a37b0a6
4 changed files with 22 additions and 22 deletions

View File

@ -444,11 +444,11 @@ class ResourceMixin(object):
"""
return self._resource.validate_request(data, files)
def filter_response(self, obj):
def filter_response(self, obj, request=None):
"""
Given the response content, filter it into a serializable object.
"""
return self._resource.filter_response(obj)
return self._resource.filter_response(obj, request)
def get_bound_form(self, content=None, method=None):
return self._resource.get_bound_form(content, method=method)

View File

@ -35,11 +35,11 @@ class BaseResource(Serializer):
"""
return data
def filter_response(self, obj):
def filter_response(self, obj, request=None):
"""
Given the response content, filter it into a serializable object.
"""
return self.serialize(obj)
return self.serialize(obj, request)
class Resource(BaseResource):

View File

@ -202,7 +202,7 @@ class Serializer(object):
return related_serializer(depth=depth, stack=stack).serialize(obj)
def serialize_max_depth(self, obj):
def serialize_max_depth(self, obj, request):
"""
Determine how objects should be serialized once `depth` is exceeded.
The default behavior is to ignore the field.
@ -210,7 +210,7 @@ class Serializer(object):
raise _SkipField
def serialize_recursion(self, obj):
def serialize_recursion(self, obj, request):
"""
Determine how objects should be serialized if recursion occurs.
The default behavior is to ignore the field.
@ -218,7 +218,7 @@ class Serializer(object):
raise _SkipField
def serialize_model(self, instance):
def serialize_model(self, instance, request):
"""
Given a model instance or dict, serialize it to a dict..
"""
@ -252,54 +252,54 @@ class Serializer(object):
return data
def serialize_iter(self, obj):
def serialize_iter(self, obj, request=None):
"""
Convert iterables into a serializable representation.
"""
return [self.serialize(item) for item in obj]
return [self.serialize(item, request) for item in obj]
def serialize_func(self, obj):
def serialize_func(self, obj, request=None):
"""
Convert no-arg methods and functions into a serializable representation.
"""
return self.serialize(obj())
return self.serialize(obj(), request)
def serialize_manager(self, obj):
def serialize_manager(self, obj, request=None):
"""
Convert a model manager into a serializable representation.
"""
return self.serialize_iter(obj.all())
return self.serialize_iter(obj.all(), request)
def serialize_fallback(self, obj):
def serialize_fallback(self, obj, request=None):
"""
Convert any unhandled object into a serializable representation.
"""
return smart_unicode(obj, strings_only=True)
def serialize(self, obj):
def serialize(self, obj, request=None):
"""
Convert any object into a serializable representation.
"""
if isinstance(obj, (dict, models.Model)):
# Model instances & dictionaries
return self.serialize_model(obj)
return self.serialize_model(obj, request)
elif isinstance(obj, (tuple, list, set, QuerySet, types.GeneratorType)):
# basic iterables
return self.serialize_iter(obj)
return self.serialize_iter(obj, request)
elif isinstance(obj, models.Manager):
# Manager objects
return self.serialize_manager(obj)
return self.serialize_manager(obj, request)
elif inspect.isfunction(obj) and not inspect.getargspec(obj)[0]:
# function with no args
return self.serialize_func(obj)
return self.serialize_func(obj, request)
elif inspect.ismethod(obj) and len(inspect.getargspec(obj)[0]) <= 1:
# bound method
return self.serialize_func(obj)
return self.serialize_func(obj, request)
# Protected types are passed through as is.
# (i.e. Primitives like None, numbers, dates, and Decimals.)
@ -307,4 +307,4 @@ class Serializer(object):
return obj
# All other values are converted to string.
return self.serialize_fallback(obj)
return self.serialize_fallback(obj, request)

View File

@ -141,7 +141,7 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
response = Response(status.HTTP_204_NO_CONTENT)
# Pre-serialize filtering (eg filter complex objects into natively serializable types)
response.cleaned_content = self.filter_response(response.raw_content)
response.cleaned_content = self.filter_response(response.raw_content, request)
except ErrorResponse, exc:
response = exc.response