Publishing collections.
[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 = ['book__title']
28
29     def get_queryset(self):
30         return models.Chunk.get_visible_for(self.request.user)
31
32
33 class ChunkDetail(RetrieveUpdateAPIView):
34     permission_classes = [IsAuthenticatedOrReadOnly]
35     serializer_class = serializers.ChunkDetailSerializer
36
37     def get_queryset(self):
38         return models.Chunk.get_visible_for(self.request.user)
39
40
41 class ChunkRevisionList(ListCreateAPIView):
42     permission_classes = [IsAuthenticatedOrReadOnly]
43     serializer_class = serializers.RevisionSerializer
44     
45     def get_serializer_class(self):
46         if self.request.method == 'POST':
47             return serializers.RevisionDetailSerializer
48         else:
49             return serializers.RevisionSerializer
50
51     def get_serializer_context(self):
52         ctx = super().get_serializer_context()
53         try:
54             ctx["chunk"] = models.Chunk.objects.get(pk=self.kwargs['pk'])
55         except models.Chunk.DoesNotExist:
56             raise Http404
57         return ctx
58
59     def get_queryset(self):
60         try:
61             return models.Chunk.get_visible_for(self.request.user).get(
62                 pk=self.kwargs['pk']
63             ).change_set.all()
64         except models.Chunk.DoesNotExist:
65             raise Http404()
66
67
68 class RevisionDetail(RetrieveAPIView):
69     queryset = models.Chunk.change_model.objects.all()
70     serializer_class = serializers.RevisionDetailSerializer
71
72     def get_queryset(self):
73         return models.Chunk.get_revisions_visible_for(self.request.user)
74