2011-05-12 15:55:13 +04:00
|
|
|
"""
|
2011-12-09 17:37:53 +04:00
|
|
|
The :mod:`mixins` module provides a set of reusable `mixin`
|
2011-05-19 00:13:48 +04:00
|
|
|
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
|
2011-12-09 17:37:53 +04:00
|
|
|
from django.core.paginator import Paginator
|
2011-07-05 09:25:39 +04:00
|
|
|
from django.db.models.fields.related import ForeignKey
|
2012-01-05 18:07:31 +04:00
|
|
|
from urlobject import URLObject
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2011-05-10 13:49:28 +04:00
|
|
|
from djangorestframework import status
|
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
|
2012-02-07 15:15:30 +04:00
|
|
|
from djangorestframework.response import Response, ImmediateResponse
|
2012-02-07 17:38:54 +04:00
|
|
|
from djangorestframework.request import Request
|
2012-02-02 20:19:44 +04:00
|
|
|
from djangorestframework.utils import as_tuple, allowed_methods
|
2011-05-10 13:49:28 +04:00
|
|
|
|
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
|
|
|
"""
|
2012-02-07 18:22:14 +04:00
|
|
|
`Mixin` class enabling the use of :class:`request.Request` in your views.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-04-02 19:32:37 +04:00
|
|
|
|
2012-02-07 17:38:54 +04:00
|
|
|
parser_classes = ()
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
2012-02-07 17:38:54 +04:00
|
|
|
The set of parsers that the view can handle.
|
2011-12-09 17:37:53 +04:00
|
|
|
|
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
|
|
|
|
2012-02-07 17:38:54 +04:00
|
|
|
request_class = Request
|
|
|
|
"""
|
|
|
|
The class to use as a wrapper for the original request object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_parsers(self):
|
2011-05-12 18:11:14 +04:00
|
|
|
"""
|
2012-02-07 17:38:54 +04:00
|
|
|
Instantiates and returns the list of parsers that will be used by the request
|
|
|
|
to parse its content.
|
2011-05-12 18:11:14 +04:00
|
|
|
"""
|
2012-02-07 17:38:54 +04:00
|
|
|
if not hasattr(self, '_parsers'):
|
|
|
|
self._parsers = [r(self) for r in self.parser_classes]
|
|
|
|
return self._parsers
|
2011-05-12 18:11:14 +04:00
|
|
|
|
2012-02-07 17:38:54 +04:00
|
|
|
def prepare_request(self, request):
|
2011-05-12 18:11:14 +04:00
|
|
|
"""
|
2012-02-07 18:22:14 +04:00
|
|
|
Prepares the request cycle. Returns an instance of :class:`request.Request`,
|
|
|
|
wrapping the original request object.
|
2011-05-12 18:14:22 +04:00
|
|
|
"""
|
2012-02-07 17:38:54 +04:00
|
|
|
parsers = self.get_parsers()
|
|
|
|
request = self.request_class(request, parsers=parsers)
|
|
|
|
self.request = request
|
|
|
|
return request
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _parsed_media_types(self):
|
|
|
|
"""
|
2012-02-07 18:22:14 +04:00
|
|
|
Returns a list of all the media types that this view can parse.
|
2012-02-07 17:38:54 +04:00
|
|
|
"""
|
|
|
|
return [p.media_type for p in self.parser_classes]
|
2012-01-22 23:28:34 +04:00
|
|
|
|
2011-04-11 19:54:02 +04:00
|
|
|
|
|
|
|
########## ResponseMixin ##########
|
|
|
|
|
|
|
|
class ResponseMixin(object):
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2012-02-07 18:22:14 +04:00
|
|
|
`Mixin` class enabling the use of :class:`response.Response` in your views.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
2011-05-12 18:11:14 +04:00
|
|
|
|
2012-02-07 15:15:30 +04:00
|
|
|
renderer_classes = ()
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
The set of response renderers that the view can handle.
|
2011-12-09 17:37:53 +04:00
|
|
|
|
|
|
|
Should be a tuple/list of classes as described in the :mod:`renderers` module.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
|
2012-02-07 15:15:30 +04:00
|
|
|
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
|
2012-02-02 20:19:44 +04:00
|
|
|
|
|
|
|
def prepare_response(self, response):
|
|
|
|
"""
|
2012-02-07 18:22:14 +04:00
|
|
|
Prepares and returns `response`.
|
2012-02-07 17:38:54 +04:00
|
|
|
This has no effect if the response is not an instance of :class:`response.Response`.
|
2012-02-02 20:19:44 +04:00
|
|
|
"""
|
|
|
|
if hasattr(response, 'request') and response.request is None:
|
|
|
|
response.request = self.request
|
2012-02-07 15:15:30 +04:00
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
# 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
|
2012-02-07 15:15:30 +04:00
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
# set the views renderers on the response
|
2012-02-07 15:15:30 +04:00
|
|
|
response.renderers = self.get_renderers()
|
2011-04-11 19:54:02 +04:00
|
|
|
self.response = response
|
2012-02-02 20:19:44 +04:00
|
|
|
return response
|
2011-04-11 19:54:02 +04:00
|
|
|
|
2012-02-07 17:38:54 +04:00
|
|
|
@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
|
|
|
|
|
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
|
|
|
"""
|
2012-02-07 15:15:30 +04:00
|
|
|
return [renderer.media_type for renderer in self.get_renderers()]
|
2011-12-09 17:37:53 +04:00
|
|
|
|
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.
|
|
|
|
"""
|
2012-02-07 15:15:30 +04:00
|
|
|
return [renderer.format for renderer in self.get_renderers()]
|
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-12-09 17:37:53 +04:00
|
|
|
|
2012-01-19 07:59:30 +04:00
|
|
|
authentication = ()
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
The set of authentication types that this view can handle.
|
2011-12-09 17:37:53 +04:00
|
|
|
|
|
|
|
Should be a tuple/list of classes as described in the :mod:`authentication` module.
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
|
2012-01-19 07:59:30 +04:00
|
|
|
permissions = ()
|
2011-05-13 12:59:36 +04:00
|
|
|
"""
|
|
|
|
The set of permissions that will be enforced on this view.
|
2011-12-09 17:37:53 +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-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
|
2011-12-09 17:37:53 +04:00
|
|
|
: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-10 15:51:49 +04:00
|
|
|
# TODO: wrap this behavior around dispatch()
|
2011-05-10 13:49:28 +04:00
|
|
|
def _check_permissions(self):
|
|
|
|
"""
|
2012-02-07 15:15:30 +04:00
|
|
|
Check user permissions and either raise an ``ImmediateResponse`` or return.
|
2011-05-10 13:49:28 +04:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2012-02-07 15:15:30 +04:00
|
|
|
May raise an :class:`response.ImmediateResponse` 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'):
|
2012-01-22 23:28:34 +04:00
|
|
|
self._content = self.validate_request(self.request.DATA, self.request.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.
|
|
|
|
|
2012-02-07 15:15:30 +04:00
|
|
|
May raise an :class:`response.ImmediateResponse` with status code 400 (Bad Request).
|
2011-05-27 17:40:19 +04:00
|
|
|
"""
|
|
|
|
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)
|
2012-01-22 23:28:34 +04:00
|
|
|
elif getattr(self, '%s_form' % self.request.method.lower(), None):
|
2011-05-27 17:40:19 +04:00
|
|
|
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.
|
2012-02-07 15:15:30 +04:00
|
|
|
May raise an :class:`response.ImmediateResponse` 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):
|
2011-07-31 00:23:53 +04:00
|
|
|
if hasattr(self._resource, 'get_bound_form'):
|
|
|
|
return self._resource.get_bound_form(content, method=method)
|
|
|
|
else:
|
|
|
|
return None
|
2011-05-12 15:55:13 +04:00
|
|
|
|
2011-05-17 12:15:35 +04:00
|
|
|
##########
|
|
|
|
|
2012-01-21 22:24:10 +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 ##########
|
|
|
|
|
2012-01-11 00:42:50 +04:00
|
|
|
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`
|
|
|
|
|
2012-01-11 18:07:33 +04:00
|
|
|
If a *ModelMixin is going to create/update an instance get_instance_data
|
|
|
|
handles the instance data creation/preaparation.
|
2012-01-11 00:42:50 +04:00
|
|
|
"""
|
|
|
|
|
2012-01-21 22:24:10 +04:00
|
|
|
queryset = None
|
|
|
|
|
2012-02-02 00:48:32 +04:00
|
|
|
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.
|
2012-01-11 00:42:50 +04:00
|
|
|
"""
|
|
|
|
|
2012-02-02 00:48:32 +04:00
|
|
|
kwargs = dict(kwargs)
|
2012-01-11 18:07:33 +04:00
|
|
|
|
|
|
|
# If the URLconf includes a .(?P<format>\w+) pattern to match against
|
|
|
|
# a .json, .xml suffix, then drop the 'format' kwarg before
|
|
|
|
# constructing the query.
|
2012-02-02 00:48:32 +04:00
|
|
|
if BaseRenderer._FORMAT_QUERY_PARAM in kwargs:
|
|
|
|
del kwargs[BaseRenderer._FORMAT_QUERY_PARAM]
|
2012-01-11 18:07:33 +04:00
|
|
|
|
2012-02-02 00:48:32 +04:00
|
|
|
return kwargs
|
2012-01-11 00:42:50 +04:00
|
|
|
|
|
|
|
def get_instance_data(self, model, content, **kwargs):
|
2012-01-11 18:07:33 +04:00
|
|
|
"""
|
2012-02-02 00:48:32 +04:00
|
|
|
Returns the dict with the data for model instance creation/update.
|
2012-01-11 00:42:50 +04:00
|
|
|
|
|
|
|
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
|
2012-01-11 18:07:33 +04:00
|
|
|
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.
|
2012-01-11 00:42:50 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
tmp = dict(kwargs)
|
|
|
|
|
|
|
|
for field in model._meta.fields:
|
2012-01-11 18:07:33 +04:00
|
|
|
if isinstance(field, ForeignKey) and field.name in tmp:
|
2012-01-11 00:42:50 +04:00
|
|
|
# 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
|
|
|
|
|
2012-02-02 00:48:32 +04:00
|
|
|
def get_instance(self, **kwargs):
|
2012-01-20 22:05:44 +04:00
|
|
|
"""
|
2012-02-02 00:48:32 +04:00
|
|
|
Get a model instance for read/update/delete requests.
|
2012-01-20 22:05:44 +04:00
|
|
|
"""
|
2012-02-02 00:48:32 +04:00
|
|
|
return self.get_queryset().get(**kwargs)
|
2012-01-20 22:05:44 +04:00
|
|
|
|
2012-01-21 22:24:10 +04:00
|
|
|
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)
|
|
|
|
|
2012-01-11 00:42:50 +04:00
|
|
|
|
|
|
|
class ReadModelMixin(ModelMixin):
|
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
|
2012-02-02 00:48:32 +04:00
|
|
|
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
|
2011-06-15 17:09:01 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
try:
|
2012-02-02 00:48:32 +04:00
|
|
|
self.model_instance = self.get_instance(**query_kwargs)
|
2011-05-04 12:21:17 +04:00
|
|
|
except model.DoesNotExist:
|
2012-02-07 15:15:30 +04:00
|
|
|
raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND)
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2011-07-01 10:32:04 +04:00
|
|
|
return self.model_instance
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2012-01-11 18:07:33 +04:00
|
|
|
|
2012-01-11 00:42:50 +04:00
|
|
|
class CreateModelMixin(ModelMixin):
|
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-07-05 09:25:39 +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-07-05 09:25:39 +04:00
|
|
|
# Copy the dict to keep self.CONTENT intact
|
|
|
|
content = dict(self.CONTENT)
|
|
|
|
m2m_data = {}
|
|
|
|
|
|
|
|
for field in model._meta.many_to_many:
|
2012-01-11 18:07:33 +04:00
|
|
|
if field.name in content:
|
2011-07-08 21:04:22 +04:00
|
|
|
m2m_data[field.name] = (
|
2011-07-08 21:14:52 +04:00
|
|
|
field.m2m_reverse_field_name(), content[field.name]
|
2011-07-08 21:04:22 +04:00
|
|
|
)
|
2011-07-05 09:25:39 +04:00
|
|
|
del content[field.name]
|
|
|
|
|
2012-01-03 12:55:12 +04:00
|
|
|
instance = model(**self.get_instance_data(model, content, *args, **kwargs))
|
2011-05-02 22:49:12 +04:00
|
|
|
instance.save()
|
2011-07-05 09:25:39 +04:00
|
|
|
|
|
|
|
for fieldname in m2m_data:
|
2011-07-08 21:04:22 +04:00
|
|
|
manager = getattr(instance, fieldname)
|
2011-12-09 17:37:53 +04:00
|
|
|
|
2011-07-08 21:04:22 +04:00
|
|
|
if hasattr(manager, 'add'):
|
|
|
|
manager.add(*m2m_data[fieldname][1])
|
|
|
|
else:
|
|
|
|
data = {}
|
|
|
|
data[manager.source_field_name] = instance
|
2011-12-09 17:37:53 +04:00
|
|
|
|
2011-07-08 21:04:22 +04:00
|
|
|
for related_item in m2m_data[fieldname][1]:
|
|
|
|
data[m2m_data[fieldname][0]] = related_item
|
|
|
|
manager.through(**data).save()
|
2011-07-05 09:25:39 +04:00
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
response = Response(instance, status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
# Set headers
|
2011-05-02 22:49:12 +04:00
|
|
|
if hasattr(instance, 'get_absolute_url'):
|
2012-02-02 20:19:44 +04:00
|
|
|
response['Location'] = self.resource(self).url(instance)
|
|
|
|
return response
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
|
2012-01-11 00:42:50 +04:00
|
|
|
class UpdateModelMixin(ModelMixin):
|
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
|
2012-02-02 00:48:32 +04:00
|
|
|
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
|
2011-12-09 17:37:53 +04:00
|
|
|
|
2012-01-21 22:24:10 +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
|
2011-05-02 22:49:12 +04:00
|
|
|
try:
|
2012-02-02 00:48:32 +04:00
|
|
|
self.model_instance = self.get_instance(*query_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:
|
2012-01-03 12:55:12 +04:00
|
|
|
self.model_instance = model(**self.get_instance_data(model, self.CONTENT, *args, **kwargs))
|
2011-07-01 10:32:04 +04:00
|
|
|
self.model_instance.save()
|
2012-02-02 20:19:44 +04:00
|
|
|
return Response(self.model_instance)
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
|
2012-01-11 00:42:50 +04:00
|
|
|
class DeleteModelMixin(ModelMixin):
|
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
|
2012-02-02 00:48:32 +04:00
|
|
|
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
|
2011-06-15 17:09:01 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
try:
|
2012-02-02 00:48:32 +04:00
|
|
|
instance = self.get_instance(**query_kwargs)
|
2011-05-04 12:21:17 +04:00
|
|
|
except model.DoesNotExist:
|
2012-02-07 15:15:30 +04:00
|
|
|
raise ImmediateResponse(status=status.HTTP_404_NOT_FOUND)
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
instance.delete()
|
2012-02-02 20:19:44 +04:00
|
|
|
return Response()
|
2011-05-02 22:49:12 +04:00
|
|
|
|
|
|
|
|
2012-01-11 00:42:50 +04:00
|
|
|
class ListModelMixin(ModelMixin):
|
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
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
def get(self, request, *args, **kwargs):
|
2012-01-19 21:56:09 +04:00
|
|
|
queryset = self.get_queryset()
|
2012-01-21 22:24:10 +04:00
|
|
|
ordering = self.get_ordering()
|
2012-02-02 00:48:32 +04:00
|
|
|
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
|
2011-06-14 21:22:13 +04:00
|
|
|
|
2012-02-02 00:48:32 +04:00
|
|
|
queryset = queryset.filter(**query_kwargs)
|
2011-05-16 17:11:36 +04:00
|
|
|
if ordering:
|
2012-01-21 22:24:10 +04:00
|
|
|
queryset = queryset.order_by(*ordering)
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2012-02-02 20:19:44 +04:00
|
|
|
return Response(queryset)
|
2012-01-19 21:56:09 +04:00
|
|
|
|
2011-05-02 22:49:12 +04:00
|
|
|
|
2011-12-09 17:37:53 +04:00
|
|
|
########## 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):
|
2012-01-21 22:36:25 +04:00
|
|
|
"""
|
|
|
|
Helper method to determine what the `limit` should be
|
|
|
|
"""
|
2011-12-09 17:37:53 +04:00
|
|
|
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):
|
2012-01-21 22:36:25 +04:00
|
|
|
"""
|
|
|
|
Constructs a url used for getting the next/previous urls
|
|
|
|
"""
|
2012-01-05 18:07:31 +04:00
|
|
|
url = URLObject.parse(self.request.get_full_path())
|
|
|
|
url = url.add_query_param('page', page_number)
|
2011-12-09 17:37:53 +04:00
|
|
|
|
|
|
|
limit = self.get_limit()
|
|
|
|
if limit != self.limit:
|
2012-01-05 18:07:31 +04:00
|
|
|
url = url.add_query_param('limit', limit)
|
2011-12-09 17:37:53 +04:00
|
|
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
def next(self, page):
|
2012-01-21 22:36:25 +04:00
|
|
|
"""
|
|
|
|
Returns a url to the next page of results (if any)
|
|
|
|
"""
|
2011-12-09 17:37:53 +04:00
|
|
|
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):
|
2012-01-21 22:36:25 +04:00
|
|
|
"""
|
|
|
|
This is some useful information that is added to the response
|
|
|
|
"""
|
2011-12-09 17:37:53 +04:00
|
|
|
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
|
2012-01-22 23:28:34 +04:00
|
|
|
if self.request.method.upper() != 'GET':
|
2011-12-09 17:37:53 +04:00
|
|
|
return self._resource.filter_response(obj)
|
|
|
|
|
|
|
|
paginator = Paginator(obj, self.get_limit())
|
|
|
|
|
|
|
|
try:
|
|
|
|
page_num = int(self.request.GET.get('page', '1'))
|
|
|
|
except ValueError:
|
2012-02-07 15:15:30 +04:00
|
|
|
raise ImmediateResponse(
|
2012-02-02 20:19:44 +04:00
|
|
|
content={'detail': 'That page contains no results'},
|
|
|
|
status=status.HTTP_404_NOT_FOUND)
|
2011-12-09 17:37:53 +04:00
|
|
|
|
|
|
|
if page_num not in paginator.page_range:
|
2012-02-07 15:15:30 +04:00
|
|
|
raise ImmediateResponse(
|
2012-02-02 20:19:44 +04:00
|
|
|
content={'detail': 'That page contains no results'},
|
|
|
|
status=status.HTTP_404_NOT_FOUND)
|
2011-12-09 17:37:53 +04:00
|
|
|
|
|
|
|
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
|