Separate out SerializerMixin

This commit is contained in:
Tom Christie 2012-01-05 18:24:47 +00:00
parent aaea2f26bc
commit 6305033373
2 changed files with 25 additions and 20 deletions

View File

@ -22,6 +22,7 @@ __all__ = (
'ResponseMixin', 'ResponseMixin',
'AuthMixin', 'AuthMixin',
'ResourceMixin', 'ResourceMixin',
'SerializerMixin',
# Reverse URL lookup behavior # Reverse URL lookup behavior
'InstanceMixin', 'InstanceMixin',
# Model behavior mixins # Model behavior mixins
@ -393,6 +394,29 @@ class AuthMixin(object):
permission.check_permission(user) permission.check_permission(user)
class SerializerMixin(object):
def validate_request(self, data, files=None):
"""
Given the request *data* and optional *files*, return the cleaned,
validated content.
May raise an :class:`response.ErrorResponse` with status code 400
(Bad Request) on failure.
"""
return self.resource.validate_request(data, files)
def filter_response(self, obj):
"""
Given the response content, filter it into a serializable object.
"""
return self.resource.filter_response(obj)
def get_bound_form(self, content=None, method=None):
if hasattr(self.resource, 'get_bound_form'):
return self.resource.get_bound_form(content, method=method)
else:
return None
########## Resource Mixin ########## ########## Resource Mixin ##########
class ResourceMixin(object): class ResourceMixin(object):
@ -451,26 +475,7 @@ class ResourceMixin(object):
self._resource = resource_class(view=self) self._resource = resource_class(view=self)
return self._resource return self._resource
def validate_request(self, data, files=None):
"""
Given the request *data* and optional *files*, return the cleaned,
validated content.
May raise an :class:`response.ErrorResponse` with status code 400
(Bad Request) on failure.
"""
return self.resource.validate_request(data, files)
def filter_response(self, obj):
"""
Given the response content, filter it into a serializable object.
"""
return self.resource.filter_response(obj)
def get_bound_form(self, content=None, method=None):
if hasattr(self.resource, 'get_bound_form'):
return self.resource.get_bound_form(content, method=method)
else:
return None
########## ##########

View File

@ -25,7 +25,7 @@ __all__ = (
) )
class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView): class View(ResourceMixin, RequestMixin, ResponseMixin, SerializerMixin, AuthMixin, DjangoView):
""" """
Handles incoming requests and maps them to REST operations. Handles incoming requests and maps them to REST operations.
Performs request deserialization, response serialization, authentication and input validation. Performs request deserialization, response serialization, authentication and input validation.