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
|
2013-04-29 15:45:00 +04:00
|
|
|
|
2013-05-25 02:44:23 +04:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
|
2013-04-09 21:45:15 +04:00
|
|
|
from django.core.paginator import Paginator, InvalidPage
|
|
|
|
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
|
2013-04-09 21:45:15 +04:00
|
|
|
from django.utils.translation import ugettext as _
|
2013-05-25 02:44:23 +04:00
|
|
|
from rest_framework import views, mixins, exceptions
|
|
|
|
from rest_framework.request import clone_request
|
2013-04-29 15:45:00 +04:00
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
import warnings
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-04-09 21:45:15 +04:00
|
|
|
|
2013-08-28 15:52:38 +04:00
|
|
|
def strict_positive_int(integer_string, cutoff=None):
|
2013-08-22 00:22:12 +04:00
|
|
|
"""
|
|
|
|
Cast a string to a strictly positive integer.
|
|
|
|
"""
|
|
|
|
ret = int(integer_string)
|
|
|
|
if ret <= 0:
|
|
|
|
raise ValueError()
|
2013-08-28 15:52:38 +04:00
|
|
|
if cutoff:
|
|
|
|
ret = min(ret, cutoff)
|
2013-08-22 00:22:12 +04:00
|
|
|
return ret
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
Same as Django's standard shortcut, but make sure to raise 404
|
|
|
|
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.
|
|
|
|
"""
|
2012-11-17 03:22:15 +04:00
|
|
|
|
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()`.
|
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
|
|
|
|
2013-04-30 11:24:33 +04:00
|
|
|
# This shortcut may be used instead of setting either or both
|
|
|
|
# of the `queryset`/`serializer_class` attributes, although using
|
|
|
|
# the explicit style is generally preferred.
|
|
|
|
model = None
|
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
# If you want to use object lookups other than pk, set this attribute.
|
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-04-25 01:40:24 +04:00
|
|
|
# Pagination settings
|
2013-04-09 21:45:15 +04:00
|
|
|
paginate_by = api_settings.PAGINATE_BY
|
|
|
|
paginate_by_param = api_settings.PAGINATE_BY_PARAM
|
2013-08-26 20:05:36 +04:00
|
|
|
max_paginate_by = api_settings.MAX_PAGINATE_BY
|
2013-04-09 21:45:15 +04:00
|
|
|
pagination_serializer_class = api_settings.DEFAULT_PAGINATION_SERIALIZER_CLASS
|
|
|
|
page_kwarg = 'page'
|
2013-04-25 01:40:24 +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
|
|
|
|
2013-04-29 17:08:38 +04:00
|
|
|
# The following attributes may be subject to change,
|
|
|
|
# and should be considered private API.
|
2013-04-25 01:40:24 +04:00
|
|
|
model_serializer_class = api_settings.DEFAULT_MODEL_SERIALIZER_CLASS
|
2013-04-29 17:08:38 +04:00
|
|
|
paginator_class = Paginator
|
2013-04-25 20:40:17 +04:00
|
|
|
|
2013-04-09 22:47:16 +04:00
|
|
|
######################################
|
2013-04-23 14:59:13 +04:00
|
|
|
# These are pending deprecation...
|
2013-04-09 21:45:15 +04:00
|
|
|
|
2013-04-09 22:47:16 +04:00
|
|
|
pk_url_kwarg = 'pk'
|
|
|
|
slug_url_kwarg = 'slug'
|
2013-04-09 21:45:15 +04:00
|
|
|
slug_field = 'slug'
|
2013-05-07 15:25:41 +04:00
|
|
|
allow_empty = True
|
2013-05-07 16:00:44 +04:00
|
|
|
filter_backend = api_settings.FILTER_BACKEND
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-09-30 20:31:28 +04:00
|
|
|
def get_serializer_context(self):
|
2012-10-01 18:49:19 +04:00
|
|
|
"""
|
|
|
|
Extra context provided to the serializer class.
|
|
|
|
"""
|
2012-09-30 20:31:28 +04:00
|
|
|
return {
|
|
|
|
'request': self.request,
|
2012-10-05 17:22:02 +04:00
|
|
|
'format': self.format_kwarg,
|
2012-10-01 18:49:19 +04:00
|
|
|
'view': self
|
2012-09-30 20:31:28 +04:00
|
|
|
}
|
|
|
|
|
2013-01-02 17:39:24 +04:00
|
|
|
def get_serializer(self, instance=None, data=None,
|
2013-02-07 01:28:03 +04:00
|
|
|
files=None, many=False, partial=False):
|
2012-11-17 03:22:15 +04:00
|
|
|
"""
|
|
|
|
Return the serializer instance that should be used for validating and
|
|
|
|
deserializing input, and for serializing output.
|
|
|
|
"""
|
2012-10-01 18:49:19 +04:00
|
|
|
serializer_class = self.get_serializer_class()
|
2012-09-30 20:31:28 +04:00
|
|
|
context = self.get_serializer_context()
|
2013-01-02 17:39:24 +04:00
|
|
|
return serializer_class(instance, data=data, files=files,
|
2013-02-06 17:04:11 +04:00
|
|
|
many=many, partial=partial, context=context)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
def get_pagination_serializer(self, page):
|
2012-10-01 18:49:19 +04:00
|
|
|
"""
|
2012-11-17 02:45:57 +04:00
|
|
|
Return a serializer instance to use with paginated data.
|
2012-10-01 18:49:19 +04:00
|
|
|
"""
|
|
|
|
class SerializerClass(self.pagination_serializer_class):
|
|
|
|
class Meta:
|
|
|
|
object_serializer_class = self.get_serializer_class()
|
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
pagination_serializer_class = SerializerClass
|
2012-10-01 18:49:19 +04:00
|
|
|
context = self.get_serializer_context()
|
|
|
|
return pagination_serializer_class(instance=page, context=context)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-04-25 20:40:17 +04:00
|
|
|
def paginate_queryset(self, queryset, page_size=None):
|
2013-04-09 21:45:15 +04:00
|
|
|
"""
|
2013-04-25 20:40:17 +04:00
|
|
|
Paginate a queryset if required, either returning a page object,
|
|
|
|
or `None` if pagination is not configured for this view.
|
2013-04-09 21:45:15 +04:00
|
|
|
"""
|
2013-04-25 20:40:17 +04:00
|
|
|
deprecated_style = False
|
|
|
|
if page_size is not None:
|
2013-04-29 15:45:00 +04:00
|
|
|
warnings.warn('The `page_size` parameter to `paginate_queryset()` '
|
|
|
|
'is due to be deprecated. '
|
|
|
|
'Note that the return style of this method is also '
|
|
|
|
'changed, and will simply return a page object '
|
|
|
|
'when called without a `page_size` argument.',
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-04-25 20:40:17 +04:00
|
|
|
deprecated_style = True
|
|
|
|
else:
|
|
|
|
# Determine the required page size.
|
|
|
|
# If pagination is not configured, simply return None.
|
|
|
|
page_size = self.get_paginate_by()
|
|
|
|
if not page_size:
|
|
|
|
return None
|
|
|
|
|
2013-05-07 15:25:41 +04:00
|
|
|
if not self.allow_empty:
|
|
|
|
warnings.warn(
|
|
|
|
'The `allow_empty` parameter is due to be deprecated. '
|
|
|
|
'To use `allow_empty=False` style behavior, You should override '
|
|
|
|
'`get_queryset()` and explicitly raise a 404 on empty querysets.',
|
|
|
|
PendingDeprecationWarning, stacklevel=2
|
|
|
|
)
|
|
|
|
|
2013-04-29 17:08:38 +04:00
|
|
|
paginator = self.paginator_class(queryset, page_size,
|
|
|
|
allow_empty_first_page=self.allow_empty)
|
2013-04-25 01:40:24 +04:00
|
|
|
page_kwarg = self.kwargs.get(self.page_kwarg)
|
|
|
|
page_query_param = self.request.QUERY_PARAMS.get(self.page_kwarg)
|
|
|
|
page = page_kwarg or page_query_param or 1
|
2013-04-09 21:45:15 +04:00
|
|
|
try:
|
2013-10-20 07:43:23 +04:00
|
|
|
page_number = paginator.validate_number(page)
|
|
|
|
except InvalidPage:
|
2013-04-09 21:45:15 +04:00
|
|
|
if page == 'last':
|
|
|
|
page_number = paginator.num_pages
|
|
|
|
else:
|
|
|
|
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
|
|
|
|
try:
|
|
|
|
page = paginator.page(page_number)
|
|
|
|
except InvalidPage as e:
|
|
|
|
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
|
|
|
|
'page_number': page_number,
|
|
|
|
'message': str(e)
|
|
|
|
})
|
|
|
|
|
2013-04-25 20:40:17 +04:00
|
|
|
if deprecated_style:
|
|
|
|
return (paginator, page, page.object_list, page.has_other_pages())
|
|
|
|
return page
|
|
|
|
|
2013-04-09 22:37:19 +04:00
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
"""
|
|
|
|
Given a queryset, filter it with whichever filter backend is in use.
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-04-23 14:59:13 +04:00
|
|
|
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.
|
2013-04-09 22:37:19 +04:00
|
|
|
"""
|
2013-10-24 17:39:02 +04:00
|
|
|
for backend in self.get_filter_backends():
|
|
|
|
queryset = backend().filter_queryset(self.request, queryset, self)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def get_filter_backends(self):
|
|
|
|
"""
|
|
|
|
Returns the list of filter backends that this view requires.
|
|
|
|
"""
|
2013-05-07 16:00:44 +04:00
|
|
|
filter_backends = self.filter_backends or []
|
|
|
|
if not filter_backends and self.filter_backend:
|
|
|
|
warnings.warn(
|
|
|
|
'The `filter_backend` attribute and `FILTER_BACKEND` setting '
|
|
|
|
'are due to be deprecated in favor of a `filter_backends` '
|
|
|
|
'attribute and `DEFAULT_FILTER_BACKENDS` setting, that take '
|
|
|
|
'a *list* of filter backend classes.',
|
|
|
|
PendingDeprecationWarning, stacklevel=2
|
|
|
|
)
|
|
|
|
filter_backends = [self.filter_backend]
|
2013-10-24 17:39:02 +04:00
|
|
|
return filter_backends
|
2013-05-07 16:00:44 +04:00
|
|
|
|
2012-11-17 03:22:15 +04:00
|
|
|
|
2013-04-09 22:47:16 +04:00
|
|
|
########################
|
2013-04-09 22:37:19 +04:00
|
|
|
### The following methods provide default implementations
|
|
|
|
### that you may want to override for more complex cases.
|
|
|
|
|
2013-04-25 01:40:24 +04:00
|
|
|
def get_paginate_by(self, queryset=None):
|
|
|
|
"""
|
|
|
|
Return the size of pages to use with pagination.
|
|
|
|
|
|
|
|
If `PAGINATE_BY_PARAM` is set it will attempt to get the page size
|
|
|
|
from a named query parameter in the url, eg. ?page_size=100
|
|
|
|
|
|
|
|
Otherwise defaults to using `self.paginate_by`.
|
|
|
|
"""
|
|
|
|
if queryset is not None:
|
2013-04-29 15:45:00 +04:00
|
|
|
warnings.warn('The `queryset` parameter to `get_paginate_by()` '
|
|
|
|
'is due to be deprecated.',
|
|
|
|
PendingDeprecationWarning, stacklevel=2)
|
2013-04-25 01:40:24 +04:00
|
|
|
|
|
|
|
if self.paginate_by_param:
|
|
|
|
try:
|
2013-08-28 15:52:38 +04:00
|
|
|
return strict_positive_int(
|
|
|
|
self.request.QUERY_PARAMS[self.paginate_by_param],
|
|
|
|
cutoff=self.max_paginate_by
|
|
|
|
)
|
2013-04-25 01:40:24 +04:00
|
|
|
except (KeyError, ValueError):
|
|
|
|
pass
|
|
|
|
|
2013-08-28 15:52:38 +04:00
|
|
|
return self.paginate_by
|
2013-04-25 01:40:24 +04:00
|
|
|
|
2013-04-09 22:37:19 +04:00
|
|
|
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.
|
|
|
|
|
2013-06-12 22:00:33 +04:00
|
|
|
(Eg. admins get full serialization, others get basic serialization)
|
2013-04-09 22:37:19 +04:00
|
|
|
"""
|
|
|
|
serializer_class = self.serializer_class
|
|
|
|
if serializer_class is not None:
|
|
|
|
return serializer_class
|
|
|
|
|
2013-04-30 11:24:33 +04:00
|
|
|
assert self.model is not None, \
|
2013-04-29 17:08:38 +04:00
|
|
|
"'%s' should either include a 'serializer_class' attribute, " \
|
2013-04-30 11:24:33 +04:00
|
|
|
"or use the 'model' attribute as a shortcut for " \
|
2013-04-29 17:08:38 +04:00
|
|
|
"automatically generating a serializer class." \
|
|
|
|
% self.__class__.__name__
|
|
|
|
|
2013-04-09 22:37:19 +04:00
|
|
|
class DefaultSerializer(self.model_serializer_class):
|
|
|
|
class Meta:
|
2013-04-30 11:24:33 +04:00
|
|
|
model = self.model
|
2013-04-09 22:37:19 +04:00
|
|
|
return DefaultSerializer
|
|
|
|
|
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`.
|
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
if self.queryset is not None:
|
2013-04-09 22:06:49 +04:00
|
|
|
return self.queryset._clone()
|
|
|
|
|
|
|
|
if self.model is not None:
|
|
|
|
return self.model._default_manager.all()
|
|
|
|
|
2013-04-23 14:59:13 +04:00
|
|
|
raise ImproperlyConfigured("'%s' must define 'queryset' or 'model'"
|
2013-04-09 22:06:49 +04:00
|
|
|
% self.__class__.__name__)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-11-14 23:34:19 +04:00
|
|
|
def get_object(self, queryset=None):
|
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
|
|
|
"""
|
2013-04-09 22:01:01 +04:00
|
|
|
# Determine the base queryset to use.
|
2013-04-09 21:45:15 +04:00
|
|
|
if queryset is None:
|
2013-04-09 22:37:19 +04:00
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
2013-04-11 18:48:18 +04:00
|
|
|
else:
|
|
|
|
pass # Deprecation warning
|
2013-04-09 22:01:01 +04:00
|
|
|
|
|
|
|
# Perform the lookup filtering.
|
2013-10-21 12:47:07 +04:00
|
|
|
# Note that `pk` and `slug` are deprecated styles of lookup filtering.
|
|
|
|
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
|
|
|
|
lookup = self.kwargs.get(lookup_url_kwarg, None)
|
2013-04-09 21:45:15 +04:00
|
|
|
pk = self.kwargs.get(self.pk_url_kwarg, None)
|
|
|
|
slug = self.kwargs.get(self.slug_url_kwarg, None)
|
2013-04-09 22:01:01 +04:00
|
|
|
|
|
|
|
if lookup is not None:
|
2013-04-23 14:59:13 +04:00
|
|
|
filter_kwargs = {self.lookup_field: lookup}
|
2013-04-29 15:45:00 +04:00
|
|
|
elif pk is not None and self.lookup_field == 'pk':
|
|
|
|
warnings.warn(
|
|
|
|
'The `pk_url_kwarg` attribute is due to be deprecated. '
|
|
|
|
'Use the `lookup_field` attribute instead',
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
2013-04-11 18:48:18 +04:00
|
|
|
filter_kwargs = {'pk': pk}
|
2013-04-29 15:45:00 +04:00
|
|
|
elif slug is not None and self.lookup_field == 'pk':
|
|
|
|
warnings.warn(
|
|
|
|
'The `slug_url_kwarg` attribute is due to be deprecated. '
|
|
|
|
'Use the `lookup_field` attribute instead',
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
2013-04-11 18:48:18 +04:00
|
|
|
filter_kwargs = {self.slug_field: slug}
|
2013-04-09 21:45:15 +04:00
|
|
|
else:
|
2013-06-05 16:45:28 +04:00
|
|
|
raise ImproperlyConfigured(
|
2013-04-29 15:45:00 +04:00
|
|
|
'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__, self.lookup_field)
|
|
|
|
)
|
2013-04-09 22:01:01 +04:00
|
|
|
|
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
|
|
|
|
|
2013-04-09 22:47:16 +04:00
|
|
|
########################
|
|
|
|
### The following are placeholder methods,
|
|
|
|
### and are intended to be overridden.
|
2013-04-25 01:40:24 +04:00
|
|
|
###
|
|
|
|
### The are not called by GenericAPIView directly,
|
|
|
|
### but are used by the mixin methods.
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-04-09 22:37:19 +04:00
|
|
|
def pre_save(self, obj):
|
|
|
|
"""
|
|
|
|
Placeholder method for calling before saving an object.
|
|
|
|
|
|
|
|
May be used to set attributes on the object that are implicit
|
|
|
|
in either the request, or the url.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def post_save(self, obj, created=False):
|
|
|
|
"""
|
|
|
|
Placeholder method for calling after saving an object.
|
|
|
|
"""
|
|
|
|
pass
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-12-03 04:07:41 +04:00
|
|
|
def pre_delete(self, obj):
|
|
|
|
"""
|
|
|
|
Placeholder method for calling before deleting an object.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def post_delete(self, obj):
|
|
|
|
"""
|
|
|
|
Placeholder method for calling after saving an object.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-05-25 02:44:23 +04:00
|
|
|
def metadata(self, request):
|
|
|
|
"""
|
|
|
|
Return a dictionary of metadata about the view.
|
|
|
|
Used to return responses for OPTIONS requests.
|
|
|
|
|
|
|
|
We override the default behavior, and add some extra information
|
|
|
|
about the required request body for POST and PUT operations.
|
|
|
|
"""
|
|
|
|
ret = super(GenericAPIView, self).metadata(request)
|
|
|
|
|
|
|
|
actions = {}
|
|
|
|
for method in ('PUT', 'POST'):
|
|
|
|
if method not in self.allowed_methods:
|
|
|
|
continue
|
|
|
|
|
|
|
|
cloned_request = clone_request(request, method)
|
|
|
|
try:
|
|
|
|
# Test global permissions
|
|
|
|
self.check_permissions(cloned_request)
|
|
|
|
# Test object permissions
|
|
|
|
if method == 'PUT':
|
2013-08-31 05:03:44 +04:00
|
|
|
try:
|
|
|
|
self.get_object()
|
|
|
|
except Http404:
|
|
|
|
# Http404 should be acceptable and the serializer
|
2013-09-03 23:30:18 +04:00
|
|
|
# metadata should be populated. Except this so the
|
|
|
|
# outer "else" clause of the try-except-else block
|
|
|
|
# will be executed.
|
2013-08-31 05:03:44 +04:00
|
|
|
pass
|
|
|
|
except (exceptions.APIException, PermissionDenied):
|
2013-05-25 02:44:23 +04:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# If user has appropriate permissions for the view, include
|
|
|
|
# appropriate metadata about the fields that should be supplied.
|
|
|
|
serializer = self.get_serializer()
|
|
|
|
actions[method] = serializer.metadata()
|
|
|
|
|
|
|
|
if actions:
|
|
|
|
ret['actions'] = actions
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2012-10-24 21:22:29 +04:00
|
|
|
|
2013-04-09 22:47:16 +04:00
|
|
|
##########################################################
|
2012-09-20 16:06:27 +04:00
|
|
|
### Concrete view classes that provide method handlers ###
|
2013-04-09 21:45:15 +04:00
|
|
|
### by composing the mixin classes with the base view. ###
|
2013-04-09 22:47:16 +04:00
|
|
|
##########################################################
|
2012-10-24 21:22:29 +04:00
|
|
|
|
|
|
|
class CreateAPIView(mixins.CreateModelMixin,
|
2012-10-25 16:50:39 +04:00
|
|
|
GenericAPIView):
|
2012-10-24 21:22:29 +04:00
|
|
|
|
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-24 21:22:29 +04:00
|
|
|
|
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-10-24 21:22:29 +04:00
|
|
|
|
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)
|
2013-04-09 21:45:15 +04:00
|
|
|
|
|
|
|
|
2013-04-09 22:47:16 +04:00
|
|
|
##########################
|
2013-04-09 21:45:15 +04:00
|
|
|
### Deprecated classes ###
|
2013-04-09 22:47:16 +04:00
|
|
|
##########################
|
2013-04-09 21:45:15 +04:00
|
|
|
|
|
|
|
class MultipleObjectAPIView(GenericAPIView):
|
2013-04-29 15:45:00 +04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn(
|
|
|
|
'Subclassing `MultipleObjectAPIView` is due to be deprecated. '
|
|
|
|
'You should simply subclass `GenericAPIView` instead.',
|
|
|
|
PendingDeprecationWarning, stacklevel=2
|
|
|
|
)
|
|
|
|
super(MultipleObjectAPIView, self).__init__(*args, **kwargs)
|
2013-04-09 21:45:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
class SingleObjectAPIView(GenericAPIView):
|
2013-04-29 15:45:00 +04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn(
|
|
|
|
'Subclassing `SingleObjectAPIView` is due to be deprecated. '
|
|
|
|
'You should simply subclass `GenericAPIView` instead.',
|
|
|
|
PendingDeprecationWarning, stacklevel=2
|
|
|
|
)
|
|
|
|
super(SingleObjectAPIView, self).__init__(*args, **kwargs)
|