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
|
2012-09-30 20:31:28 +04:00
|
|
|
from rest_framework import views, mixins
|
|
|
|
from rest_framework.settings import api_settings
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
from django.views.generic.list import MultipleObjectMixin
|
2012-11-08 01:07:24 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
### Base classes for the generic views ###
|
|
|
|
|
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
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
model = None
|
2012-09-20 16:06:27 +04:00
|
|
|
serializer_class = None
|
2012-10-18 02:09:11 +04:00
|
|
|
model_serializer_class = api_settings.DEFAULT_MODEL_SERIALIZER_CLASS
|
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
|
|
|
}
|
|
|
|
|
2012-10-01 18:49:19 +04:00
|
|
|
def get_serializer_class(self):
|
|
|
|
"""
|
|
|
|
Return the class to use for the serializer.
|
2012-11-17 02:45:57 +04:00
|
|
|
|
|
|
|
Defaults to using `self.serializer_class`, falls back to constructing a
|
|
|
|
model serializer class using `self.model_serializer_class`, with
|
|
|
|
`self.model` as the model.
|
2012-10-01 18:49:19 +04:00
|
|
|
"""
|
2012-09-28 17:28:50 +04:00
|
|
|
serializer_class = self.serializer_class
|
|
|
|
|
|
|
|
if serializer_class is None:
|
2012-09-30 20:31:28 +04:00
|
|
|
class DefaultSerializer(self.model_serializer_class):
|
2012-09-28 17:28:50 +04:00
|
|
|
class Meta:
|
|
|
|
model = self.model
|
|
|
|
serializer_class = DefaultSerializer
|
|
|
|
|
2012-10-01 18:49:19 +04:00
|
|
|
return serializer_class
|
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-02-07 01:28:03 +04:00
|
|
|
def pre_save(self, obj):
|
|
|
|
"""
|
|
|
|
Placeholder method for calling before saving an object.
|
|
|
|
May be used eg. 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
|
|
|
|
2012-10-25 16:50:39 +04:00
|
|
|
class MultipleObjectAPIView(MultipleObjectMixin, GenericAPIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Base class for generic views onto a queryset.
|
|
|
|
"""
|
2012-10-01 18:49:19 +04:00
|
|
|
|
|
|
|
paginate_by = api_settings.PAGINATE_BY
|
2012-11-17 02:45:57 +04:00
|
|
|
paginate_by_param = api_settings.PAGINATE_BY_PARAM
|
2012-11-17 03:22:15 +04:00
|
|
|
pagination_serializer_class = api_settings.DEFAULT_PAGINATION_SERIALIZER_CLASS
|
2012-11-08 01:07:24 +04:00
|
|
|
filter_backend = api_settings.FILTER_BACKEND
|
2012-10-09 00:00:55 +04:00
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
2012-11-17 02:45:57 +04:00
|
|
|
"""
|
|
|
|
Given a queryset, filter it with whichever filter backend is in use.
|
|
|
|
"""
|
2012-11-08 01:07:24 +04:00
|
|
|
if not self.filter_backend:
|
|
|
|
return queryset
|
|
|
|
backend = self.filter_backend()
|
|
|
|
return backend.filter_queryset(self.request, queryset, self)
|
2012-10-09 00:00:55 +04:00
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
def get_pagination_serializer(self, page=None):
|
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
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
def get_paginate_by(self, queryset):
|
|
|
|
"""
|
|
|
|
Return the size of pages to use with pagination.
|
|
|
|
"""
|
|
|
|
if self.paginate_by_param:
|
2012-11-17 03:22:15 +04:00
|
|
|
query_params = self.request.QUERY_PARAMS
|
2012-11-17 02:45:57 +04:00
|
|
|
try:
|
2012-11-17 03:22:15 +04:00
|
|
|
return int(query_params[self.paginate_by_param])
|
2012-11-17 02:45:57 +04:00
|
|
|
except (KeyError, ValueError):
|
|
|
|
pass
|
|
|
|
return self.paginate_by
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-25 16:50:39 +04:00
|
|
|
class SingleObjectAPIView(SingleObjectMixin, GenericAPIView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Base class for generic views onto a model instance.
|
|
|
|
"""
|
2012-11-17 03:22:15 +04:00
|
|
|
|
2012-10-05 20:02:33 +04:00
|
|
|
pk_url_kwarg = 'pk' # Not provided in Django 1.3
|
|
|
|
slug_url_kwarg = 'slug' # Not provided in Django 1.3
|
2012-11-17 03:22:15 +04:00
|
|
|
slug_field = 'slug'
|
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
|
|
|
"""
|
|
|
|
Override default to add support for object-level permissions.
|
|
|
|
"""
|
2012-11-14 23:34:19 +04:00
|
|
|
obj = super(SingleObjectAPIView, self).get_object(queryset)
|
2013-02-11 17:02:20 +04:00
|
|
|
self.check_object_permissions(self.request, obj)
|
2012-09-20 16:06:27 +04:00
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
### Concrete view classes that provide method handlers ###
|
|
|
|
### by composing the mixin classes with a base view. ###
|
|
|
|
|
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,
|
2012-10-25 16:50:39 +04:00
|
|
|
MultipleObjectAPIView):
|
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,
|
2012-10-25 16:50:39 +04:00
|
|
|
SingleObjectAPIView):
|
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,
|
2012-10-27 13:32:49 +04:00
|
|
|
SingleObjectAPIView):
|
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,
|
2012-10-25 16:50:39 +04:00
|
|
|
SingleObjectAPIView):
|
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-01-02 17:39:24 +04:00
|
|
|
kwargs['partial'] = True
|
|
|
|
return self.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,
|
2012-10-25 16:50:39 +04:00
|
|
|
MultipleObjectAPIView):
|
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,
|
|
|
|
SingleObjectAPIView):
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
kwargs['partial'] = True
|
|
|
|
return self.update(request, *args, **kwargs)
|
|
|
|
|
2012-12-13 23:41:40 +04:00
|
|
|
|
2012-10-25 15:26:08 +04:00
|
|
|
class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
2012-10-25 16:50:39 +04:00
|
|
|
SingleObjectAPIView):
|
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,
|
2012-10-25 16:50:39 +04:00
|
|
|
SingleObjectAPIView):
|
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-01-02 17:39:24 +04:00
|
|
|
kwargs['partial'] = True
|
|
|
|
return self.update(request, *args, **kwargs)
|
2013-01-02 21:43:43 +04:00
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
return self.destroy(request, *args, **kwargs)
|