1 from rest_framework.generics import RetrieveAPIView, RetrieveUpdateAPIView, ListAPIView, ListCreateAPIView
2 from rest_framework.permissions import IsAuthenticatedOrReadOnly
3 from django.http import Http404
5 from . import serializers
8 class BookList(ListAPIView):
9 serializer_class = serializers.BookSerializer
10 search_fields = ['title']
12 def get_queryset(self):
13 return models.Book.get_visible_for(self.request.user)
16 class BookDetail(RetrieveAPIView):
17 serializer_class = serializers.BookDetailSerializer
19 def get_queryset(self):
20 return models.Book.get_visible_for(self.request.user)
23 class ChunkList(ListAPIView):
24 queryset = models.Chunk.objects.all()
25 serializer_class = serializers.ChunkSerializer
26 filter_fields = ['user', 'stage']
27 search_fields = ['book__title']
29 def get_queryset(self):
30 return models.Chunk.get_visible_for(self.request.user)
33 class ChunkDetail(RetrieveUpdateAPIView):
34 permission_classes = [IsAuthenticatedOrReadOnly]
35 serializer_class = serializers.ChunkDetailSerializer
37 def get_queryset(self):
38 return models.Chunk.get_visible_for(self.request.user)
41 class ChunkRevisionList(ListCreateAPIView):
42 permission_classes = [IsAuthenticatedOrReadOnly]
43 serializer_class = serializers.RevisionSerializer
45 def get_serializer_class(self):
46 if self.request.method == 'POST':
47 return serializers.RevisionDetailSerializer
49 return serializers.RevisionSerializer
51 def get_serializer_context(self):
52 ctx = super().get_serializer_context()
54 ctx["chunk"] = models.Chunk.objects.get(pk=self.kwargs['pk'])
55 except models.Chunk.DoesNotExist:
59 def get_queryset(self):
61 return models.Chunk.get_visible_for(self.request.user).get(
64 except models.Chunk.DoesNotExist:
68 class RevisionDetail(RetrieveAPIView):
69 queryset = models.Chunk.change_model.objects.all()
70 serializer_class = serializers.RevisionDetailSerializer
72 def get_queryset(self):
73 return models.Chunk.get_revisions_visible_for(self.request.user)