2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Generic views that provide commmonly needed behaviour.
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
### Base classes for the generic views ###
|
|
|
|
|
|
|
|
class BaseView(views.APIView):
|
|
|
|
"""
|
|
|
|
Base class for all other generic views.
|
|
|
|
"""
|
|
|
|
serializer_class = None
|
2012-09-30 20:31:28 +04:00
|
|
|
model_serializer_class = api_settings.MODEL_SERIALIZER
|
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.
|
|
|
|
Use `self.serializer_class`, falling back to constructing a
|
|
|
|
model serializer class from `self.model_serializer_class`
|
|
|
|
"""
|
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
|
|
|
|
2012-10-01 18:49:19 +04:00
|
|
|
def get_serializer(self, data=None, files=None, instance=None):
|
|
|
|
# TODO: add support for files
|
|
|
|
# TODO: add support for seperate serializer/deserializer
|
|
|
|
serializer_class = self.get_serializer_class()
|
2012-09-30 20:31:28 +04:00
|
|
|
context = self.get_serializer_context()
|
2012-10-01 18:49:19 +04:00
|
|
|
return serializer_class(data, instance=instance, context=context)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class MultipleObjectBaseView(MultipleObjectMixin, BaseView):
|
|
|
|
"""
|
|
|
|
Base class for generic views onto a queryset.
|
|
|
|
"""
|
2012-10-01 18:49:19 +04:00
|
|
|
|
|
|
|
pagination_serializer_class = api_settings.PAGINATION_SERIALIZER
|
|
|
|
paginate_by = api_settings.PAGINATE_BY
|
|
|
|
|
|
|
|
def get_pagination_serializer_class(self):
|
|
|
|
"""
|
|
|
|
Return the class to use for the pagination serializer.
|
|
|
|
"""
|
|
|
|
class SerializerClass(self.pagination_serializer_class):
|
|
|
|
class Meta:
|
|
|
|
object_serializer_class = self.get_serializer_class()
|
|
|
|
|
|
|
|
return SerializerClass
|
|
|
|
|
|
|
|
def get_pagination_serializer(self, page=None):
|
|
|
|
pagination_serializer_class = self.get_pagination_serializer_class()
|
|
|
|
context = self.get_serializer_context()
|
|
|
|
return pagination_serializer_class(instance=page, context=context)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class SingleObjectBaseView(SingleObjectMixin, BaseView):
|
|
|
|
"""
|
|
|
|
Base class for generic views onto a model instance.
|
|
|
|
"""
|
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-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
"""
|
|
|
|
Override default to add support for object-level permissions.
|
|
|
|
"""
|
|
|
|
obj = super(SingleObjectBaseView, self).get_object()
|
2012-09-28 00:51:46 +04:00
|
|
|
if not self.has_permission(self.request, obj):
|
|
|
|
self.permission_denied(self.request)
|
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. ###
|
|
|
|
|
|
|
|
class ListAPIView(mixins.ListModelMixin,
|
|
|
|
MultipleObjectBaseView):
|
|
|
|
"""
|
|
|
|
Concrete view for listing a queryset.
|
|
|
|
"""
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2012-10-03 12:26:15 +04:00
|
|
|
class ListCreateAPIView(mixins.ListModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
MultipleObjectBaseView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Concrete view for listing a queryset or creating a model instance.
|
|
|
|
"""
|
|
|
|
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-03 12:26:15 +04:00
|
|
|
class RetrieveAPIView(mixins.RetrieveModelMixin,
|
|
|
|
SingleObjectBaseView):
|
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
|
|
|
|
|
|
|
class RetrieveDestroyAPIView(mixins.RetrieveModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
SingleObjectBaseView):
|
|
|
|
"""
|
|
|
|
Concrete view for retrieving or deleting a model instance.
|
|
|
|
"""
|
|
|
|
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-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-10-03 12:26:15 +04:00
|
|
|
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
SingleObjectBaseView):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Concrete view for retrieving, updating or deleting 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)
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
return self.destroy(request, *args, **kwargs)
|