From 6305033373a3af9327dbe2f6c9654b4d5ed4e78e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 5 Jan 2012 18:24:47 +0000 Subject: [PATCH] Separate out SerializerMixin --- djangorestframework/mixins.py | 43 +++++++++++++++++++---------------- djangorestframework/views.py | 2 +- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index b13a48687..8b3280dbd 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -22,6 +22,7 @@ __all__ = ( 'ResponseMixin', 'AuthMixin', 'ResourceMixin', + 'SerializerMixin', # Reverse URL lookup behavior 'InstanceMixin', # Model behavior mixins @@ -393,6 +394,29 @@ class AuthMixin(object): 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 ########## class ResourceMixin(object): @@ -451,26 +475,7 @@ class ResourceMixin(object): self._resource = resource_class(view=self) 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 ########## diff --git a/djangorestframework/views.py b/djangorestframework/views.py index a3d7d0c37..01dbf3a59 100644 --- a/djangorestframework/views.py +++ b/djangorestframework/views.py @@ -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. Performs request deserialization, response serialization, authentication and input validation.