2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
The :mod:`mixins` module provides a set of reusable `mixin`
|
|
|
|
classes that can be added to a `View`.
|
2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-04-11 14:47:22 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
from django.db.models.fields.related import RelatedField
|
2011-04-11 19:54:02 +04:00
|
|
|
from django.http import HttpResponse
|
2011-05-10 15:51:49 +04:00
|
|
|
from django.http.multipartparser import LimitBytes
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
from djangorestframework import status
|
|
|
|
from djangorestframework.parsers import FormParser, MultiPartParser
|
2011-06-26 18:03:36 +04:00
|
|
|
from djangorestframework.renderers import BaseRenderer
|
2011-05-27 12:58:21 +04:00
|
|
|
from djangorestframework.resources import Resource, FormResource, ModelResource
|
2011-05-10 13:49:28 +04:00
|
|
|
from djangorestframework.response import Response, ErrorResponse
|
|
|
|
from djangorestframework.utils import as_tuple, MSIE_USER_AGENT_REGEX
|
2011-05-24 16:29:30 +04:00
|
|
|
from djangorestframework.utils.mediatypes import is_form_media_type, order_by_precedence
|
2011-05-10 13:49:28 +04:00
|
|
|
|
2011-04-11 19:54:02 +04:00
|
|
|
from decimal import Decimal
|
|
|
|
import re
|
2011-05-10 13:49:28 +04:00
|
|
|
from StringIO import StringIO
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
|
2011-05-10 15:21:48 +04:00
|
|
|
__all__ = (
|
2011-05-12 15:55:13 +04:00
|
|
|
# Base behavior mixins
|
2011-05-10 15:21:48 +04:00
|
|
|
'RequestMixin',
|
|
|
|
'ResponseMixin',
|
|
|
|
'AuthMixin',
|
2011-05-12 15:55:13 +04:00
|
|
|
'ResourceMixin',
|
2011-05-19 11:36:55 +04:00
|
|
|
# Reverse URL lookup behavior
|
2011-05-13 20:19:12 +04:00
|
|
|
'InstanceMixin',
|
2011-05-12 15:55:13 +04:00
|
|
|
# Model behavior mixins
|
2011-05-10 15:21:48 +04:00
|
|
|
'ReadModelMixin',
|
|
|
|
'CreateModelMixin',
|
|
|
|
'UpdateModelMixin',
|
|
|
|
'DeleteModelMixin',
|
|
|
|
'ListModelMixin'
|
|
|
|
)
|
2011-05-10 13:49:28 +04:00
|
|
|
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
########## Request Mixin ##########
|
2011-04-02 19:32:37 +04:00
|
|
|
|
|
|
|
class RequestMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
`Mixin` class to provide request parsing behavior.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
_USE_FORM_OVERLOADING = True
|
|
|
|
_METHOD_PARAM = '_method'
|
|
|
|
_CONTENTTYPE_PARAM = '_content_type'
|
|
|
|
_CONTENT_PARAM = '_content'
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
The set of request parsers that the view can handle.
|
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
Should be a tuple/list of classes as described in the :mod:`parsers` module.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-04-11 14:47:22 +04:00
|
|
|
parsers = ()
|
|
|
|
|
2011-05-12 18:11:14 +04:00
|
|
|
@property
|
|
|
|
def method(self):
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
Returns the HTTP method.
|
2011-05-13 12:59:36 +04:00
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
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.
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
|
|
|
if not hasattr(self, '_method'):
|
2011-05-12 19:03:14 +04:00
|
|
|
self._load_method_and_content_type()
|
2011-04-02 19:32:37 +04:00
|
|
|
return self._method
|
|
|
|
|
|
|
|
|
2011-05-12 18:11:14 +04:00
|
|
|
@property
|
|
|
|
def content_type(self):
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-10 13:49:28 +04:00
|
|
|
Returns the content type header.
|
2011-05-13 12:59:36 +04:00
|
|
|
|
|
|
|
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.
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
|
|
|
if not hasattr(self, '_content_type'):
|
2011-05-12 19:03:14 +04:00
|
|
|
self._load_method_and_content_type()
|
2011-04-02 19:32:37 +04:00
|
|
|
return self._content_type
|
|
|
|
|
|
|
|
|
2011-05-12 18:11:14 +04:00
|
|
|
@property
|
|
|
|
def DATA(self):
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-13 12:59:36 +04:00
|
|
|
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).
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
if not hasattr(self, '_data'):
|
|
|
|
self._load_data_and_files()
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def FILES(self):
|
|
|
|
"""
|
2011-05-13 12:59:36 +04:00
|
|
|
Parses the request body and returns the files.
|
2011-05-19 00:13:48 +04:00
|
|
|
Similar to ``request.FILES``, except that it handles arbitrary parsers,
|
2011-05-13 12:59:36 +04:00
|
|
|
and also works on methods other than POST (eg PUT).
|
2011-05-12 18:11:14 +04:00
|
|
|
"""
|
|
|
|
if not hasattr(self, '_files'):
|
|
|
|
self._load_data_and_files()
|
|
|
|
return self._files
|
|
|
|
|
|
|
|
|
|
|
|
def _load_data_and_files(self):
|
|
|
|
"""
|
|
|
|
Parse the request content into self.DATA and self.FILES.
|
|
|
|
"""
|
2011-05-12 19:03:14 +04:00
|
|
|
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)
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-05-12 19:03:14 +04:00
|
|
|
|
|
|
|
def _load_method_and_content_type(self):
|
2011-05-12 18:14:22 +04:00
|
|
|
"""
|
2011-05-12 19:03:14 +04:00
|
|
|
Set the method and content_type, and then check if they've been overridden.
|
2011-05-12 18:14:22 +04:00
|
|
|
"""
|
|
|
|
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()
|
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
|
|
|
|
def _get_stream(self):
|
|
|
|
"""
|
|
|
|
Returns an object that may be used to stream the request content.
|
|
|
|
"""
|
2011-05-12 19:03:14 +04:00
|
|
|
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.
|
2011-05-13 20:19:12 +04:00
|
|
|
# NOTE: Currently only supports parsing request body as a stream with 1.3
|
2011-05-12 19:03:14 +04:00
|
|
|
if content_length == 0:
|
|
|
|
return None
|
|
|
|
elif hasattr(request, 'read'):
|
|
|
|
return request
|
|
|
|
return StringIO(request.raw_post_data)
|
|
|
|
|
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
def _perform_form_overloading(self):
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-12 19:03:14 +04:00
|
|
|
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.
|
2011-04-02 19:32:37 +04:00
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
|
2011-05-12 19:03:14 +04:00
|
|
|
# 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):
|
2011-04-02 19:32:37 +04:00
|
|
|
return
|
2011-05-12 19:03:14 +04:00
|
|
|
|
|
|
|
# At this point we're committed to parsing the request as form data.
|
|
|
|
self._data = data = self.request.POST
|
|
|
|
self._files = self.request.FILES
|
|
|
|
|
|
|
|
# Method overloading - change the method and remove the param from the content.
|
|
|
|
if self._METHOD_PARAM in data:
|
|
|
|
self._method = data[self._METHOD_PARAM].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 = data[self._CONTENTTYPE_PARAM]
|
|
|
|
stream = StringIO(data[self._CONTENT_PARAM])
|
|
|
|
(self._data, self._files) = self._parse(stream, self._content_type)
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-04-11 16:52:16 +04:00
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
def _parse(self, stream, content_type):
|
2011-04-11 14:24:14 +04:00
|
|
|
"""
|
|
|
|
Parse the request content.
|
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
May raise a 415 ErrorResponse (Unsupported Media Type), or a 400 ErrorResponse (Bad Request).
|
2011-04-11 14:24:14 +04:00
|
|
|
"""
|
2011-04-11 14:47:22 +04:00
|
|
|
if stream is None or content_type is None:
|
2011-05-12 15:55:13 +04:00
|
|
|
return (None, None)
|
2011-04-11 14:47:22 +04:00
|
|
|
|
2011-04-11 14:24:14 +04:00
|
|
|
parsers = as_tuple(self.parsers)
|
|
|
|
|
|
|
|
for parser_cls in parsers:
|
2011-05-10 13:49:28 +04:00
|
|
|
parser = parser_cls(self)
|
|
|
|
if parser.can_handle_request(content_type):
|
|
|
|
return parser.parse(stream)
|
2011-04-11 14:24:14 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
raise ErrorResponse(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
|
|
|
{'error': 'Unsupported media type in request \'%s\'.' %
|
|
|
|
content_type})
|
2011-04-11 14:24:14 +04:00
|
|
|
|
2011-04-11 18:03:49 +04:00
|
|
|
|
2011-04-11 14:24:14 +04:00
|
|
|
@property
|
2011-05-12 18:11:14 +04:00
|
|
|
def _parsed_media_types(self):
|
2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
Return a list of all the media types that this view can parse.
|
2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-04-11 14:24:14 +04:00
|
|
|
return [parser.media_type for parser in self.parsers]
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2011-04-11 14:24:14 +04:00
|
|
|
|
|
|
|
@property
|
2011-05-12 18:11:14 +04:00
|
|
|
def _default_parser(self):
|
2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-05-24 16:29:30 +04:00
|
|
|
Return the view's default parser class.
|
2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-04-11 14:24:14 +04:00
|
|
|
return self.parsers[0]
|
|
|
|
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-04-11 19:54:02 +04:00
|
|
|
########## ResponseMixin ##########
|
|
|
|
|
|
|
|
class ResponseMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-24 13:27:24 +04:00
|
|
|
Adds behavior for pluggable `Renderers` to a :class:`views.View` class.
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2011-05-10 15:51:49 +04:00
|
|
|
Default behavior is to use standard HTTP Accept header content negotiation.
|
2011-05-19 00:13:48 +04:00
|
|
|
Also supports overriding the content type by specifying an ``_accept=`` parameter in the URL.
|
2011-05-10 13:49:28 +04:00
|
|
|
Ignores Accept headers from Internet Explorer user agents and uses a sensible browser Accept header instead.
|
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
|
|
|
|
_ACCEPT_QUERY_PARAM = '_accept' # Allow override of Accept header in URL query params
|
|
|
|
_IGNORE_IE_ACCEPT_HEADER = True
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
The set of response renderers that the view can handle.
|
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
Should be a tuple/list of classes as described in the :mod:`renderers` module.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-04-28 22:54:30 +04:00
|
|
|
renderers = ()
|
2011-05-13 12:59:36 +04:00
|
|
|
|
|
|
|
|
2011-05-10 15:59:13 +04:00
|
|
|
# TODO: wrap this behavior around dispatch(), ensuring it works
|
|
|
|
# out of the box with existing Django classes that use render_to_response.
|
2011-04-28 22:54:30 +04:00
|
|
|
def render(self, response):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Takes a :obj:`Response` object and returns an :obj:`HttpResponse`.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-04-11 19:54:02 +04:00
|
|
|
self.response = response
|
|
|
|
|
|
|
|
try:
|
2011-05-24 19:31:17 +04:00
|
|
|
renderer, media_type = self._determine_renderer(self.request)
|
2011-04-11 19:54:02 +04:00
|
|
|
except ErrorResponse, exc:
|
2011-05-24 16:29:30 +04:00
|
|
|
renderer = self._default_renderer(self)
|
2011-05-24 19:31:17 +04:00
|
|
|
media_type = renderer.media_type
|
2011-04-11 19:54:02 +04:00
|
|
|
response = exc.response
|
2011-05-24 19:31:17 +04:00
|
|
|
|
|
|
|
# Set the media type of the response
|
|
|
|
# Note that the renderer *could* override it in .render() if required.
|
|
|
|
response.media_type = renderer.media_type
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
# Serialize the response content
|
|
|
|
if response.has_content_body:
|
2011-05-24 19:31:17 +04:00
|
|
|
content = renderer.render(response.cleaned_content, media_type)
|
2011-04-11 19:54:02 +04:00
|
|
|
else:
|
2011-05-24 16:29:30 +04:00
|
|
|
content = renderer.render()
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
# Build the HTTP Response
|
2011-05-24 19:31:17 +04:00
|
|
|
resp = HttpResponse(content, mimetype=response.media_type, status=response.status)
|
2011-04-11 19:54:02 +04:00
|
|
|
for (key, val) in response.headers.items():
|
|
|
|
resp[key] = val
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2011-04-28 22:54:30 +04:00
|
|
|
def _determine_renderer(self, request):
|
2011-05-10 15:51:49 +04:00
|
|
|
"""
|
2011-05-24 19:31:17 +04:00
|
|
|
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)`
|
2011-05-10 19:01:58 +04:00
|
|
|
|
2011-05-10 15:51:49 +04:00
|
|
|
See: RFC 2616, Section 14 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
|
|
|
"""
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2011-05-12 18:11:14 +04:00
|
|
|
if self._ACCEPT_QUERY_PARAM and request.GET.get(self._ACCEPT_QUERY_PARAM, None):
|
2011-04-11 19:54:02 +04:00
|
|
|
# Use _accept parameter override
|
2011-05-12 18:11:14 +04:00
|
|
|
accept_list = [request.GET.get(self._ACCEPT_QUERY_PARAM)]
|
|
|
|
elif (self._IGNORE_IE_ACCEPT_HEADER and
|
2011-04-11 19:54:02 +04:00
|
|
|
request.META.has_key('HTTP_USER_AGENT') and
|
|
|
|
MSIE_USER_AGENT_REGEX.match(request.META['HTTP_USER_AGENT'])):
|
2011-05-24 16:29:30 +04:00
|
|
|
# Ignore MSIE's broken accept behavior and do something sensible instead
|
2011-04-11 19:54:02 +04:00
|
|
|
accept_list = ['text/html', '*/*']
|
|
|
|
elif request.META.has_key('HTTP_ACCEPT'):
|
|
|
|
# Use standard HTTP Accept negotiation
|
2011-05-24 16:29:30 +04:00
|
|
|
accept_list = [token.strip() for token in request.META["HTTP_ACCEPT"].split(',')]
|
2011-04-11 19:54:02 +04:00
|
|
|
else:
|
|
|
|
# No accept header specified
|
2011-06-26 18:03:36 +04:00
|
|
|
accept_list = ['*/*']
|
2011-05-24 16:29:30 +04:00
|
|
|
|
|
|
|
# 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 :)
|
2011-05-24 19:31:17 +04:00
|
|
|
# Worst case is we're looping over len(accept_list) * len(self.renderers)
|
2011-05-24 16:29:30 +04:00
|
|
|
renderers = [renderer_cls(self) for renderer_cls in self.renderers]
|
|
|
|
|
2011-06-26 18:03:36 +04:00
|
|
|
for accepted_media_type_lst in order_by_precedence(accept_list):
|
2011-05-24 16:29:30 +04:00
|
|
|
for renderer in renderers:
|
2011-06-26 18:03:36 +04:00
|
|
|
for accepted_media_type in accepted_media_type_lst:
|
|
|
|
if renderer.can_handle_response(accepted_media_type):
|
|
|
|
return renderer, accepted_media_type
|
|
|
|
|
2011-05-24 16:29:30 +04:00
|
|
|
# No acceptable renderers were found
|
2011-04-11 19:54:02 +04:00
|
|
|
raise ErrorResponse(status.HTTP_406_NOT_ACCEPTABLE,
|
|
|
|
{'detail': 'Could not satisfy the client\'s Accept header',
|
2011-05-12 18:11:14 +04:00
|
|
|
'available_types': self._rendered_media_types})
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2011-05-24 16:29:30 +04:00
|
|
|
|
2011-04-11 19:54:02 +04:00
|
|
|
@property
|
2011-05-12 18:11:14 +04:00
|
|
|
def _rendered_media_types(self):
|
2011-05-10 15:51:49 +04:00
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
Return an list of all the media types that this view can render.
|
2011-05-10 15:51:49 +04:00
|
|
|
"""
|
2011-04-28 22:54:30 +04:00
|
|
|
return [renderer.media_type for renderer in self.renderers]
|
2011-06-26 18:03:36 +04:00
|
|
|
|
|
|
|
@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]
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
@property
|
2011-05-12 18:11:14 +04:00
|
|
|
def _default_renderer(self):
|
2011-05-10 15:51:49 +04:00
|
|
|
"""
|
2011-05-24 16:29:30 +04:00
|
|
|
Return the view's default renderer class.
|
2011-05-10 15:51:49 +04:00
|
|
|
"""
|
2011-04-28 22:54:30 +04:00
|
|
|
return self.renderers[0]
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
|
|
|
|
########## Auth Mixin ##########
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2011-04-11 16:45:38 +04:00
|
|
|
class AuthMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Simple :class:`mixin` class to add authentication and permission checking to a :class:`View` class.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-13 12:59:36 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
The set of authentication types that this view can handle.
|
2011-05-19 11:49:57 +04:00
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
Should be a tuple/list of classes as described in the :mod:`authentication` module.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-04-29 17:32:56 +04:00
|
|
|
authentication = ()
|
2011-05-13 12:59:36 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
The set of permissions that will be enforced on this view.
|
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
Should be a tuple/list of classes as described in the :mod:`permissions` module.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-04-25 04:03:23 +04:00
|
|
|
permissions = ()
|
2011-04-11 16:45:38 +04:00
|
|
|
|
2011-05-13 12:59:36 +04:00
|
|
|
|
2011-04-11 16:45:38 +04:00
|
|
|
@property
|
2011-05-10 13:49:28 +04:00
|
|
|
def user(self):
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Returns the :obj:`user` for the current request, as determined by the set of
|
|
|
|
:class:`authentication` classes applied to the :class:`View`.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2011-05-10 13:49:28 +04:00
|
|
|
if not hasattr(self, '_user'):
|
|
|
|
self._user = self._authenticate()
|
|
|
|
return self._user
|
2011-05-13 20:19:12 +04:00
|
|
|
|
|
|
|
|
2011-04-25 04:03:23 +04:00
|
|
|
def _authenticate(self):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
|
|
|
Attempt to authenticate the request using each authentication class in turn.
|
|
|
|
Returns a ``User`` object, which may be ``AnonymousUser``.
|
|
|
|
"""
|
2011-04-29 17:32:56 +04:00
|
|
|
for authentication_cls in self.authentication:
|
|
|
|
authentication = authentication_cls(self)
|
2011-05-10 13:49:28 +04:00
|
|
|
user = authentication.authenticate(self.request)
|
|
|
|
if user:
|
|
|
|
return user
|
|
|
|
return AnonymousUser()
|
2011-04-11 16:45:38 +04:00
|
|
|
|
2011-05-13 12:59:36 +04:00
|
|
|
|
2011-05-10 15:51:49 +04:00
|
|
|
# TODO: wrap this behavior around dispatch()
|
2011-05-10 13:49:28 +04:00
|
|
|
def _check_permissions(self):
|
|
|
|
"""
|
|
|
|
Check user permissions and either raise an ``ErrorResponse`` or return.
|
|
|
|
"""
|
|
|
|
user = self.user
|
2011-04-25 04:03:23 +04:00
|
|
|
for permission_cls in self.permissions:
|
|
|
|
permission = permission_cls(self)
|
2011-05-13 12:59:36 +04:00
|
|
|
permission.check_permission(user)
|
2011-04-25 04:03:23 +04:00
|
|
|
|
2011-04-11 16:45:38 +04:00
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
########## Resource Mixin ##########
|
|
|
|
|
2011-05-12 18:11:14 +04:00
|
|
|
class ResourceMixin(object):
|
2011-05-13 20:19:12 +04:00
|
|
|
"""
|
|
|
|
Provides request validation and response filtering behavior.
|
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
Should be a class as described in the :mod:`resources` module.
|
2011-05-13 20:19:12 +04:00
|
|
|
|
2011-05-19 00:13:48 +04:00
|
|
|
The :obj:`resource` is an object that maps a view onto it's representation on the server.
|
2011-05-13 20:19:12 +04:00
|
|
|
|
|
|
|
It provides validation on the content of incoming requests,
|
|
|
|
and filters the object representation into a serializable object for the response.
|
|
|
|
"""
|
2011-05-27 12:58:21 +04:00
|
|
|
resource = None
|
2011-05-13 20:19:12 +04:00
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
@property
|
|
|
|
def CONTENT(self):
|
2011-05-17 12:15:35 +04:00
|
|
|
"""
|
|
|
|
Returns the cleaned, validated request content.
|
2011-05-27 17:40:19 +04:00
|
|
|
|
|
|
|
May raise an :class:`response.ErrorResponse` with status code 400 (Bad Request).
|
2011-05-17 12:15:35 +04:00
|
|
|
"""
|
2011-05-12 15:55:13 +04:00
|
|
|
if not hasattr(self, '_content'):
|
2011-05-13 20:19:12 +04:00
|
|
|
self._content = self.validate_request(self.DATA, self.FILES)
|
2011-05-12 15:55:13 +04:00
|
|
|
return self._content
|
|
|
|
|
2011-05-27 17:40:19 +04:00
|
|
|
@property
|
|
|
|
def PARAMS(self):
|
|
|
|
"""
|
|
|
|
Returns the cleaned, validated query parameters.
|
|
|
|
|
|
|
|
May raise an :class:`response.ErrorResponse` with status code 400 (Bad Request).
|
|
|
|
"""
|
|
|
|
return self.validate_request(self.request.GET)
|
|
|
|
|
2011-05-27 12:58:21 +04:00
|
|
|
@property
|
|
|
|
def _resource(self):
|
|
|
|
if self.resource:
|
|
|
|
return self.resource(self)
|
2011-05-27 13:35:43 +04:00
|
|
|
elif getattr(self, 'model', None):
|
2011-05-27 12:58:21 +04:00
|
|
|
return ModelResource(self)
|
2011-05-27 13:35:43 +04:00
|
|
|
elif getattr(self, 'form', None):
|
2011-05-27 12:58:21 +04:00
|
|
|
return FormResource(self)
|
2011-05-27 17:40:19 +04:00
|
|
|
elif getattr(self, '%s_form' % self.method.lower(), None):
|
|
|
|
return FormResource(self)
|
2011-05-27 12:58:21 +04:00
|
|
|
return Resource(self)
|
|
|
|
|
2011-05-27 17:40:19 +04:00
|
|
|
def validate_request(self, data, files=None):
|
2011-05-13 20:19:12 +04:00
|
|
|
"""
|
2011-05-27 17:40:19 +04:00
|
|
|
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.
|
2011-05-13 20:19:12 +04:00
|
|
|
"""
|
2011-05-27 12:58:21 +04:00
|
|
|
return self._resource.validate_request(data, files)
|
2011-05-13 20:19:12 +04:00
|
|
|
|
|
|
|
def filter_response(self, obj):
|
|
|
|
"""
|
|
|
|
Given the response content, filter it into a serializable object.
|
|
|
|
"""
|
2011-05-27 12:58:21 +04:00
|
|
|
return self._resource.filter_response(obj)
|
2011-05-12 15:55:13 +04:00
|
|
|
|
2011-05-27 17:40:19 +04:00
|
|
|
def get_bound_form(self, content=None, method=None):
|
|
|
|
return self._resource.get_bound_form(content, method=method)
|
2011-05-12 15:55:13 +04:00
|
|
|
|
2011-05-13 20:19:12 +04:00
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
|
2011-05-17 12:15:35 +04:00
|
|
|
##########
|
|
|
|
|
|
|
|
class InstanceMixin(object):
|
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
`Mixin` class that is used to identify a `View` class as being the canonical identifier
|
|
|
|
for the resources it is mapped to.
|
2011-05-17 12:15:35 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
@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)
|
2011-05-23 20:07:31 +04:00
|
|
|
resource = getattr(cls(**initkwargs), 'resource', None)
|
|
|
|
if resource:
|
2011-05-17 12:15:35 +04:00
|
|
|
# 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).
|
2011-06-15 17:09:01 +04:00
|
|
|
# This makes sure our URL reversing works ok.
|
2011-05-23 20:07:31 +04:00
|
|
|
resource.view_callable = (view,)
|
2011-05-17 12:15:35 +04:00
|
|
|
return view
|
|
|
|
|
2011-05-12 15:55:13 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
########## Model Mixins ##########
|
|
|
|
|
|
|
|
class ReadModelMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Behavior to read a `model` instance on GET requests
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-02 22:49:12 +04:00
|
|
|
def get(self, request, *args, **kwargs):
|
2011-05-04 12:21:17 +04:00
|
|
|
model = self.resource.model
|
2011-06-15 17:09:01 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
try:
|
|
|
|
if args:
|
|
|
|
# If we have any none kwargs then assume the last represents the primrary key
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance = model.objects.get(pk=args[-1], **kwargs)
|
2011-05-02 22:49:12 +04:00
|
|
|
else:
|
|
|
|
# Otherwise assume the kwargs uniquely identify the model
|
2011-06-26 18:03:36 +04:00
|
|
|
filtered_keywords = kwargs.copy()
|
|
|
|
if BaseRenderer._FORMAT_QUERY_PARAM in filtered_keywords:
|
|
|
|
del filtered_keywords[BaseRenderer._FORMAT_QUERY_PARAM]
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance = model.objects.get(**filtered_keywords)
|
2011-05-04 12:21:17 +04:00
|
|
|
except model.DoesNotExist:
|
2011-05-02 22:49:12 +04:00
|
|
|
raise ErrorResponse(status.HTTP_404_NOT_FOUND)
|
|
|
|
|
2011-07-01 10:32:04 +04:00
|
|
|
return self.model_instance
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
class CreateModelMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Behavior to create a `model` instance on POST requests
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-02 22:49:12 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2011-05-04 12:21:17 +04:00
|
|
|
model = self.resource.model
|
2011-06-15 17:09:01 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
# translated 'related_field' kwargs into 'related_field_id'
|
2011-05-04 12:21:17 +04:00
|
|
|
for related_name in [field.name for field in model._meta.fields if isinstance(field, RelatedField)]:
|
2011-05-02 22:49:12 +04:00
|
|
|
if kwargs.has_key(related_name):
|
|
|
|
kwargs[related_name + '_id'] = kwargs[related_name]
|
|
|
|
del kwargs[related_name]
|
|
|
|
|
|
|
|
all_kw_args = dict(self.CONTENT.items() + kwargs.items())
|
|
|
|
if args:
|
2011-05-04 12:21:17 +04:00
|
|
|
instance = model(pk=args[-1], **all_kw_args)
|
2011-05-02 22:49:12 +04:00
|
|
|
else:
|
2011-05-04 12:21:17 +04:00
|
|
|
instance = model(**all_kw_args)
|
2011-05-02 22:49:12 +04:00
|
|
|
instance.save()
|
|
|
|
headers = {}
|
|
|
|
if hasattr(instance, 'get_absolute_url'):
|
2011-05-16 17:11:36 +04:00
|
|
|
headers['Location'] = self.resource(self).url(instance)
|
2011-05-02 22:49:12 +04:00
|
|
|
return Response(status.HTTP_201_CREATED, instance, headers)
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateModelMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Behavior to update a `model` instance on PUT requests
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-02 22:49:12 +04:00
|
|
|
def put(self, request, *args, **kwargs):
|
2011-05-04 12:21:17 +04:00
|
|
|
model = self.resource.model
|
2011-06-15 17:09:01 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
# 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:
|
|
|
|
if args:
|
|
|
|
# If we have any none kwargs then assume the last represents the primrary key
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance = model.objects.get(pk=args[-1], **kwargs)
|
2011-05-02 22:49:12 +04:00
|
|
|
else:
|
|
|
|
# Otherwise assume the kwargs uniquely identify the model
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance = model.objects.get(**kwargs)
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
for (key, val) in self.CONTENT.items():
|
2011-07-01 10:32:04 +04:00
|
|
|
setattr(self.model_instance, key, val)
|
2011-05-04 12:21:17 +04:00
|
|
|
except model.DoesNotExist:
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance = model(**self.CONTENT)
|
|
|
|
self.model_instance.save()
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance.save()
|
|
|
|
return self.model_instance
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteModelMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Behavior to delete a `model` instance on DELETE requests
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-02 22:49:12 +04:00
|
|
|
def delete(self, request, *args, **kwargs):
|
2011-05-04 12:21:17 +04:00
|
|
|
model = self.resource.model
|
2011-06-15 17:09:01 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
try:
|
|
|
|
if args:
|
|
|
|
# If we have any none kwargs then assume the last represents the primrary key
|
2011-05-04 12:21:17 +04:00
|
|
|
instance = model.objects.get(pk=args[-1], **kwargs)
|
2011-05-02 22:49:12 +04:00
|
|
|
else:
|
|
|
|
# Otherwise assume the kwargs uniquely identify the model
|
2011-05-04 12:21:17 +04:00
|
|
|
instance = model.objects.get(**kwargs)
|
|
|
|
except model.DoesNotExist:
|
2011-05-02 22:49:12 +04:00
|
|
|
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
|
|
|
|
|
|
|
|
instance.delete()
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
class ListModelMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-19 00:13:48 +04:00
|
|
|
Behavior to list a set of `model` instances on GET requests
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-16 17:11:36 +04:00
|
|
|
|
|
|
|
# NB. Not obvious to me if it would be better to set this on the resource?
|
|
|
|
#
|
|
|
|
# Presumably it's more useful to have on the view, because that way you can
|
|
|
|
# have multiple views across different querysets mapping to the same resource.
|
|
|
|
#
|
|
|
|
# Perhaps it ought to be:
|
|
|
|
#
|
|
|
|
# 1) View.queryset
|
|
|
|
# 2) if None fall back to Resource.queryset
|
|
|
|
# 3) if None fall back to Resource.model.objects.all()
|
|
|
|
#
|
|
|
|
# Any feedback welcomed.
|
2011-05-02 22:49:12 +04:00
|
|
|
queryset = None
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2011-06-15 17:09:01 +04:00
|
|
|
model = self.resource.model
|
|
|
|
|
2011-07-01 14:30:28 +04:00
|
|
|
queryset = self.queryset if self.queryset is not None else model.objects.all()
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
if hasattr(self, 'resource'):
|
2011-06-15 17:09:01 +04:00
|
|
|
ordering = getattr(self.resource, 'ordering', None)
|
2011-06-14 21:22:13 +04:00
|
|
|
else:
|
|
|
|
ordering = None
|
|
|
|
|
2011-05-16 17:11:36 +04:00
|
|
|
if ordering:
|
|
|
|
args = as_tuple(ordering)
|
|
|
|
queryset = queryset.order_by(*args)
|
2011-05-02 22:49:12 +04:00
|
|
|
return queryset.filter(**kwargs)
|
|
|
|
|
|
|
|
|