2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-30 03:30:52 +04:00
|
|
|
Generic views that provide commonly needed behaviour.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-18 16:38:29 +03:00
|
|
|
|
|
|
|
from django.db.models.query import QuerySet
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.http import Http404
|
2013-05-28 14:57:11 +04:00
|
|
|
from django.shortcuts import get_object_or_404 as _get_object_or_404
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2015-06-25 23:55:51 +03:00
|
|
|
from rest_framework import mixins, views
|
2013-04-29 15:45:00 +04:00
|
|
|
from rest_framework.settings import api_settings
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-04-09 21:45:15 +04:00
|
|
|
|
2013-10-04 18:49:56 +04:00
|
|
|
def get_object_or_404(queryset, *filter_args, **filter_kwargs):
|
2013-05-28 14:57:11 +04:00
|
|
|
"""
|
2014-09-05 19:29:46 +04:00
|
|
|
Same as Django's standard shortcut, but make sure to also raise 404
|
2013-05-28 14:57:11 +04:00
|
|
|
if the filter_kwargs don't match the required types.
|
|
|
|
"""
|
|
|
|
try:
|
2013-10-04 18:49:56 +04:00
|
|
|
return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
|
2013-05-28 14:57:11 +04:00
|
|
|
except (TypeError, ValueError):
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
|
2012-10-25 16:50:39 +04:00
|
|
|
class GenericAPIView(views.APIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Base class for all other generic views.
|
|
|
|
"""
|
2013-04-25 01:40:24 +04:00
|
|
|
# You'll need to either set these attributes,
|
2013-04-30 11:24:33 +04:00
|
|
|
# or override `get_queryset()`/`get_serializer_class()`.
|
2014-07-28 21:11:40 +04:00
|
|
|
# If you are overriding a view method, it is important that you call
|
|
|
|
# `get_queryset()` instead of accessing the `queryset` property directly,
|
|
|
|
# as `queryset` will get evaluated only once, and those results are cached
|
|
|
|
# for all subsequent requests.
|
2013-04-09 21:45:15 +04:00
|
|
|
queryset = None
|
2012-09-20 16:06:27 +04:00
|
|
|
serializer_class = None
|
2013-04-09 21:45:15 +04:00
|
|
|
|
2015-01-09 18:30:36 +03:00
|
|
|
# If you want to use object lookups other than pk, set 'lookup_field'.
|
2013-04-30 11:24:33 +04:00
|
|
|
# For more complex lookup requirements override `get_object()`.
|
2013-04-25 01:40:24 +04:00
|
|
|
lookup_field = 'pk'
|
2013-10-21 12:47:07 +04:00
|
|
|
lookup_url_kwarg = None
|
2013-04-23 14:59:13 +04:00
|
|
|
|
2013-05-07 16:00:44 +04:00
|
|
|
# The filter backend classes to use for queryset filtering
|
|
|
|
filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
|
2013-04-25 01:40:24 +04:00
|
|
|
|
2015-01-09 18:30:36 +03:00
|
|
|
# The style to use for queryset pagination.
|
|
|
|
pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
|
2013-04-09 22:37:19 +04:00
|
|
|
|
2013-04-09 21:45:15 +04:00
|
|
|
def get_queryset(self):
|
|
|
|
"""
|
2013-04-09 22:06:49 +04:00
|
|
|
Get the list of items for this view.
|
|
|
|
This must be an iterable, and may be a queryset.
|
2013-04-09 22:37:19 +04:00
|
|
|
Defaults to using `self.queryset`.
|
|
|
|
|
2014-07-28 21:11:40 +04:00
|
|
|
This method should always be used rather than accessing `self.queryset`
|
|
|
|
directly, as `self.queryset` gets evaluated only once, and those results
|
|
|
|
are cached for all subsequent requests.
|
|
|
|
|
2013-04-09 22:37:19 +04:00
|
|
|
You may want to override this if you need to provide different
|
|
|
|
querysets depending on the incoming request.
|
|
|
|
|
|
|
|
(Eg. return a list of items that is specific to the user)
|
2013-04-09 21:45:15 +04:00
|
|
|
"""
|
2014-08-29 15:35:53 +04:00
|
|
|
assert self.queryset is not None, (
|
|
|
|
"'%s' should either include a `queryset` attribute, "
|
|
|
|
"or override the `get_queryset()` method."
|
|
|
|
% self.__class__.__name__
|
|
|
|
)
|
|
|
|
|
2014-09-11 23:50:26 +04:00
|
|
|
queryset = self.queryset
|
2014-09-12 13:59:51 +04:00
|
|
|
if isinstance(queryset, QuerySet):
|
2014-09-11 23:50:26 +04:00
|
|
|
# Ensure queryset is re-evaluated on each request.
|
|
|
|
queryset = queryset.all()
|
|
|
|
return queryset
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2014-08-29 14:29:26 +04:00
|
|
|
def get_object(self):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2013-04-09 21:45:15 +04:00
|
|
|
Returns the object the view is displaying.
|
2013-04-09 22:37:19 +04:00
|
|
|
|
|
|
|
You may want to override this if you need to provide non-standard
|
|
|
|
queryset lookups. Eg if objects are referenced using multiple
|
|
|
|
keyword arguments in the url conf.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2014-08-29 14:29:26 +04:00
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
2013-04-09 22:01:01 +04:00
|
|
|
|
|
|
|
# Perform the lookup filtering.
|
2013-10-21 12:47:07 +04:00
|
|
|
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
|
2013-04-09 22:01:01 +04:00
|
|
|
|
2014-08-29 15:48:04 +04:00
|
|
|
assert lookup_url_kwarg in self.kwargs, (
|
|
|
|
'Expected view %s to be called with a URL keyword argument '
|
|
|
|
'named "%s". Fix your URL conf, or set the `.lookup_field` '
|
|
|
|
'attribute on the view correctly.' %
|
|
|
|
(self.__class__.__name__, lookup_url_kwarg)
|
|
|
|
)
|
2013-04-09 22:01:01 +04:00
|
|
|
|
2014-08-29 15:48:04 +04:00
|
|
|
filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
|
2013-04-11 18:48:18 +04:00
|
|
|
obj = get_object_or_404(queryset, **filter_kwargs)
|
2013-04-09 21:45:15 +04:00
|
|
|
|
2013-04-09 22:01:01 +04:00
|
|
|
# May raise a permission denied
|
2013-02-11 17:02:20 +04:00
|
|
|
self.check_object_permissions(self.request, obj)
|
2013-04-09 22:01:01 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
return obj
|
|
|
|
|
2015-01-09 18:30:36 +03:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Return the serializer instance that should be used for validating and
|
|
|
|
deserializing input, and for serializing output.
|
|
|
|
"""
|
|
|
|
serializer_class = self.get_serializer_class()
|
|
|
|
kwargs['context'] = self.get_serializer_context()
|
|
|
|
return serializer_class(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
"""
|
|
|
|
Return the class to use for the serializer.
|
|
|
|
Defaults to using `self.serializer_class`.
|
|
|
|
|
|
|
|
You may want to override this if you need to provide different
|
|
|
|
serializations depending on the incoming request.
|
|
|
|
|
|
|
|
(Eg. admins get full serialization, others get basic serialization)
|
|
|
|
"""
|
|
|
|
assert self.serializer_class is not None, (
|
|
|
|
"'%s' should either include a `serializer_class` attribute, "
|
|
|
|
"or override the `get_serializer_class()` method."
|
|
|
|
% self.__class__.__name__
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.serializer_class
|
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
"""
|
|
|
|
Extra context provided to the serializer class.
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
'request': self.request,
|
|
|
|
'format': self.format_kwarg,
|
|
|
|
'view': self
|
|
|
|
}
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
"""
|
|
|
|
Given a queryset, filter it with whichever filter backend is in use.
|
|
|
|
|
|
|
|
You are unlikely to want to override this method, although you may need
|
|
|
|
to call it either from a list view, or from a custom `get_object`
|
|
|
|
method if you want to apply the configured filtering backend to the
|
|
|
|
default queryset.
|
|
|
|
"""
|
|
|
|
for backend in list(self.filter_backends):
|
|
|
|
queryset = backend().filter_queryset(self.request, queryset, self)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
@property
|
2015-01-15 19:52:07 +03:00
|
|
|
def paginator(self):
|
2015-01-16 19:55:46 +03:00
|
|
|
"""
|
|
|
|
The paginator instance associated with the view, or `None`.
|
|
|
|
"""
|
2015-01-15 19:52:07 +03:00
|
|
|
if not hasattr(self, '_paginator'):
|
2015-01-09 18:30:36 +03:00
|
|
|
if self.pagination_class is None:
|
2015-01-15 19:52:07 +03:00
|
|
|
self._paginator = None
|
2015-01-09 18:30:36 +03:00
|
|
|
else:
|
2015-01-15 19:52:07 +03:00
|
|
|
self._paginator = self.pagination_class()
|
|
|
|
return self._paginator
|
2015-01-09 18:30:36 +03:00
|
|
|
|
|
|
|
def paginate_queryset(self, queryset):
|
2015-01-16 19:55:46 +03:00
|
|
|
"""
|
|
|
|
Return a single page of results, or `None` if pagination is disabled.
|
|
|
|
"""
|
2015-01-15 19:52:07 +03:00
|
|
|
if self.paginator is None:
|
2015-01-16 19:55:46 +03:00
|
|
|
return None
|
2015-01-15 19:52:07 +03:00
|
|
|
return self.paginator.paginate_queryset(queryset, self.request, view=self)
|
2015-01-09 18:30:36 +03:00
|
|
|
|
2015-01-13 20:14:13 +03:00
|
|
|
def get_paginated_response(self, data):
|
2015-01-16 19:55:46 +03:00
|
|
|
"""
|
|
|
|
Return a paginated style `Response` object for the given output data.
|
|
|
|
"""
|
|
|
|
assert self.paginator is not None
|
2015-01-15 19:52:07 +03:00
|
|
|
return self.paginator.get_paginated_response(data)
|
2015-01-09 18:30:36 +03:00
|
|
|
|
2012-10-24 21:22:29 +04:00
|
|
|
|
2014-08-19 16:54:52 +04:00
|
|
|
# Concrete view classes that provide method handlers
|
|
|
|
# by composing the mixin classes with the base view.
|
2012-10-24 21:22:29 +04:00
|
|
|
|
|
|
|
class CreateAPIView(mixins.CreateModelMixin,
|
2012-10-25 16:50:39 +04:00
|
|
|
GenericAPIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-24 21:22:29 +04:00
|
|
|
Concrete view for creating a model instance.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-24 21:22:29 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return self.create(request, *args, **kwargs)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-10-24 21:22:29 +04:00
|
|
|
class ListAPIView(mixins.ListModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-24 21:22:29 +04:00
|
|
|
Concrete view for listing a queryset.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2012-10-03 12:26:15 +04:00
|
|
|
class RetrieveAPIView(mixins.RetrieveModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Concrete view for retrieving a model instance.
|
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
|
2012-10-08 17:13:15 +04:00
|
|
|
|
2012-10-24 21:22:29 +04:00
|
|
|
class DestroyAPIView(mixins.DestroyModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-10-08 17:13:15 +04:00
|
|
|
"""
|
2012-10-24 21:22:29 +04:00
|
|
|
Concrete view for deleting a model instance.
|
2012-10-08 17:13:15 +04:00
|
|
|
"""
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
return self.destroy(request, *args, **kwargs)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-10-24 21:22:29 +04:00
|
|
|
class UpdateAPIView(mixins.UpdateModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-24 21:22:29 +04:00
|
|
|
Concrete view for updating a model instance.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
def put(self, request, *args, **kwargs):
|
2013-01-02 17:39:24 +04:00
|
|
|
return self.update(request, *args, **kwargs)
|
2012-12-16 23:49:18 +04:00
|
|
|
|
|
|
|
def patch(self, request, *args, **kwargs):
|
2013-04-05 01:24:30 +04:00
|
|
|
return self.partial_update(request, *args, **kwargs)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-24 21:22:29 +04:00
|
|
|
|
2012-10-25 15:26:08 +04:00
|
|
|
class ListCreateAPIView(mixins.ListModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-10-24 21:22:29 +04:00
|
|
|
"""
|
|
|
|
Concrete view for listing a queryset or creating a model instance.
|
|
|
|
"""
|
2012-10-25 15:26:08 +04:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return self.create(request, *args, **kwargs)
|
2012-10-24 21:22:29 +04:00
|
|
|
|
|
|
|
|
2012-12-13 19:57:17 +04:00
|
|
|
class RetrieveUpdateAPIView(mixins.RetrieveModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-12-13 19:57:17 +04:00
|
|
|
"""
|
|
|
|
Concrete view for retrieving, updating a model instance.
|
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
return self.update(request, *args, **kwargs)
|
|
|
|
|
2013-01-02 21:43:43 +04:00
|
|
|
def patch(self, request, *args, **kwargs):
|
2013-04-05 01:24:30 +04:00
|
|
|
return self.partial_update(request, *args, **kwargs)
|
2013-01-02 21:43:43 +04:00
|
|
|
|
2012-12-13 23:41:40 +04:00
|
|
|
|
2012-10-25 15:26:08 +04:00
|
|
|
class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-10-24 21:22:29 +04:00
|
|
|
"""
|
|
|
|
Concrete view for retrieving or deleting a model instance.
|
|
|
|
"""
|
2012-10-25 15:26:08 +04:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
return self.destroy(request, *args, **kwargs)
|
2012-10-24 21:22:29 +04:00
|
|
|
|
|
|
|
|
2012-10-25 15:26:08 +04:00
|
|
|
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
2013-04-09 21:45:15 +04:00
|
|
|
GenericAPIView):
|
2012-10-24 21:22:29 +04:00
|
|
|
"""
|
|
|
|
Concrete view for retrieving, updating or deleting a model instance.
|
|
|
|
"""
|
2012-10-25 15:26:08 +04:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.retrieve(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
2013-01-02 17:39:24 +04:00
|
|
|
return self.update(request, *args, **kwargs)
|
2012-12-16 23:49:18 +04:00
|
|
|
|
|
|
|
def patch(self, request, *args, **kwargs):
|
2013-04-05 01:24:30 +04:00
|
|
|
return self.partial_update(request, *args, **kwargs)
|
2013-01-02 21:43:43 +04:00
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
return self.destroy(request, *args, **kwargs)
|