1 from django.http import HttpResponse
2 from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
3 from paypal.permissions import IsSubscribed
4 from api.handlers import read_tags
5 from . import serializers
6 from catalogue.models import Book, Collection, Tag, Fragment
9 class CollectionList(ListAPIView):
10 queryset = Collection.objects.all()
11 serializer_class = serializers.CollectionListSerializer
14 class CollectionDetail(RetrieveAPIView):
15 queryset = Collection.objects.all()
17 serializer_class = serializers.CollectionSerializer
20 class BookDetail(RetrieveAPIView):
21 queryset = Book.objects.all()
23 serializer_class = serializers.BookDetailSerializer
26 class EpubView(RetrieveAPIView):
27 queryset = Book.objects.all()
29 permission_classes = [IsSubscribed]
31 def get(self, *args, **kwargs):
32 return HttpResponse(self.get_object().get_media('epub'))
35 class TagCategoryView(ListAPIView):
36 serializer_class = serializers.TagSerializer
38 def get_queryset(self):
39 category = self.kwargs['category']
40 tags = Tag.objects.filter(category=category).exclude(items=None).order_by('slug')
41 if self.request.query_params.get('book_only') == 'true':
42 tags = tags.filter(for_books=True)
43 if self.request.GET.get('picture_only') == 'true':
44 tags = filter(for_pictures=True)
46 after = self.request.query_params.get('after')
47 count = self.request.query_params.get('count')
49 tags = tags.filter(slug__gt=after)
56 class TagView(RetrieveAPIView):
57 serializer_class = serializers.TagDetailSerializer
60 return get_object_or_404(
62 category=self.kwargs['category'],
63 slug=self.kwargs['slug']
67 class FragmentList(ListAPIView):
68 serializer_class = serializers.FragmentSerializer
70 def get_queryset(self):
72 tags, ancestors = read_tags(
75 allowed={'author', 'epoch', 'kind', 'genre', 'book', 'theme'}
79 return Fragment.tagged.with_all(tags).select_related('book')
82 class FragmentView(RetrieveAPIView):
83 serializer_class = serializers.FragmentDetailSerializer
86 return get_object_or_404(
88 book__slug=self.kwargs['book'],
89 anchor=self.kwargs['anchor']