django-rest-framework/rest_framework/mixins.py

128 lines
4.2 KiB
Python
Raw Normal View History

"""
Basic building blocks for generic class based views.
We don't bind behaviour to http method handlers yet,
which allows mixin classes to be composed in interesting ways.
"""
2012-11-22 03:20:49 +04:00
from __future__ import unicode_literals
2012-09-30 20:31:28 +04:00
from django.http import Http404
from rest_framework import status
from rest_framework.response import Response
class CreateModelMixin(object):
"""
Create a model instance.
Should be mixed in with any `BaseView`.
"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.DATA, files=request.FILES)
if serializer.is_valid():
2012-10-25 15:15:31 +04:00
self.pre_save(serializer.object)
self.object = serializer.save()
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
2012-11-14 16:24:20 +04:00
def get_success_headers(self, data):
try:
return {'Location': data['url']}
except (TypeError, KeyError):
return {}
2012-10-25 15:15:31 +04:00
def pre_save(self, obj):
pass
class ListModelMixin(object):
"""
List a queryset.
Should be mixed in with `MultipleObjectAPIView`.
"""
2012-11-22 03:20:49 +04:00
empty_error = "Empty list and '%(class_name)s.allow_empty' is False."
2012-09-30 20:31:28 +04:00
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
self.object_list = self.filter_queryset(queryset)
2012-09-30 20:31:28 +04:00
# Default is to allow empty querysets. This can be altered by setting
# `.allow_empty = False`, to raise 404 errors on empty querysets.
allow_empty = self.get_allow_empty()
if not allow_empty and not self.object_list:
class_name = self.__class__.__name__
error_msg = self.empty_error % {'class_name': class_name}
raise Http404(error_msg)
2012-09-30 20:31:28 +04:00
# Pagination size is set by the `.paginate_by` attribute,
# which may be `None` to disable pagination.
page_size = self.get_paginate_by(self.object_list)
if page_size:
2012-10-25 15:15:31 +04:00
packed = self.paginate_queryset(self.object_list, page_size)
paginator, page, queryset, is_paginated = packed
2012-09-30 20:31:28 +04:00
serializer = self.get_pagination_serializer(page)
else:
2012-11-06 21:04:48 +04:00
serializer = self.get_serializer(self.object_list)
2012-09-30 20:31:28 +04:00
return Response(serializer.data)
class RetrieveModelMixin(object):
"""
Retrieve a model instance.
Should be mixed in with `SingleObjectBaseView`.
"""
def retrieve(self, request, *args, **kwargs):
self.object = self.get_object()
2012-11-06 21:04:48 +04:00
serializer = self.get_serializer(self.object)
return Response(serializer.data)
class UpdateModelMixin(object):
"""
Update a model instance.
Should be mixed in with `SingleObjectBaseView`.
"""
def update(self, request, *args, **kwargs):
2012-10-05 19:24:52 +04:00
try:
self.object = self.get_object()
created = False
2012-10-05 19:24:52 +04:00
except Http404:
self.object = None
created = True
2012-10-05 19:24:52 +04:00
serializer = self.get_serializer(self.object, data=request.DATA, files=request.FILES)
2012-10-08 15:52:56 +04:00
if serializer.is_valid():
2012-10-25 15:15:31 +04:00
self.pre_save(serializer.object)
self.object = serializer.save()
status_code = created and status.HTTP_201_CREATED or status.HTTP_200_OK
return Response(serializer.data, status=status_code)
2012-10-08 15:52:56 +04:00
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
2012-10-25 15:15:31 +04:00
def pre_save(self, obj):
2012-10-05 19:24:52 +04:00
"""
2012-10-25 15:15:31 +04:00
Set any attributes on the object that are implicit in the request.
2012-10-05 19:24:52 +04:00
"""
2012-10-25 15:15:31 +04:00
# pk and/or slug attributes are implicit in the URL.
2012-10-05 19:24:52 +04:00
pk = self.kwargs.get(self.pk_url_kwarg, None)
if pk:
setattr(obj, 'pk', pk)
slug = self.kwargs.get(self.slug_url_kwarg, None)
if slug:
slug_field = self.get_slug_field()
setattr(obj, slug_field, slug)
class DestroyModelMixin(object):
"""
Destroy a model instance.
Should be mixed in with `SingleObjectBaseView`.
"""
def destroy(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.delete()
return Response(status=status.HTTP_204_NO_CONTENT)