django-rest-framework/djangorestframework/mixins.py

518 lines
18 KiB
Python
Raw Normal View History

"""
The mixins module provides a set of reusable mixin classes that can be added to a ``View``.
"""
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
from djangorestframework.response import Response, ErrorResponse
from djangorestframework.utils import as_tuple, MSIE_USER_AGENT_REGEX
from djangorestframework.utils.mediatypes import is_form_media_type
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__ = (
# Base behavior mixins
2011-05-10 15:21:48 +04:00
'RequestMixin',
'ResponseMixin',
'AuthMixin',
'ResourceMixin',
# 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 ##########
class RequestMixin(object):
2011-05-10 13:49:28 +04:00
"""
Mixin class to provide request parsing behavior.
2011-05-10 13:49:28 +04:00
"""
_USE_FORM_OVERLOADING = True
_METHOD_PARAM = '_method'
_CONTENTTYPE_PARAM = '_content_type'
_CONTENT_PARAM = '_content'
2011-04-11 14:47:22 +04:00
parsers = ()
2011-05-12 18:11:14 +04:00
@property
def method(self):
"""
2011-05-12 18:11:14 +04:00
Returns the HTTP method.
"""
if not hasattr(self, '_method'):
self._method = self.request.method
return self._method
2011-05-12 18:11:14 +04:00
@property
def content_type(self):
"""
2011-05-10 13:49:28 +04:00
Returns the content type header.
"""
if not hasattr(self, '_content_type'):
2011-05-10 13:49:28 +04:00
self._content_type = self.request.META.get('HTTP_CONTENT_TYPE', self.request.META.get('CONTENT_TYPE', ''))
return self._content_type
2011-05-12 18:11:14 +04:00
@property
def DATA(self):
"""
2011-05-12 18:11:14 +04:00
Returns the request data.
"""
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):
"""
Returns the request files.
"""
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.
"""
stream = self._get_stream()
(self._data, self._files) = self._parse(stream, self.content_type)
def _get_stream(self):
"""
Returns an object that may be used to stream the request content.
"""
if not hasattr(self, '_stream'):
request = self.request
2011-04-11 14:47:22 +04:00
try:
content_length = int(request.META.get('CONTENT_LENGTH', request.META.get('HTTP_CONTENT_LENGTH')))
except (ValueError, TypeError):
content_length = 0
2011-05-10 15:51:49 +04:00
# TODO: Add 1.3's LimitedStream to compat and use that.
# Currently only supports parsing request body as a stream with 1.3
2011-04-11 14:47:22 +04:00
if content_length == 0:
return None
elif hasattr(request, 'read'):
# UPDATE BASED ON COMMENT BELOW:
#
# Yup, this was a bug in Django - fixed and waiting check in - see ticket 15785.
# http://code.djangoproject.com/ticket/15785
#
# COMMENT:
#
# It's not at all clear if this needs to be byte limited or not.
# Maybe I'm just being dumb but it looks to me like there's some issues
# with that in Django.
#
# Either:
# 1. It *can't* be treated as a limited byte stream, and you _do_ need to
# respect CONTENT_LENGTH, in which case that ought to be documented,
# and there probably ought to be a feature request for it to be
# treated as a limited byte stream.
# 2. It *can* be treated as a limited byte stream, in which case there's a
# minor bug in the test client, and potentially some redundant
2011-05-10 13:49:28 +04:00
# code in MultiPartParser.
#
# It's an issue because it affects if you can pass a request off to code that
# does something like:
#
# while stream.read(BUFFER_SIZE):
# [do stuff]
#
#try:
# content_length = int(request.META.get('CONTENT_LENGTH',0))
#except (ValueError, TypeError):
# content_length = 0
# self._stream = LimitedStream(request, content_length)
self._stream = request
else:
self._stream = StringIO(request.raw_post_data)
return self._stream
2011-05-10 15:59:13 +04:00
# TODO: Modify this so that it happens implictly, rather than being called explicitly
# ie accessing any of .DATA, .FILES, .content_type, .method will force
2011-05-10 15:59:13 +04:00
# form overloading.
def _perform_form_overloading(self):
"""
Check the request to see if it is using form POST '_method'/'_content'/'_content_type' overrides.
If it is then alter self.method, self.content_type, self.CONTENT to reflect that rather than simply
delegating them to the original request.
"""
2011-05-12 18:11:14 +04:00
# We only need to use form overloading on form POST requests
content_type = self.request.META.get('HTTP_CONTENT_TYPE', self.request.META.get('CONTENT_TYPE', ''))
if not self._USE_FORM_OVERLOADING or self.request.method != 'POST' or not not is_form_media_type(content_type):
return
2011-04-11 15:19:28 +04:00
# Temporarily switch to using the form parsers, then parse the content
parsers = self.parsers
2011-05-10 13:49:28 +04:00
self.parsers = (FormParser, MultiPartParser)
content = self.DATA
2011-04-11 15:19:28 +04:00
self.parsers = parsers
# Method overloading - change the method and remove the param from the content
if self._METHOD_PARAM in content:
2011-05-12 18:11:14 +04:00
self._method = content[self._METHOD_PARAM].upper()
del self._data[self._METHOD_PARAM]
2011-04-11 15:19:28 +04:00
# Content overloading - rewind the stream and modify the content type
if self._CONTENT_PARAM in content and self._CONTENTTYPE_PARAM in content:
self._content_type = content[self._CONTENTTYPE_PARAM]
self._stream = StringIO(content[self._CONTENT_PARAM])
del(self._data)
2011-04-11 16:52:16 +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:
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 14:24:14 +04:00
@property
2011-05-12 18:11:14 +04:00
def _parsed_media_types(self):
"""
2011-05-12 18:11:14 +04:00
Return a list of all the media types that this view can parse.
"""
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 18:11:14 +04:00
Return the view's default parser.
"""
2011-04-11 14:24:14 +04:00
return self.parsers[0]
2011-04-11 19:54:02 +04:00
2011-04-11 19:54:02 +04:00
########## ResponseMixin ##########
class ResponseMixin(object):
2011-05-10 13:49:28 +04:00
"""
Adds behavior for pluggable Renderers to a :class:`.BaseView` or Django :class:`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.
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-04-28 22:54:30 +04:00
renderers = ()
2011-05-10 15:51:49 +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
"""
Takes a ``Response`` object and returns an ``HttpResponse``.
"""
2011-04-11 19:54:02 +04:00
self.response = response
try:
2011-04-28 22:54:30 +04:00
renderer = self._determine_renderer(self.request)
2011-04-11 19:54:02 +04:00
except ErrorResponse, exc:
2011-05-12 18:11:14 +04:00
renderer = self._default_renderer
2011-04-11 19:54:02 +04:00
response = exc.response
# Serialize the response content
# TODO: renderer.media_type isn't the right thing to do here...
2011-04-11 19:54:02 +04:00
if response.has_content_body:
2011-05-10 15:21:48 +04:00
content = renderer(self).render(response.cleaned_content, renderer.media_type)
2011-04-11 19:54:02 +04:00
else:
2011-04-28 22:54:30 +04:00
content = renderer(self).render()
2011-04-11 19:54:02 +04:00
# Build the HTTP Response
# TODO: renderer.media_type isn't the right thing to do here...
2011-04-28 22:54:30 +04:00
resp = HttpResponse(content, mimetype=renderer.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
# TODO: This should be simpler now.
# Add a handles_response() to the renderer, then iterate through the
# acceptable media types, ordered by how specific they are,
# calling handles_response on each renderer.
2011-04-28 22:54:30 +04:00
def _determine_renderer(self, request):
2011-05-10 15:51:49 +04:00
"""
Return the appropriate renderer for the output, given the client's 'Accept' header,
2011-05-04 12:21:17 +04:00
and the content types that this mixin knows how to serve.
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'])):
accept_list = ['text/html', '*/*']
elif request.META.has_key('HTTP_ACCEPT'):
# Use standard HTTP Accept negotiation
accept_list = request.META["HTTP_ACCEPT"].split(',')
else:
# No accept header specified
2011-05-12 18:11:14 +04:00
return self._default_renderer
2011-04-11 19:54:02 +04:00
# Parse the accept header into a dict of {qvalue: set of media types}
# We ignore mietype parameters
accept_dict = {}
for token in accept_list:
components = token.split(';')
mimetype = components[0].strip()
qvalue = Decimal('1.0')
if len(components) > 1:
# Parse items that have a qvalue eg 'text/html; q=0.9'
2011-04-11 19:54:02 +04:00
try:
(q, num) = components[-1].split('=')
if q == 'q':
qvalue = Decimal(num)
except:
# Skip malformed entries
continue
if accept_dict.has_key(qvalue):
accept_dict[qvalue].add(mimetype)
else:
accept_dict[qvalue] = set((mimetype,))
# Convert to a list of sets ordered by qvalue (highest first)
accept_sets = [accept_dict[qvalue] for qvalue in sorted(accept_dict.keys(), reverse=True)]
for accept_set in accept_sets:
# Return any exact match
2011-04-28 22:54:30 +04:00
for renderer in self.renderers:
if renderer.media_type in accept_set:
return renderer
2011-04-11 19:54:02 +04:00
# Return any subtype match
2011-04-28 22:54:30 +04:00
for renderer in self.renderers:
if renderer.media_type.split('/')[0] + '/*' in accept_set:
return renderer
2011-04-11 19:54:02 +04:00
# Return default
if '*/*' in accept_set:
2011-05-12 18:11:14 +04:00
return self._default_renderer
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
@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-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-12 18:11:14 +04:00
Return the view's default renderer.
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 ##########
class AuthMixin(object):
2011-05-10 13:49:28 +04:00
"""
2011-05-12 18:11:14 +04:00
Simple mixin class to add authentication and permission checking to a ``View`` class.
2011-05-10 13:49:28 +04:00
"""
authentication = ()
permissions = ()
@property
2011-05-10 13:49:28 +04:00
def user(self):
if not hasattr(self, '_user'):
self._user = self._authenticate()
return self._user
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``.
"""
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-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
for permission_cls in self.permissions:
permission = permission_cls(self)
2011-05-10 13:49:28 +04:00
permission.check_permission(user)
########## Resource Mixin ##########
2011-05-12 18:11:14 +04:00
class ResourceMixin(object):
@property
def CONTENT(self):
if not hasattr(self, '_content'):
2011-05-12 18:11:14 +04:00
self._content = self._get_content()
return self._content
2011-05-12 18:11:14 +04:00
def _get_content(self):
resource = self.resource(self)
2011-05-12 18:11:14 +04:00
return resource.validate(self.DATA, self.FILES)
def get_bound_form(self, content=None):
resource = self.resource(self)
return resource.get_bound_form(content)
def object_to_data(self, obj):
resource = self.resource(self)
return resource.object_to_data(obj)
2011-05-02 22:49:12 +04:00
########## Model Mixins ##########
class ReadModelMixin(object):
2011-05-10 13:49:28 +04:00
"""
Behavior to read a model instance on GET requests
"""
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-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)
return instance
class CreateModelMixin(object):
2011-05-10 13:49:28 +04:00
"""
Behavior to create a model instance on POST requests
"""
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-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'):
headers['Location'] = instance.get_absolute_url()
return Response(status.HTTP_201_CREATED, instance, headers)
class UpdateModelMixin(object):
2011-05-10 13:49:28 +04:00
"""
Behavior to update a model instance on PUT requests
"""
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-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-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)
2011-05-02 22:49:12 +04:00
for (key, val) in self.CONTENT.items():
setattr(instance, key, val)
2011-05-04 12:21:17 +04:00
except model.DoesNotExist:
instance = model(**self.CONTENT)
2011-05-02 22:49:12 +04:00
instance.save()
instance.save()
return instance
class DeleteModelMixin(object):
2011-05-10 13:49:28 +04:00
"""
Behavior to delete a model instance on DELETE requests
"""
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-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
"""
Behavior to list a set of model instances on GET requests
"""
2011-05-02 22:49:12 +04:00
queryset = None
def get(self, request, *args, **kwargs):
2011-05-10 13:49:28 +04:00
queryset = self.queryset if self.queryset else self.resource.model.objects.all()
2011-05-02 22:49:12 +04:00
return queryset.filter(**kwargs)