add search option
[redakcja.git] / src / documents / api / views.py
1 from rest_framework.generics import RetrieveAPIView, RetrieveUpdateAPIView, ListAPIView, ListCreateAPIView
2 from rest_framework.permissions import IsAuthenticatedOrReadOnly
3 from django.http import Http404
4 from .. import models
5 from . import serializers
6
7
8 class BookList(ListAPIView):
9     serializer_class = serializers.BookSerializer
10     search_fields = ['title']
11
12     def get_queryset(self):
13         return models.Book.get_visible_for(self.request.user)
14
15
16 class BookDetail(RetrieveAPIView):
17     serializer_class = serializers.BookDetailSerializer
18
19     def get_queryset(self):
20         return models.Book.get_visible_for(self.request.user)
21     
22
23 class ChunkList(ListAPIView):
24     queryset = models.Chunk.objects.all()
25     serializer_class = serializers.ChunkSerializer
26     filterset_fields = ['user', 'stage']
27     search_fields = [
28         'book__title',
29         '=book_slug',
30     ]
31
32     def get_queryset(self):
33         return models.Chunk.get_visible_for(self.request.user)
34
35
36 class ChunkDetail(RetrieveUpdateAPIView):
37     permission_classes = [IsAuthenticatedOrReadOnly]
38     serializer_class = serializers.ChunkDetailSerializer
39
40     def get_queryset(self):
41         return models.Chunk.get_visible_for(self.request.user)
42
43
44 class ChunkRevisionList(ListCreateAPIView):
45     permission_classes = [IsAuthenticatedOrReadOnly]
46     serializer_class = serializers.RevisionSerializer
47     
48     def get_serializer_class(self):
49         if self.request.method == 'POST':
50             return serializers.RevisionDetailSerializer
51         else:
52             return serializers.RevisionSerializer
53
54     def get_serializer_context(self):
55         ctx = super().get_serializer_context()
56         try:
57             ctx["chunk"] = models.Chunk.objects.get(pk=self.kwargs['pk'])
58         except models.Chunk.DoesNotExist:
59             raise Http404
60         return ctx
61
62     def get_queryset(self):
63         try:
64             return models.Chunk.get_visible_for(self.request.user).get(
65                 pk=self.kwargs['pk']
66             ).change_set.all()
67         except models.Chunk.DoesNotExist:
68             raise Http404()
69
70
71 class RevisionDetail(RetrieveAPIView):
72     queryset = models.Chunk.change_model.objects.all()
73     serializer_class = serializers.RevisionDetailSerializer
74
75     def get_queryset(self):
76         return models.Chunk.get_revisions_visible_for(self.request.user)
77