From ab0b72a7c1a17c6d85530514caa51ce5bd77b592 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Sun, 22 Jan 2012 21:28:34 +0200 Subject: .DATA, .FILES, overloaded HTTP method, content type and content available directly on the request - see #128 --- djangorestframework/mixins.py | 165 ++++++------------------------------------ 1 file changed, 21 insertions(+), 144 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 7f0870f8..d016b0f1 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -14,11 +14,10 @@ from djangorestframework import status from djangorestframework.renderers import BaseRenderer from djangorestframework.resources import Resource, FormResource, ModelResource from djangorestframework.response import Response, ErrorResponse +from djangorestframework.request import request_class_factory from djangorestframework.utils import as_tuple, MSIE_USER_AGENT_REGEX from djangorestframework.utils.mediatypes import is_form_media_type, order_by_precedence -from StringIO import StringIO - __all__ = ( # Base behavior mixins @@ -56,150 +55,28 @@ class RequestMixin(object): Should be a tuple/list of classes as described in the :mod:`parsers` module. """ - @property - def method(self): - """ - Returns the HTTP method. - - This should be used instead of just reading :const:`request.method`, as it allows the `method` - to be overridden by using a hidden `form` field on a form POST request. - """ - if not hasattr(self, '_method'): - self._load_method_and_content_type() - return self._method - - @property - def content_type(self): - """ - Returns the content type header. - - This should be used instead of ``request.META.get('HTTP_CONTENT_TYPE')``, - as it allows the content type to be overridden by using a hidden form - field on a form POST request. - """ - if not hasattr(self, '_content_type'): - self._load_method_and_content_type() - return self._content_type - - @property - def DATA(self): - """ - Parses the request body and returns the data. - - Similar to ``request.POST``, except that it handles arbitrary parsers, - and also works on methods other than POST (eg PUT). - """ - if not hasattr(self, '_data'): - self._load_data_and_files() - return self._data - - @property - def FILES(self): + def get_request_class(self): """ - Parses the request body and returns the files. - Similar to ``request.FILES``, except that it handles arbitrary parsers, - and also works on methods other than POST (eg PUT). + Returns a custom subclass of Django's `HttpRequest`, providing new facilities + such as direct access to the parsed request content. """ - if not hasattr(self, '_files'): - self._load_data_and_files() - return self._files + if not hasattr(self, '_request_class'): + self._request_class = request_class_factory(self.request) + self._request_class._USE_FORM_OVERLOADING = self._USE_FORM_OVERLOADING + self._request_class._METHOD_PARAM = self._METHOD_PARAM + self._request_class._CONTENTTYPE_PARAM = self._CONTENTTYPE_PARAM + self._request_class._CONTENT_PARAM = self._CONTENT_PARAM + self._request_class.parsers = self.parsers + return self._request_class - def _load_data_and_files(self): + def get_request(self): """ - Parse the request content into self.DATA and self.FILES. - """ - if not hasattr(self, '_content_type'): - self._load_method_and_content_type() - - if not hasattr(self, '_data'): - (self._data, self._files) = self._parse(self._get_stream(), self._content_type) - - def _load_method_and_content_type(self): + Returns a custom request instance, with data and attributes copied from the + original request. """ - Set the method and content_type, and then check if they've been overridden. - """ - self._method = self.request.method - self._content_type = self.request.META.get('HTTP_CONTENT_TYPE', self.request.META.get('CONTENT_TYPE', '')) - self._perform_form_overloading() - - def _get_stream(self): - """ - Returns an object that may be used to stream the request content. - """ - request = self.request - - try: - content_length = int(request.META.get('CONTENT_LENGTH', request.META.get('HTTP_CONTENT_LENGTH'))) - except (ValueError, TypeError): - content_length = 0 - - # TODO: Add 1.3's LimitedStream to compat and use that. - # NOTE: Currently only supports parsing request body as a stream with 1.3 - if content_length == 0: - return None - elif hasattr(request, 'read'): - return request - return StringIO(request.raw_post_data) - - def _perform_form_overloading(self): - """ - If this is a form POST request, then we need to check if the method and content/content_type have been - overridden by setting them in hidden form fields or not. - """ - - # We only need to use form overloading on form POST requests. - if not self._USE_FORM_OVERLOADING or self._method != 'POST' or not is_form_media_type(self._content_type): - return - - # At this point we're committed to parsing the request as form data. - self._data = data = self.request.POST.copy() - self._files = self.request.FILES - - # Method overloading - change the method and remove the param from the content. - if self._METHOD_PARAM in data: - # NOTE: unlike `get`, `pop` on a `QueryDict` seems to return a list of values. - self._method = self._data.pop(self._METHOD_PARAM)[0].upper() - - # Content overloading - modify the content type, and re-parse. - if self._CONTENT_PARAM in data and self._CONTENTTYPE_PARAM in data: - self._content_type = self._data.pop(self._CONTENTTYPE_PARAM)[0] - stream = StringIO(self._data.pop(self._CONTENT_PARAM)[0]) - (self._data, self._files) = self._parse(stream, self._content_type) - - def _parse(self, stream, content_type): - """ - Parse the request content. - - May raise a 415 ErrorResponse (Unsupported Media Type), or a 400 ErrorResponse (Bad Request). - """ - if stream is None or content_type is None: - return (None, None) - - parsers = as_tuple(self.parsers) - - for parser_cls in parsers: - parser = parser_cls(self) - if parser.can_handle_request(content_type): - return parser.parse(stream) - - raise ErrorResponse(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, - {'error': 'Unsupported media type in request \'%s\'.' % - content_type}) - - @property - def _parsed_media_types(self): - """ - Return a list of all the media types that this view can parse. - """ - return [parser.media_type for parser in self.parsers] - - @property - def _default_parser(self): - """ - Return the view's default parser class. - """ - return self.parsers[0] - + request_class = self.get_request_class() + return request_class(self.request) + ########## ResponseMixin ########## @@ -395,7 +272,7 @@ class ResourceMixin(object): May raise an :class:`response.ErrorResponse` with status code 400 (Bad Request). """ if not hasattr(self, '_content'): - self._content = self.validate_request(self.DATA, self.FILES) + self._content = self.validate_request(self.request.DATA, self.request.FILES) return self._content @property @@ -415,7 +292,7 @@ class ResourceMixin(object): return ModelResource(self) elif getattr(self, 'form', None): return FormResource(self) - elif getattr(self, '%s_form' % self.method.lower(), None): + elif getattr(self, '%s_form' % self.request.method.lower(), None): return FormResource(self) return Resource(self) @@ -752,7 +629,7 @@ class PaginatorMixin(object): """ # We don't want to paginate responses for anything other than GET requests - if self.method.upper() != 'GET': + if self.request.method.upper() != 'GET': return self._resource.filter_response(obj) paginator = Paginator(obj, self.get_limit()) -- cgit v1.2.3 From 714a90d7559885c15e5b2c86ef6f457fdf857ee0 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 24 Jan 2012 21:21:10 +0200 Subject: documentation for request module --- djangorestframework/mixins.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index d016b0f1..e53f8e6a 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -40,7 +40,7 @@ __all__ = ( class RequestMixin(object): """ - `Mixin` class to provide request parsing behavior. + `Mixin` class to enhance API of Django's standard `request`. """ _USE_FORM_OVERLOADING = True @@ -50,15 +50,15 @@ class RequestMixin(object): parsers = () """ - The set of request parsers that the view can handle. + The set of parsers that the request can handle. Should be a tuple/list of classes as described in the :mod:`parsers` module. """ def get_request_class(self): """ - Returns a custom subclass of Django's `HttpRequest`, providing new facilities - such as direct access to the parsed request content. + Returns a subclass of Django's `HttpRequest` with a richer API, + as described in :mod:`request`. """ if not hasattr(self, '_request_class'): self._request_class = request_class_factory(self.request) -- cgit v1.2.3 From 5bb6301b7f53e3815ab1a81a5fa38721dc95b113 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Thu, 2 Feb 2012 18:19:44 +0200 Subject: Response as a subclass of HttpResponse - first draft, not quite there yet. --- djangorestframework/mixins.py | 139 +++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 90 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 6c88fb64..dc2cfd27 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -6,7 +6,6 @@ classes that can be added to a `View`. from django.contrib.auth.models import AnonymousUser from django.core.paginator import Paginator from django.db.models.fields.related import ForeignKey -from django.http import HttpResponse from urlobject import URLObject from djangorestframework import status @@ -14,8 +13,7 @@ from djangorestframework.renderers import BaseRenderer from djangorestframework.resources import Resource, FormResource, ModelResource from djangorestframework.response import Response, ErrorResponse from djangorestframework.request import request_class_factory -from djangorestframework.utils import as_tuple, MSIE_USER_AGENT_REGEX -from djangorestframework.utils.mediatypes import is_form_media_type, order_by_precedence +from djangorestframework.utils import as_tuple, allowed_methods __all__ = ( @@ -34,6 +32,7 @@ __all__ = ( 'ListModelMixin' ) +#TODO: In RequestMixin and ResponseMixin : get_response_class/get_request_class are a bit ugly. Do we even want to be able to set the parameters on the view ? ########## Request Mixin ########## @@ -88,9 +87,6 @@ class ResponseMixin(object): Ignores Accept headers from Internet Explorer user agents and uses a sensible browser Accept header instead. """ - _ACCEPT_QUERY_PARAM = '_accept' # Allow override of Accept header in URL query params - _IGNORE_IE_ACCEPT_HEADER = True - renderers = () """ The set of response renderers that the view can handle. @@ -98,79 +94,27 @@ class ResponseMixin(object): Should be a tuple/list of classes as described in the :mod:`renderers` module. """ - # TODO: wrap this behavior around dispatch(), ensuring it works - # out of the box with existing Django classes that use render_to_response. - def render(self, response): - """ - Takes a :obj:`Response` object and returns an :obj:`HttpResponse`. - """ + response_class = Response + + def prepare_response(self, response): + """ + Prepares response for the response cycle. Sets some headers, sets renderers, ... + """ + if hasattr(response, 'request') and response.request is None: + response.request = self.request + # Always add these headers. + response['Allow'] = ', '.join(allowed_methods(self)) + # sample to allow caching using Vary http header + response['Vary'] = 'Authenticate, Accept' + # merge with headers possibly set at some point in the view + for name, value in self.headers.items(): + response[name] = value + # set the views renderers on the response + response.renderers = self.renderers + # TODO: must disappear + response.view = self self.response = response - - try: - renderer, media_type = self._determine_renderer(self.request) - except ErrorResponse, exc: - renderer = self._default_renderer(self) - media_type = renderer.media_type - response = exc.response - - # Set the media type of the response - # Note that the renderer *could* override it in .render() if required. - response.media_type = renderer.media_type - - # Serialize the response content - if response.has_content_body: - content = renderer.render(response.cleaned_content, media_type) - else: - content = renderer.render() - - # Build the HTTP Response - resp = HttpResponse(content, mimetype=response.media_type, status=response.status) - for (key, val) in response.headers.items(): - resp[key] = val - - return resp - - def _determine_renderer(self, request): - """ - Determines the appropriate renderer for the output, given the client's 'Accept' header, - and the :attr:`renderers` set on this class. - - Returns a 2-tuple of `(renderer, media_type)` - - See: RFC 2616, Section 14 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html - """ - - if self._ACCEPT_QUERY_PARAM and request.GET.get(self._ACCEPT_QUERY_PARAM, None): - # Use _accept parameter override - accept_list = [request.GET.get(self._ACCEPT_QUERY_PARAM)] - elif (self._IGNORE_IE_ACCEPT_HEADER and - 'HTTP_USER_AGENT' in request.META and - MSIE_USER_AGENT_REGEX.match(request.META['HTTP_USER_AGENT'])): - # Ignore MSIE's broken accept behavior and do something sensible instead - accept_list = ['text/html', '*/*'] - elif 'HTTP_ACCEPT' in request.META: - # Use standard HTTP Accept negotiation - accept_list = [token.strip() for token in request.META['HTTP_ACCEPT'].split(',')] - else: - # No accept header specified - accept_list = ['*/*'] - - # Check the acceptable media types against each renderer, - # attempting more specific media types first - # NB. The inner loop here isn't as bad as it first looks :) - # Worst case is we're looping over len(accept_list) * len(self.renderers) - renderers = [renderer_cls(self) for renderer_cls in self.renderers] - - for accepted_media_type_lst in order_by_precedence(accept_list): - for renderer in renderers: - for accepted_media_type in accepted_media_type_lst: - if renderer.can_handle_response(accepted_media_type): - return renderer, accepted_media_type - - # No acceptable renderers were found - raise ErrorResponse(status.HTTP_406_NOT_ACCEPTABLE, - {'detail': 'Could not satisfy the client\'s Accept header', - 'available_types': self._rendered_media_types}) + return response @property def _rendered_media_types(self): @@ -193,6 +137,17 @@ class ResponseMixin(object): """ return self.renderers[0] + @property + def headers(self): + """ + Dictionary of headers to set on the response. + This is useful when the response doesn't exist yet, but you + want to memorize some headers to set on it when it will exist. + """ + if not hasattr(self, '_headers'): + self._headers = {} + return self._headers + ########## Auth Mixin ########## @@ -429,7 +384,7 @@ class ReadModelMixin(ModelMixin): try: self.model_instance = self.get_instance(**query_kwargs) except model.DoesNotExist: - raise ErrorResponse(status.HTTP_404_NOT_FOUND) + raise ErrorResponse(status=status.HTTP_404_NOT_FOUND) return self.model_instance @@ -468,10 +423,12 @@ class CreateModelMixin(ModelMixin): data[m2m_data[fieldname][0]] = related_item manager.through(**data).save() - headers = {} + response = Response(instance, status=status.HTTP_201_CREATED) + + # Set headers if hasattr(instance, 'get_absolute_url'): - headers['Location'] = self.resource(self).url(instance) - return Response(status.HTTP_201_CREATED, instance, headers) + response['Location'] = self.resource(self).url(instance) + return response class UpdateModelMixin(ModelMixin): @@ -492,7 +449,7 @@ class UpdateModelMixin(ModelMixin): except model.DoesNotExist: self.model_instance = model(**self.get_instance_data(model, self.CONTENT, *args, **kwargs)) self.model_instance.save() - return self.model_instance + return Response(self.model_instance) class DeleteModelMixin(ModelMixin): @@ -506,10 +463,10 @@ class DeleteModelMixin(ModelMixin): try: instance = self.get_instance(**query_kwargs) except model.DoesNotExist: - raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {}) + raise ErrorResponse(status=status.HTTP_404_NOT_FOUND) instance.delete() - return + return Response() class ListModelMixin(ModelMixin): @@ -526,7 +483,7 @@ class ListModelMixin(ModelMixin): if ordering: queryset = queryset.order_by(*ordering) - return queryset + return Response(queryset) ########## Pagination Mixins ########## @@ -613,12 +570,14 @@ class PaginatorMixin(object): try: page_num = int(self.request.GET.get('page', '1')) except ValueError: - raise ErrorResponse(status.HTTP_404_NOT_FOUND, - {'detail': 'That page contains no results'}) + raise ErrorResponse( + content={'detail': 'That page contains no results'}, + status=status.HTTP_404_NOT_FOUND) if page_num not in paginator.page_range: - raise ErrorResponse(status.HTTP_404_NOT_FOUND, - {'detail': 'That page contains no results'}) + raise ErrorResponse( + content={'detail': 'That page contains no results'}, + status=status.HTTP_404_NOT_FOUND) page = paginator.page(page_num) -- cgit v1.2.3 From ca96b4523b4c09489e4bfe726a894a5c6ada78aa Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 7 Feb 2012 13:15:30 +0200 Subject: cleaned a bit Response/ResponseMixin code, added some documentation + renamed ErrorResponse to ImmediateResponse --- djangorestframework/mixins.py | 46 +++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index dc2cfd27..c30ef10b 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -11,7 +11,7 @@ from urlobject import URLObject from djangorestframework import status from djangorestframework.renderers import BaseRenderer from djangorestframework.resources import Resource, FormResource, ModelResource -from djangorestframework.response import Response, ErrorResponse +from djangorestframework.response import Response, ImmediateResponse from djangorestframework.request import request_class_factory from djangorestframework.utils import as_tuple, allowed_methods @@ -80,28 +80,37 @@ class RequestMixin(object): class ResponseMixin(object): """ - Adds behavior for pluggable `Renderers` to a :class:`views.View` class. + Adds behavior for pluggable `renderers` to a :class:`views.View` class. Default behavior is to use standard HTTP Accept header content negotiation. Also supports overriding the content type by specifying an ``_accept=`` parameter in the URL. Ignores Accept headers from Internet Explorer user agents and uses a sensible browser Accept header instead. """ - renderers = () + renderer_classes = () """ The set of response renderers that the view can handle. Should be a tuple/list of classes as described in the :mod:`renderers` module. """ - response_class = Response + def get_renderers(self): + """ + Instantiates and returns the list of renderers that will be used to render + the response. + """ + if not hasattr(self, '_renderers'): + self._renderers = [r(self) for r in self.renderer_classes] + return self._renderers def prepare_response(self, response): """ - Prepares response for the response cycle. Sets some headers, sets renderers, ... + Prepares the response for the response cycle. This has no effect if the + response is not an instance of :class:`response.Response`. """ if hasattr(response, 'request') and response.request is None: response.request = self.request + # Always add these headers. response['Allow'] = ', '.join(allowed_methods(self)) # sample to allow caching using Vary http header @@ -109,10 +118,9 @@ class ResponseMixin(object): # merge with headers possibly set at some point in the view for name, value in self.headers.items(): response[name] = value + # set the views renderers on the response - response.renderers = self.renderers - # TODO: must disappear - response.view = self + response.renderers = self.get_renderers() self.response = response return response @@ -121,21 +129,21 @@ class ResponseMixin(object): """ Return an list of all the media types that this view can render. """ - return [renderer.media_type for renderer in self.renderers] + return [renderer.media_type for renderer in self.get_renderers()] @property def _rendered_formats(self): """ Return a list of all the formats that this view can render. """ - return [renderer.format for renderer in self.renderers] + return [renderer.format for renderer in self.get_renderers()] @property def _default_renderer(self): """ Return the view's default renderer class. """ - return self.renderers[0] + return self.get_renderers()[0] @property def headers(self): @@ -195,7 +203,7 @@ class AuthMixin(object): # TODO: wrap this behavior around dispatch() def _check_permissions(self): """ - Check user permissions and either raise an ``ErrorResponse`` or return. + Check user permissions and either raise an ``ImmediateResponse`` or return. """ user = self.user for permission_cls in self.permissions: @@ -223,7 +231,7 @@ class ResourceMixin(object): """ Returns the cleaned, validated request content. - May raise an :class:`response.ErrorResponse` with status code 400 (Bad Request). + May raise an :class:`response.ImmediateResponse` with status code 400 (Bad Request). """ if not hasattr(self, '_content'): self._content = self.validate_request(self.request.DATA, self.request.FILES) @@ -234,7 +242,7 @@ class ResourceMixin(object): """ Returns the cleaned, validated query parameters. - May raise an :class:`response.ErrorResponse` with status code 400 (Bad Request). + May raise an :class:`response.ImmediateResponse` with status code 400 (Bad Request). """ return self.validate_request(self.request.GET) @@ -253,7 +261,7 @@ class ResourceMixin(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. + May raise an :class:`response.ImmediateResponse` with status code 400 (Bad Request) on failure. """ return self._resource.validate_request(data, files) @@ -384,7 +392,7 @@ class ReadModelMixin(ModelMixin): try: self.model_instance = self.get_instance(**query_kwargs) except model.DoesNotExist: - raise ErrorResponse(status=status.HTTP_404_NOT_FOUND) + raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND) return self.model_instance @@ -463,7 +471,7 @@ class DeleteModelMixin(ModelMixin): try: instance = self.get_instance(**query_kwargs) except model.DoesNotExist: - raise ErrorResponse(status=status.HTTP_404_NOT_FOUND) + raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND) instance.delete() return Response() @@ -570,12 +578,12 @@ class PaginatorMixin(object): try: page_num = int(self.request.GET.get('page', '1')) except ValueError: - raise ErrorResponse( + raise ImmediateResponse( content={'detail': 'That page contains no results'}, status=status.HTTP_404_NOT_FOUND) if page_num not in paginator.page_range: - raise ErrorResponse( + raise ImmediateResponse( content={'detail': 'That page contains no results'}, status=status.HTTP_404_NOT_FOUND) -- cgit v1.2.3 From 21292d31e7ad5ec731c9ef3e471f90cb29054686 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 7 Feb 2012 15:38:54 +0200 Subject: cleaned Request/Response/mixins to have similar interface --- djangorestframework/mixins.py | 86 +++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 45 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index c30ef10b..c1f755b8 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -12,7 +12,7 @@ from djangorestframework import status from djangorestframework.renderers import BaseRenderer from djangorestframework.resources import Resource, FormResource, ModelResource from djangorestframework.response import Response, ImmediateResponse -from djangorestframework.request import request_class_factory +from djangorestframework.request import Request from djangorestframework.utils import as_tuple, allowed_methods @@ -32,7 +32,6 @@ __all__ = ( 'ListModelMixin' ) -#TODO: In RequestMixin and ResponseMixin : get_response_class/get_request_class are a bit ugly. Do we even want to be able to set the parameters on the view ? ########## Request Mixin ########## @@ -41,39 +40,43 @@ class RequestMixin(object): `Mixin` class to enhance API of Django's standard `request`. """ - _USE_FORM_OVERLOADING = True - _METHOD_PARAM = '_method' - _CONTENTTYPE_PARAM = '_content_type' - _CONTENT_PARAM = '_content' - - parsers = () + parser_classes = () """ - The set of parsers that the request can handle. + The set of parsers that the view can handle. Should be a tuple/list of classes as described in the :mod:`parsers` module. """ - def get_request_class(self): + request_class = Request + """ + The class to use as a wrapper for the original request object. + """ + + def get_parsers(self): """ - Returns a subclass of Django's `HttpRequest` with a richer API, - as described in :mod:`request`. + Instantiates and returns the list of parsers that will be used by the request + to parse its content. """ - if not hasattr(self, '_request_class'): - self._request_class = request_class_factory(self.request) - self._request_class._USE_FORM_OVERLOADING = self._USE_FORM_OVERLOADING - self._request_class._METHOD_PARAM = self._METHOD_PARAM - self._request_class._CONTENTTYPE_PARAM = self._CONTENTTYPE_PARAM - self._request_class._CONTENT_PARAM = self._CONTENT_PARAM - self._request_class.parsers = self.parsers - return self._request_class + if not hasattr(self, '_parsers'): + self._parsers = [r(self) for r in self.parser_classes] + return self._parsers - def get_request(self): + def prepare_request(self, request): """ - Returns a custom request instance, with data and attributes copied from the - original request. + Prepares the request for the request cycle. Returns a custom request instance, + with data and attributes copied from the original request. """ - request_class = self.get_request_class() - return request_class(self.request) + parsers = self.get_parsers() + request = self.request_class(request, parsers=parsers) + self.request = request + return request + + @property + def _parsed_media_types(self): + """ + Return a list of all the media types that this view can parse. + """ + return [p.media_type for p in self.parser_classes] ########## ResponseMixin ########## @@ -105,8 +108,8 @@ class ResponseMixin(object): def prepare_response(self, response): """ - Prepares the response for the response cycle. This has no effect if the - response is not an instance of :class:`response.Response`. + Prepares the response for the response cycle, and returns the prepared response. + This has no effect if the response is not an instance of :class:`response.Response`. """ if hasattr(response, 'request') and response.request is None: response.request = self.request @@ -124,6 +127,17 @@ class ResponseMixin(object): self.response = response return response + @property + def headers(self): + """ + Dictionary of headers to set on the response. + This is useful when the response doesn't exist yet, but you + want to memorize some headers to set on it when it will exist. + """ + if not hasattr(self, '_headers'): + self._headers = {} + return self._headers + @property def _rendered_media_types(self): """ @@ -138,24 +152,6 @@ class ResponseMixin(object): """ return [renderer.format for renderer in self.get_renderers()] - @property - def _default_renderer(self): - """ - Return the view's default renderer class. - """ - return self.get_renderers()[0] - - @property - def headers(self): - """ - Dictionary of headers to set on the response. - This is useful when the response doesn't exist yet, but you - want to memorize some headers to set on it when it will exist. - """ - if not hasattr(self, '_headers'): - self._headers = {} - return self._headers - ########## Auth Mixin ########## -- cgit v1.2.3 From 6963fd3623ee217fe489abb25f0ffa8c0781e4cd Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 7 Feb 2012 16:22:14 +0200 Subject: some docs for Request/Response/mixins --- djangorestframework/mixins.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index c1f755b8..ef4965a5 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -37,7 +37,7 @@ __all__ = ( class RequestMixin(object): """ - `Mixin` class to enhance API of Django's standard `request`. + `Mixin` class enabling the use of :class:`request.Request` in your views. """ parser_classes = () @@ -63,8 +63,8 @@ class RequestMixin(object): def prepare_request(self, request): """ - Prepares the request for the request cycle. Returns a custom request instance, - with data and attributes copied from the original request. + Prepares the request cycle. Returns an instance of :class:`request.Request`, + wrapping the original request object. """ parsers = self.get_parsers() request = self.request_class(request, parsers=parsers) @@ -74,7 +74,7 @@ class RequestMixin(object): @property def _parsed_media_types(self): """ - Return a list of all the media types that this view can parse. + Returns a list of all the media types that this view can parse. """ return [p.media_type for p in self.parser_classes] @@ -83,11 +83,7 @@ class RequestMixin(object): class ResponseMixin(object): """ - Adds behavior for pluggable `renderers` to a :class:`views.View` class. - - Default behavior is to use standard HTTP Accept header content negotiation. - Also supports overriding the content type by specifying an ``_accept=`` parameter in the URL. - Ignores Accept headers from Internet Explorer user agents and uses a sensible browser Accept header instead. + `Mixin` class enabling the use of :class:`response.Response` in your views. """ renderer_classes = () @@ -108,7 +104,7 @@ class ResponseMixin(object): def prepare_response(self, response): """ - Prepares the response for the response cycle, and returns the prepared response. + Prepares and returns `response`. This has no effect if the response is not an instance of :class:`response.Response`. """ if hasattr(response, 'request') and response.request is None: -- cgit v1.2.3 From 2cdff1b01e3aca6c56cef433e786e3ae75362739 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 7 Feb 2012 16:52:15 +0200 Subject: modified examples, somethin' still broken, can't find what --- djangorestframework/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index ef4965a5..57b85595 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -58,7 +58,7 @@ class RequestMixin(object): to parse its content. """ if not hasattr(self, '_parsers'): - self._parsers = [r(self) for r in self.parser_classes] + self._parsers = [p(self) for p in self.parser_classes] return self._parsers def prepare_request(self, request): -- cgit v1.2.3 From db0b01037a95946938ccd44eae14d8779bfff1a9 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Fri, 10 Feb 2012 10:18:39 +0200 Subject: made suggested fixes --- djangorestframework/mixins.py | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 57b85595..516a0f4b 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -43,7 +43,6 @@ class RequestMixin(object): parser_classes = () """ The set of parsers that the view can handle. - Should be a tuple/list of classes as described in the :mod:`parsers` module. """ @@ -54,22 +53,18 @@ class RequestMixin(object): def get_parsers(self): """ - Instantiates and returns the list of parsers that will be used by the request - to parse its content. + Instantiates and returns the list of parsers the request will use. """ - if not hasattr(self, '_parsers'): - self._parsers = [p(self) for p in self.parser_classes] - return self._parsers + return [p(self) for p in self.parser_classes] - def prepare_request(self, request): + def create_request(self, request): """ - Prepares the request cycle. Returns an instance of :class:`request.Request`, - wrapping the original request object. + Creates and returns an instance of :class:`request.Request`. + This new instance wraps the `request` passed as a parameter, and use the + parsers set on the view. """ parsers = self.get_parsers() - request = self.request_class(request, parsers=parsers) - self.request = request - return request + return self.request_class(request, parsers=parsers) @property def _parsed_media_types(self): @@ -89,38 +84,29 @@ class ResponseMixin(object): renderer_classes = () """ The set of response renderers that the view can handle. - Should be a tuple/list of classes as described in the :mod:`renderers` module. """ def get_renderers(self): """ - Instantiates and returns the list of renderers that will be used to render - the response. + Instantiates and returns the list of renderers the response will use. """ - if not hasattr(self, '_renderers'): - self._renderers = [r(self) for r in self.renderer_classes] - return self._renderers + return [r(self) for r in self.renderer_classes] def prepare_response(self, response): """ - Prepares and returns `response`. + Prepares and returns `response`. This has no effect if the response is not an instance of :class:`response.Response`. """ if hasattr(response, 'request') and response.request is None: response.request = self.request - # Always add these headers. - response['Allow'] = ', '.join(allowed_methods(self)) - # sample to allow caching using Vary http header - response['Vary'] = 'Authenticate, Accept' - # merge with headers possibly set at some point in the view + # set all the cached headers for name, value in self.headers.items(): response[name] = value # set the views renderers on the response response.renderers = self.get_renderers() - self.response = response return response @property @@ -571,12 +557,12 @@ class PaginatorMixin(object): page_num = int(self.request.GET.get('page', '1')) except ValueError: raise ImmediateResponse( - content={'detail': 'That page contains no results'}, + {'detail': 'That page contains no results'}, status=status.HTTP_404_NOT_FOUND) if page_num not in paginator.page_range: raise ImmediateResponse( - content={'detail': 'That page contains no results'}, + {'detail': 'That page contains no results'}, status=status.HTTP_404_NOT_FOUND) page = paginator.page(page_num) -- cgit v1.2.3 From b33579a7a18c2cbc6e3789d4a7dc78c82fb0fe80 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Fri, 10 Feb 2012 11:05:20 +0200 Subject: attempt at fixing the examples --- djangorestframework/mixins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 516a0f4b..43dce870 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -372,7 +372,7 @@ class ReadModelMixin(ModelMixin): except model.DoesNotExist: raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND) - return self.model_instance + return Response(self.model_instance) class CreateModelMixin(ModelMixin): @@ -428,7 +428,7 @@ class UpdateModelMixin(ModelMixin): # TODO: update on the url of a non-existing resource url doesn't work # correctly at the moment - will end up with a new url try: - self.model_instance = self.get_instance(*query_kwargs) + self.model_instance = self.get_instance(**query_kwargs) for (key, val) in self.CONTENT.items(): setattr(self.model_instance, key, val) -- cgit v1.2.3 From 21fcd3a90631e96e3fa210dd526abab9571ad6e1 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 20 Feb 2012 09:36:03 +0000 Subject: Some cleanup --- djangorestframework/mixins.py | 1 - 1 file changed, 1 deletion(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index cf746839..aae0f76f 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -13,7 +13,6 @@ from djangorestframework.renderers import BaseRenderer from djangorestframework.resources import Resource, FormResource, ModelResource from djangorestframework.response import Response, ImmediateResponse from djangorestframework.request import Request -from djangorestframework.utils import as_tuple, allowed_methods __all__ = ( -- cgit v1.2.3 From af9e4f69d732cc643d6ec7ae13d4a19ac0332d44 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 21 Feb 2012 20:12:14 +0000 Subject: Merging master into develop --- djangorestframework/mixins.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index aae0f76f..51c859cd 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -497,12 +497,12 @@ class PaginatorMixin(object): """ Constructs a url used for getting the next/previous urls """ - url = URLObject.parse(self.request.get_full_path()) - url = url.set_query_param('page', page_number) + url = URLObject(self.request.get_full_path()) + url = url.set_query_param('page', str(page_number)) limit = self.get_limit() if limit != self.limit: - url = url.add_query_param('limit', limit) + url = url.set_query_param('limit', str(limit)) return url -- cgit v1.2.3 From afd490238a38c5445013f030547b1019f484f0bc Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Thu, 23 Feb 2012 22:47:45 +0200 Subject: authentication refactor : request.user + tests pass --- djangorestframework/mixins.py | 63 ++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 37 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 51c859cd..398ed28a 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -3,7 +3,6 @@ The :mod:`mixins` module provides a set of reusable `mixin` classes that can be added to a `View`. """ -from django.contrib.auth.models import AnonymousUser from django.core.paginator import Paginator from django.db.models.fields.related import ForeignKey from urlobject import URLObject @@ -19,7 +18,7 @@ __all__ = ( # Base behavior mixins 'RequestMixin', 'ResponseMixin', - 'AuthMixin', + 'PermissionsMixin', 'ResourceMixin', # Reverse URL lookup behavior 'InstanceMixin', @@ -45,6 +44,13 @@ class RequestMixin(object): Should be a tuple/list of classes as described in the :mod:`parsers` module. """ + authentication_classes = () + """ + The set of authentication types that this view can handle. + + Should be a tuple/list of classes as described in the :mod:`authentication` module. + """ + request_class = Request """ The class to use as a wrapper for the original request object. @@ -56,6 +62,12 @@ class RequestMixin(object): """ return [p(self) for p in self.parser_classes] + def get_authentications(self): + """ + Instantiates and returns the list of authentications the request will use. + """ + return [a(self) for a in self.authentication_classes] + def create_request(self, request): """ Creates and returns an instance of :class:`request.Request`. @@ -63,7 +75,9 @@ class RequestMixin(object): parsers set on the view. """ parsers = self.get_parsers() - return self.request_class(request, parsers=parsers) + authentications = self.get_authentications() + return self.request_class(request, parsers=parsers, + authentications=authentications) @property def _parsed_media_types(self): @@ -134,57 +148,32 @@ class ResponseMixin(object): return [renderer.format for renderer in self.get_renderers()] -########## Auth Mixin ########## +########## Permissions Mixin ########## -class AuthMixin(object): +class PermissionsMixin(object): """ - Simple :class:`mixin` class to add authentication and permission checking to a :class:`View` class. + Simple :class:`mixin` class to add permission checking to a :class:`View` class. """ - authentication = () - """ - The set of authentication types that this view can handle. - - Should be a tuple/list of classes as described in the :mod:`authentication` module. - """ - - permissions = () + permissions_classes = () """ The set of permissions that will be enforced on this view. Should be a tuple/list of classes as described in the :mod:`permissions` module. """ - @property - def user(self): - """ - Returns the :obj:`user` for the current request, as determined by the set of - :class:`authentication` classes applied to the :class:`View`. - """ - if not hasattr(self, '_user'): - self._user = self._authenticate() - return self._user - - def _authenticate(self): + def get_permissions(self): """ - Attempt to authenticate the request using each authentication class in turn. - Returns a ``User`` object, which may be ``AnonymousUser``. + Instantiates and returns the list of permissions that this view requires. """ - for authentication_cls in self.authentication: - authentication = authentication_cls(self) - user = authentication.authenticate(self.request) - if user: - return user - return AnonymousUser() + return [p(self) for p in self.permissions_classes] # TODO: wrap this behavior around dispatch() - def _check_permissions(self): + def check_permissions(self, user): """ Check user permissions and either raise an ``ImmediateResponse`` or return. """ - user = self.user - for permission_cls in self.permissions: - permission = permission_cls(self) + for permission in self.get_permissions(): permission.check_permission(user) -- cgit v1.2.3 From 1cde31c86d9423e9b7a7409c2ef2ba7c0500e47f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Sat, 25 Feb 2012 18:45:17 +0000 Subject: Massive merge --- djangorestframework/mixins.py | 109 ++++++++++-------------------------------- 1 file changed, 26 insertions(+), 83 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 51c859cd..f95ec60f 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -21,14 +21,13 @@ __all__ = ( 'ResponseMixin', 'AuthMixin', 'ResourceMixin', - # Reverse URL lookup behavior - 'InstanceMixin', # Model behavior mixins 'ReadModelMixin', 'CreateModelMixin', 'UpdateModelMixin', 'DeleteModelMixin', - 'ListModelMixin' + 'ListModelMixin', + 'PaginatorMixin' ) @@ -39,39 +38,33 @@ class RequestMixin(object): `Mixin` class enabling the use of :class:`request.Request` in your views. """ - parser_classes = () - """ - The set of parsers that the view can handle. - Should be a tuple/list of classes as described in the :mod:`parsers` module. - """ - request_class = Request """ The class to use as a wrapper for the original request object. """ - def get_parsers(self): - """ - Instantiates and returns the list of parsers the request will use. - """ - return [p(self) for p in self.parser_classes] - def create_request(self, request): """ Creates and returns an instance of :class:`request.Request`. - This new instance wraps the `request` passed as a parameter, and use the - parsers set on the view. + This new instance wraps the `request` passed as a parameter, and use + the parsers set on the view. """ - parsers = self.get_parsers() - return self.request_class(request, parsers=parsers) + return self.request_class(request, parsers=self.parsers) @property def _parsed_media_types(self): """ - Returns a list of all the media types that this view can parse. + Return a list of all the media types that this view can parse. + """ + return [parser.media_type for parser in self.parsers] + + @property + def _default_parser(self): + """ + Return the view's default parser class. """ - return [p.media_type for p in self.parser_classes] - + return self.parsers[0] + ########## ResponseMixin ########## @@ -80,58 +73,32 @@ class ResponseMixin(object): `Mixin` class enabling the use of :class:`response.Response` in your views. """ - renderer_classes = () + renderers = () """ The set of response renderers that the view can handle. Should be a tuple/list of classes as described in the :mod:`renderers` module. """ - def get_renderers(self): - """ - Instantiates and returns the list of renderers the response will use. - """ - return [r(self) for r in self.renderer_classes] - - def prepare_response(self, response): - """ - Prepares and returns `response`. - This has no effect if the response is not an instance of :class:`response.Response`. - """ - if hasattr(response, 'request') and response.request is None: - response.request = self.request - - # set all the cached headers - for name, value in self.headers.items(): - response[name] = value - - # set the views renderers on the response - response.renderers = self.get_renderers() - return response - @property - def headers(self): + def _rendered_media_types(self): """ - Dictionary of headers to set on the response. - This is useful when the response doesn't exist yet, but you - want to memorize some headers to set on it when it will exist. + Return an list of all the media types that this response can render. """ - if not hasattr(self, '_headers'): - self._headers = {} - return self._headers + return [renderer.media_type for renderer in self.renderers] @property - def _rendered_media_types(self): + def _rendered_formats(self): """ - Return an list of all the media types that this view can render. + Return a list of all the formats that this response can render. """ - return [renderer.media_type for renderer in self.get_renderers()] + return [renderer.format for renderer in self.renderers] @property - def _rendered_formats(self): + def _default_renderer(self): """ - Return a list of all the formats that this view can render. + Return the response's default renderer class. """ - return [renderer.format for renderer in self.get_renderers()] + return self.renderers[0] ########## Auth Mixin ########## @@ -254,30 +221,6 @@ class ResourceMixin(object): else: return None -########## - - -class InstanceMixin(object): - """ - `Mixin` class that is used to identify a `View` class as being the canonical identifier - for the resources it is mapped to. - """ - - @classmethod - def as_view(cls, **initkwargs): - """ - Store the callable object on the resource class that has been associated with this view. - """ - view = super(InstanceMixin, cls).as_view(**initkwargs) - resource = getattr(cls(**initkwargs), 'resource', None) - if resource: - # We do a little dance when we store the view callable... - # we need to store it wrapped in a 1-tuple, so that inspect will treat it - # as a function when we later look it up (rather than turning it into a method). - # This makes sure our URL reversing works ok. - resource.view_callable = (view,) - return view - ########## Model Mixins ########## @@ -411,7 +354,7 @@ class CreateModelMixin(ModelMixin): response = Response(instance, status=status.HTTP_201_CREATED) # Set headers - if hasattr(instance, 'get_absolute_url'): + if hasattr(self.resource, 'url'): response['Location'] = self.resource(self).url(instance) return response -- cgit v1.2.3 From 4e4584a01a4cf67c23aec21088110cd477ba841b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 24 Aug 2012 20:50:24 +0100 Subject: Remove RequestMixinx / ReponseMixin --- djangorestframework/mixins.py | 73 ------------------------------------------- 1 file changed, 73 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 3142b093..d1014a84 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -11,13 +11,10 @@ from djangorestframework import status from djangorestframework.renderers import BaseRenderer from djangorestframework.resources import Resource, FormResource, ModelResource from djangorestframework.response import Response, ImmediateResponse -from djangorestframework.request import Request __all__ = ( # Base behavior mixins - 'RequestMixin', - 'ResponseMixin', 'PermissionsMixin', 'ResourceMixin', # Model behavior mixins @@ -30,76 +27,6 @@ __all__ = ( ) -########## Request Mixin ########## - -class RequestMixin(object): - """ - `Mixin` class enabling the use of :class:`request.Request` in your views. - """ - - request_class = Request - """ - The class to use as a wrapper for the original request object. - """ - - def create_request(self, request): - """ - Creates and returns an instance of :class:`request.Request`. - This new instance wraps the `request` passed as a parameter, and use - the parsers set on the view. - """ - return self.request_class(request, parsers=self.parsers, authentication=self.authentication) - - @property - def _parsed_media_types(self): - """ - Return a list of all the media types that this view can parse. - """ - return [parser.media_type for parser in self.parsers] - - @property - def _default_parser(self): - """ - Return the view's default parser class. - """ - return self.parsers[0] - - -########## ResponseMixin ########## - -class ResponseMixin(object): - """ - `Mixin` class enabling the use of :class:`response.Response` in your views. - """ - - renderers = () - """ - The set of response renderers that the view can handle. - Should be a tuple/list of classes as described in the :mod:`renderers` module. - """ - - @property - def _rendered_media_types(self): - """ - Return an list of all the media types that this response can render. - """ - return [renderer.media_type for renderer in self.renderers] - - @property - def _rendered_formats(self): - """ - Return a list of all the formats that this response can render. - """ - return [renderer.format for renderer in self.renderers] - - @property - def _default_renderer(self): - """ - Return the response's default renderer class. - """ - return self.renderers[0] - - ########## Permissions Mixin ########## class PermissionsMixin(object): -- cgit v1.2.3 From 87b363f7bc5f73d850df123a61895d65ec0b05e7 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 24 Aug 2012 20:57:10 +0100 Subject: Remove PermissionsMixin --- djangorestframework/mixins.py | 30 ------------------------------ 1 file changed, 30 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index d1014a84..28fa5847 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -15,7 +15,6 @@ from djangorestframework.response import Response, ImmediateResponse __all__ = ( # Base behavior mixins - 'PermissionsMixin', 'ResourceMixin', # Model behavior mixins 'ReadModelMixin', @@ -27,35 +26,6 @@ __all__ = ( ) -########## Permissions Mixin ########## - -class PermissionsMixin(object): - """ - Simple :class:`mixin` class to add permission checking to a :class:`View` class. - """ - - permissions_classes = () - """ - The set of permissions that will be enforced on this view. - - Should be a tuple/list of classes as described in the :mod:`permissions` module. - """ - - def get_permissions(self): - """ - Instantiates and returns the list of permissions that this view requires. - """ - return [p(self) for p in self.permissions_classes] - - # TODO: wrap this behavior around dispatch() - def check_permissions(self, user): - """ - Check user permissions and either raise an ``ImmediateResponse`` or return. - """ - for permission in self.get_permissions(): - permission.check_permission(user) - - ########## Resource Mixin ########## class ResourceMixin(object): -- cgit v1.2.3 From aed26b218ea39110489e85abc6f412399a1774a1 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 24 Aug 2012 22:11:00 +0100 Subject: Drop out resources & mixins --- djangorestframework/mixins.py | 388 ------------------------------------------ 1 file changed, 388 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 28fa5847..e69de29b 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -1,388 +0,0 @@ -""" -The :mod:`mixins` module provides a set of reusable `mixin` -classes that can be added to a `View`. -""" - -from django.core.paginator import Paginator -from django.db.models.fields.related import ForeignKey -from urlobject import URLObject - -from djangorestframework import status -from djangorestframework.renderers import BaseRenderer -from djangorestframework.resources import Resource, FormResource, ModelResource -from djangorestframework.response import Response, ImmediateResponse - - -__all__ = ( - # Base behavior mixins - 'ResourceMixin', - # Model behavior mixins - 'ReadModelMixin', - 'CreateModelMixin', - 'UpdateModelMixin', - 'DeleteModelMixin', - 'ListModelMixin', - 'PaginatorMixin' -) - - -########## Resource Mixin ########## - -class ResourceMixin(object): - """ - Provides request validation and response filtering behavior. - - Should be a class as described in the :mod:`resources` module. - - The :obj:`resource` is an object that maps a view onto it's representation on the server. - - It provides validation on the content of incoming requests, - and filters the object representation into a serializable object for the response. - """ - resource = None - - @property - def CONTENT(self): - """ - Returns the cleaned, validated request content. - - May raise an :class:`response.ImmediateResponse` with status code 400 (Bad Request). - """ - if not hasattr(self, '_content'): - self._content = self.validate_request(self.request.DATA, self.request.FILES) - return self._content - - @property - def PARAMS(self): - """ - Returns the cleaned, validated query parameters. - - May raise an :class:`response.ImmediateResponse` with status code 400 (Bad Request). - """ - return self.validate_request(self.request.GET) - - @property - def _resource(self): - if self.resource: - return self.resource(self) - elif getattr(self, 'model', None): - return ModelResource(self) - elif getattr(self, 'form', None): - return FormResource(self) - elif getattr(self, '%s_form' % self.request.method.lower(), None): - return FormResource(self) - return Resource(self) - - def validate_request(self, data, files=None): - """ - Given the request *data* and optional *files*, return the cleaned, validated content. - May raise an :class:`response.ImmediateResponse` 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 - - -########## Model Mixins ########## - -class ModelMixin(object): - """ Implements mechanisms used by other classes (like *ModelMixin group) to - define a query that represents Model instances the Mixin is working with. - - If a *ModelMixin is going to retrive an instance (or queryset) using args and kwargs - passed by as URL arguments, it should provied arguments to objects.get and objects.filter - methods wrapped in by `build_query` - - If a *ModelMixin is going to create/update an instance get_instance_data - handles the instance data creation/preaparation. - """ - - queryset = None - - def get_query_kwargs(self, *args, **kwargs): - """ - Return a dict of kwargs that will be used to build the - model instance retrieval or to filter querysets. - """ - - kwargs = dict(kwargs) - - # If the URLconf includes a .(?P\w+) pattern to match against - # a .json, .xml suffix, then drop the 'format' kwarg before - # constructing the query. - if BaseRenderer._FORMAT_QUERY_PARAM in kwargs: - del kwargs[BaseRenderer._FORMAT_QUERY_PARAM] - - return kwargs - - def get_instance_data(self, model, content, **kwargs): - """ - Returns the dict with the data for model instance creation/update. - - Arguments: - - model: model class (django.db.models.Model subclass) to work with - - content: a dictionary with instance data - - kwargs: a dict of URL provided keyword arguments - - The create/update queries are created basicly with the contet provided - with POST/PUT HTML methods and kwargs passed in the URL. This methods - simply merges the URL data and the content preaparing the ready-to-use - data dictionary. - """ - - tmp = dict(kwargs) - - for field in model._meta.fields: - if isinstance(field, ForeignKey) and field.name in tmp: - # translate 'related_field' kwargs into 'related_field_id' - tmp[field.name + '_id'] = tmp[field.name] - del tmp[field.name] - - all_kw_args = dict(content.items() + tmp.items()) - - return all_kw_args - - def get_instance(self, **kwargs): - """ - Get a model instance for read/update/delete requests. - """ - return self.get_queryset().get(**kwargs) - - def get_queryset(self): - """ - Return the queryset for this view. - """ - return getattr(self.resource, 'queryset', - self.resource.model.objects.all()) - - def get_ordering(self): - """ - Return the ordering for this view. - """ - return getattr(self.resource, 'ordering', None) - - -class ReadModelMixin(ModelMixin): - """ - Behavior to read a `model` instance on GET requests - """ - def get(self, request, *args, **kwargs): - model = self.resource.model - query_kwargs = self.get_query_kwargs(request, *args, **kwargs) - - try: - self.model_instance = self.get_instance(**query_kwargs) - except model.DoesNotExist: - raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND) - - return Response(self.model_instance) - - -class CreateModelMixin(ModelMixin): - """ - Behavior to create a `model` instance on POST requests - """ - def post(self, request, *args, **kwargs): - model = self.resource.model - - # Copy the dict to keep self.CONTENT intact - content = dict(self.CONTENT) - m2m_data = {} - - for field in model._meta.many_to_many: - if field.name in content: - m2m_data[field.name] = ( - field.m2m_reverse_field_name(), content[field.name] - ) - del content[field.name] - - instance = model(**self.get_instance_data(model, content, *args, **kwargs)) - instance.save() - - for fieldname in m2m_data: - manager = getattr(instance, fieldname) - - if hasattr(manager, 'add'): - manager.add(*m2m_data[fieldname][1]) - else: - data = {} - data[manager.source_field_name] = instance - - for related_item in m2m_data[fieldname][1]: - data[m2m_data[fieldname][0]] = related_item - manager.through(**data).save() - - response = Response(instance, status=status.HTTP_201_CREATED) - - # Set headers - if hasattr(self.resource, 'url'): - response['Location'] = self.resource(self).url(instance) - return response - - -class UpdateModelMixin(ModelMixin): - """ - Behavior to update a `model` instance on PUT requests - """ - def put(self, request, *args, **kwargs): - model = self.resource.model - query_kwargs = self.get_query_kwargs(request, *args, **kwargs) - - # TODO: update on the url of a non-existing resource url doesn't work - # correctly at the moment - will end up with a new url - try: - self.model_instance = self.get_instance(**query_kwargs) - - for (key, val) in self.CONTENT.items(): - setattr(self.model_instance, key, val) - except model.DoesNotExist: - self.model_instance = model(**self.get_instance_data(model, self.CONTENT, *args, **kwargs)) - self.model_instance.save() - return Response(self.model_instance) - - -class DeleteModelMixin(ModelMixin): - """ - Behavior to delete a `model` instance on DELETE requests - """ - def delete(self, request, *args, **kwargs): - model = self.resource.model - query_kwargs = self.get_query_kwargs(request, *args, **kwargs) - - try: - instance = self.get_instance(**query_kwargs) - except model.DoesNotExist: - raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND) - - instance.delete() - return Response() - - -class ListModelMixin(ModelMixin): - """ - Behavior to list a set of `model` instances on GET requests - """ - - def get(self, request, *args, **kwargs): - queryset = self.get_queryset() - ordering = self.get_ordering() - query_kwargs = self.get_query_kwargs(request, *args, **kwargs) - - queryset = queryset.filter(**query_kwargs) - if ordering: - queryset = queryset.order_by(*ordering) - - return Response(queryset) - - -########## Pagination Mixins ########## - -class PaginatorMixin(object): - """ - Adds pagination support to GET requests - Obviously should only be used on lists :) - - A default limit can be set by setting `limit` on the object. This will also - be used as the maximum if the client sets the `limit` GET param - """ - limit = 20 - - def get_limit(self): - """ - Helper method to determine what the `limit` should be - """ - try: - limit = int(self.request.GET.get('limit', self.limit)) - return min(limit, self.limit) - except ValueError: - return self.limit - - def url_with_page_number(self, page_number): - """ - Constructs a url used for getting the next/previous urls - """ - url = URLObject(self.request.get_full_path()) - url = url.set_query_param('page', str(page_number)) - - limit = self.get_limit() - if limit != self.limit: - url = url.set_query_param('limit', str(limit)) - - return url - - def next(self, page): - """ - Returns a url to the next page of results (if any) - """ - if not page.has_next(): - return None - - return self.url_with_page_number(page.next_page_number()) - - def previous(self, page): - """ Returns a url to the previous page of results (if any) """ - if not page.has_previous(): - return None - - return self.url_with_page_number(page.previous_page_number()) - - def serialize_page_info(self, page): - """ - This is some useful information that is added to the response - """ - return { - 'next': self.next(page), - 'page': page.number, - 'pages': page.paginator.num_pages, - 'per_page': self.get_limit(), - 'previous': self.previous(page), - 'total': page.paginator.count, - } - - def filter_response(self, obj): - """ - Given the response content, paginate and then serialize. - - The response is modified to include to useful data relating to the number - of objects, number of pages, next/previous urls etc. etc. - - The serialised objects are put into `results` on this new, modified - response - """ - - # We don't want to paginate responses for anything other than GET requests - if self.request.method.upper() != 'GET': - return self._resource.filter_response(obj) - - paginator = Paginator(obj, self.get_limit()) - - try: - page_num = int(self.request.GET.get('page', '1')) - except ValueError: - raise ImmediateResponse( - {'detail': 'That page contains no results'}, - status=status.HTTP_404_NOT_FOUND) - - if page_num not in paginator.page_range: - raise ImmediateResponse( - {'detail': 'That page contains no results'}, - status=status.HTTP_404_NOT_FOUND) - - page = paginator.page(page_num) - - serialized_object_list = self._resource.filter_response(page.object_list) - serialized_page_info = self.serialize_page_info(page) - - serialized_page_info['results'] = serialized_object_list - - return serialized_page_info -- cgit v1.2.3 From 6e21915934686cc7d46c8144403c933fa6fd2375 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 3 Sep 2012 17:49:22 +0100 Subject: First pass at mixins & generic views --- djangorestframework/mixins.py | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index e69de29b..2721f59e 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -0,0 +1,64 @@ +from djangorestframework import status +from djangorestframework.response import Response + + +class CreateModelMixin(object): + """ + Create a model instance. + Should be mixed in with any `APIView` + """ + def create(self, request, *args, **kwargs): + serializer = self.get_serializer(data=request.DATA) + if serializer.is_valid(): + self.object = serializer.object + self.object.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST) + + +class ListModelMixin(object): + """ + List a queryset. + Should be mixed in with `MultipleObjectBaseView`. + """ + def list(self, request, *args, **kwargs): + self.object_list = self.get_queryset() + serializer = self.get_serializer(instance=self.object_list) + return Response(serializer.data) + + +class RetrieveModelMixin(object): + """ + Retrieve a model instance. + Should be mixed in with `SingleObjectBaseView`. + """ + def retrieve(self, request, *args, **kwargs): + self.object = self.get_object() + serializer = self.get_serializer(instance=self.object) + return Response(serializer.data) + + +class UpdateModelMixin(object): + """ + Update a model instance. + Should be mixed in with `SingleObjectBaseView`. + """ + def update(self, request, *args, **kwargs): + self.object = self.get_object() + serializer = self.get_serializer(data=request.DATA, instance=self.object) + if serializer.is_valid(): + self.object = serializer.deserialized + self.object.save() + return Response(serializer.data) + return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST) + + +class DestroyModelMixin(object): + """ + Destroy a model instance. + Should be mixed in with `SingleObjectBaseView`. + """ + def destroy(self, request, *args, **kwargs): + self.object = self.get_object() + self.object.delete() + return Response(status=status.HTTP_204_NO_CONTENT) -- cgit v1.2.3 From 8457c871963264c9f62552f30307e98221a1c25d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 4 Sep 2012 12:02:05 +0100 Subject: Bits of cleanup --- djangorestframework/mixins.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 2721f59e..7269f298 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -2,10 +2,31 @@ from djangorestframework import status from djangorestframework.response import Response +class MetadataMixin(object): + """ + Should be mixed in with any `BaseView`. + """ + def metadata(self, request, *args, **kwargs): + content = { + 'name': self.get_name(), + 'description': self.get_description(), + 'renders': self._rendered_media_types, + 'parses': self._parsed_media_types, + } + # TODO: Add 'fields', from serializer info. + # form = self.get_bound_form() + # if form is not None: + # field_name_types = {} + # for name, field in form.fields.iteritems(): + # field_name_types[name] = field.__class__.__name__ + # content['fields'] = field_name_types + raise Response(content, status=status.HTTP_200_OK) + + class CreateModelMixin(object): """ Create a model instance. - Should be mixed in with any `APIView` + Should be mixed in with any `BaseView`. """ def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.DATA) @@ -47,7 +68,7 @@ class UpdateModelMixin(object): self.object = self.get_object() serializer = self.get_serializer(data=request.DATA, instance=self.object) if serializer.is_valid(): - self.object = serializer.deserialized + self.object = serializer.object self.object.save() return Response(serializer.data) return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST) -- cgit v1.2.3 From da4fa9bded9bbbbacf2a3009e1e211bdd51e287a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 5 Sep 2012 09:54:46 +0100 Subject: Minor tweaks --- djangorestframework/mixins.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 7269f298..2f78876f 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -2,27 +2,6 @@ from djangorestframework import status from djangorestframework.response import Response -class MetadataMixin(object): - """ - Should be mixed in with any `BaseView`. - """ - def metadata(self, request, *args, **kwargs): - content = { - 'name': self.get_name(), - 'description': self.get_description(), - 'renders': self._rendered_media_types, - 'parses': self._parsed_media_types, - } - # TODO: Add 'fields', from serializer info. - # form = self.get_bound_form() - # if form is not None: - # field_name_types = {} - # for name, field in form.fields.iteritems(): - # field_name_types[name] = field.__class__.__name__ - # content['fields'] = field_name_types - raise Response(content, status=status.HTTP_200_OK) - - class CreateModelMixin(object): """ Create a model instance. @@ -83,3 +62,25 @@ class DestroyModelMixin(object): self.object = self.get_object() self.object.delete() return Response(status=status.HTTP_204_NO_CONTENT) + + +class MetadataMixin(object): + """ + Return a dicitonary of view metadata. + Should be mixed in with any `BaseView`. + """ + def metadata(self, request, *args, **kwargs): + content = { + 'name': self.get_name(), + 'description': self.get_description(), + 'renders': self._rendered_media_types, + 'parses': self._parsed_media_types, + } + # TODO: Add 'fields', from serializer info. + # form = self.get_bound_form() + # if form is not None: + # field_name_types = {} + # for name, field in form.fields.iteritems(): + # field_name_types[name] = field.__class__.__name__ + # content['fields'] = field_name_types + raise Response(content, status=status.HTTP_200_OK) -- cgit v1.2.3 From 01d6a0899e4435417f774196b08e0613bd0a51f7 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 7 Sep 2012 09:36:52 +0100 Subject: Bits of cleanup --- djangorestframework/mixins.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 2f78876f..4b833aac 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -1,3 +1,12 @@ +""" +Basic building blocks for generic class based views. + +We don't bind behaviour to http method handlers yet, +which allows mixin classes to be composed in interesting ways. + +Eg. Use mixins to build a Resource class, and have a Router class + perform the binding of http methods to actions for us. +""" from djangorestframework import status from djangorestframework.response import Response @@ -68,6 +77,8 @@ class MetadataMixin(object): """ Return a dicitonary of view metadata. Should be mixed in with any `BaseView`. + + This mixin is typically used for the HTTP 'OPTIONS' method. """ def metadata(self, request, *args, **kwargs): content = { -- cgit v1.2.3 From 071e7d72cd3d0f48bd3c0f8e22da319a9c4d4a09 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 7 Sep 2012 10:57:04 +0100 Subject: Fix method overloading --- djangorestframework/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 4b833aac..6ab7ab6e 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -94,4 +94,4 @@ class MetadataMixin(object): # for name, field in form.fields.iteritems(): # field_name_types[name] = field.__class__.__name__ # content['fields'] = field_name_types - raise Response(content, status=status.HTTP_200_OK) + return Response(content, status=status.HTTP_200_OK) -- cgit v1.2.3 From 55f7dd9bcede90d6c5596e357035007b26a98dba Mon Sep 17 00:00:00 2001 From: Marko Tibold Date: Sat, 8 Sep 2012 21:56:18 +0200 Subject: `error_data` -> `errors` Prefill form for instance view. --- djangorestframework/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 6ab7ab6e..1f06dd34 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -22,7 +22,7 @@ class CreateModelMixin(object): self.object = serializer.object self.object.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) class ListModelMixin(object): -- cgit v1.2.3 From 308677037f1b1f2edbd2527beac8505033c98bdc Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 17 Sep 2012 20:19:45 +0100 Subject: Tweak docs, fix .error_data -> .errors --- djangorestframework/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py index 1f06dd34..ec27a4c1 100644 --- a/djangorestframework/mixins.py +++ b/djangorestframework/mixins.py @@ -59,7 +59,7 @@ class UpdateModelMixin(object): self.object = serializer.object self.object.save() return Response(serializer.data) - return Response(serializer.error_data, status=status.HTTP_400_BAD_REQUEST) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) class DestroyModelMixin(object): -- cgit v1.2.3 From 87dae4d8549c02fa9a57adb3bb876d249dae1f79 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 20 Sep 2012 13:19:43 +0100 Subject: Remove old 'djangorestframework directories --- djangorestframework/mixins.py | 97 ------------------------------------------- 1 file changed, 97 deletions(-) delete mode 100644 djangorestframework/mixins.py (limited to 'djangorestframework/mixins.py') diff --git a/djangorestframework/mixins.py b/djangorestframework/mixins.py deleted file mode 100644 index ec27a4c1..00000000 --- a/djangorestframework/mixins.py +++ /dev/null @@ -1,97 +0,0 @@ -""" -Basic building blocks for generic class based views. - -We don't bind behaviour to http method handlers yet, -which allows mixin classes to be composed in interesting ways. - -Eg. Use mixins to build a Resource class, and have a Router class - perform the binding of http methods to actions for us. -""" -from djangorestframework import status -from djangorestframework.response import Response - - -class CreateModelMixin(object): - """ - Create a model instance. - Should be mixed in with any `BaseView`. - """ - def create(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.DATA) - if serializer.is_valid(): - self.object = serializer.object - self.object.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - -class ListModelMixin(object): - """ - List a queryset. - Should be mixed in with `MultipleObjectBaseView`. - """ - def list(self, request, *args, **kwargs): - self.object_list = self.get_queryset() - serializer = self.get_serializer(instance=self.object_list) - return Response(serializer.data) - - -class RetrieveModelMixin(object): - """ - Retrieve a model instance. - Should be mixed in with `SingleObjectBaseView`. - """ - def retrieve(self, request, *args, **kwargs): - self.object = self.get_object() - serializer = self.get_serializer(instance=self.object) - return Response(serializer.data) - - -class UpdateModelMixin(object): - """ - Update a model instance. - Should be mixed in with `SingleObjectBaseView`. - """ - def update(self, request, *args, **kwargs): - self.object = self.get_object() - serializer = self.get_serializer(data=request.DATA, instance=self.object) - if serializer.is_valid(): - self.object = serializer.object - self.object.save() - return Response(serializer.data) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - -class DestroyModelMixin(object): - """ - Destroy a model instance. - Should be mixed in with `SingleObjectBaseView`. - """ - def destroy(self, request, *args, **kwargs): - self.object = self.get_object() - self.object.delete() - return Response(status=status.HTTP_204_NO_CONTENT) - - -class MetadataMixin(object): - """ - Return a dicitonary of view metadata. - Should be mixed in with any `BaseView`. - - This mixin is typically used for the HTTP 'OPTIONS' method. - """ - def metadata(self, request, *args, **kwargs): - content = { - 'name': self.get_name(), - 'description': self.get_description(), - 'renders': self._rendered_media_types, - 'parses': self._parsed_media_types, - } - # TODO: Add 'fields', from serializer info. - # form = self.get_bound_form() - # if form is not None: - # field_name_types = {} - # for name, field in form.fields.iteritems(): - # field_name_types[name] = field.__class__.__name__ - # content['fields'] = field_name_types - return Response(content, status=status.HTTP_200_OK) -- cgit v1.2.3