| 1 |
from rest_framework import viewsets |
| 2 |
from rest_framework import generics |
| 3 |
from rest_framework import status |
| 4 |
from rest_framework.response import Response |
| 5 |
from rest_framework_extensions.etag.mixins import APIETAGMixin |
| 6 |
from rest_framework_extensions.etag.decorators import api_etag |
| 7 |
from rest_framework_extensions.utils import default_api_object_etag_func |
| 8 |
from .models import Book |
| 9 |
from .serializers import BookSerializer |
| 10 |
|
| 11 |
|
| 12 |
class BookViewSet(APIETAGMixin, |
| 13 |
viewsets.ModelViewSet): |
| 14 |
"""Test the mixin with DRF viewset.""" |
| 15 |
|
| 16 |
queryset = Book.objects.all() |
| 17 |
serializer_class = BookSerializer |
| 18 |
|
| 19 |
|
| 20 |
class BookChangeView(APIETAGMixin, |
| 21 |
generics.RetrieveUpdateDestroyAPIView): |
| 22 |
"""Test the mixin with DRF generic API views.""" |
| 23 |
|
| 24 |
queryset = Book.objects.all() |
| 25 |
serializer_class = BookSerializer |
| 26 |
|
| 27 |
|
| 28 |
class BookListCreateView(APIETAGMixin, |
| 29 |
generics.ListCreateAPIView): |
| 30 |
"""Test the mixin with DRF generic API views.""" |
| 31 |
|
| 32 |
queryset = Book.objects.all() |
| 33 |
serializer_class = BookSerializer |
| 34 |
|
| 35 |
|
| 36 |
class BookCustomDestroyView(generics.DestroyAPIView): |
| 37 |
"""Test the decorator with DRF generic API views.""" |
| 38 |
|
| 39 |
# include the queryset here to enable the object lookup in `@api_etag` |
| 40 |
queryset = Book.objects.all() |
| 41 |
|
| 42 |
@api_etag(etag_func=default_api_object_etag_func) |
| 43 |
def delete(self, request, *args, **kwargs): |
| 44 |
obj = Book.objects.get(id=kwargs['pk']) |
| 45 |
obj.delete() |
| 46 |
return Response(status=status.HTTP_204_NO_CONTENT) |
| 47 |
|
| 48 |
|
| 49 |
class BookUnconditionalDestroyView(generics.DestroyAPIView): |
| 50 |
"""Test the decorator with DRF generic API views.""" |
| 51 |
|
| 52 |
# include the queryset here to enable the object lookup in `@api_etag` |
| 53 |
queryset = Book.objects.all() |
| 54 |
|
| 55 |
@api_etag(etag_func=default_api_object_etag_func, precondition_map={}) |
| 56 |
def delete(self, request, *args, **kwargs): |
| 57 |
obj = Book.objects.get(id=kwargs['pk']) |
| 58 |
obj.delete() |
| 59 |
return Response(status=status.HTTP_204_NO_CONTENT) |
| 60 |
|
| 61 |
|
| 62 |
class BookUnconditionalUpdateView(generics.UpdateAPIView): |
| 63 |
"""Test the decorator with DRF generic API views.""" |
| 64 |
|
| 65 |
# include the queryset here to enable the object lookup in `@api_etag` |
| 66 |
queryset = Book.objects.all() |
| 67 |
serializer_class = BookSerializer |
| 68 |
|
| 69 |
@api_etag(etag_func=default_api_object_etag_func, precondition_map={}) |
| 70 |
def update(self, request, *args, **kwargs): |
| 71 |
return super().update(request, *args, **kwargs) |