Fix after upgrading django-filter.
[redakcja.git] / src / catalogue / views.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.db.models import Prefetch
5 from django.contrib.auth.models import User
6 from django.views.generic import DetailView, TemplateView
7 from . import models
8 import documents.models
9 from rest_framework.generics import ListAPIView
10 from rest_framework.filters import SearchFilter
11 from rest_framework import serializers
12
13
14 class CatalogueView(TemplateView):
15     template_name = "catalogue/catalogue.html"
16
17     def get_context_data(self):
18         ctx = super().get_context_data()
19         ctx["authors"] = models.Author.objects.all().prefetch_related('book_set__book_set', 'translated_book_set__book_set')
20
21         return ctx
22
23
24 class AuthorView(TemplateView):
25     model = models.Author
26     template_name = "catalogue/author_detail.html"
27
28     def get_context_data(self, slug):
29         ctx = super().get_context_data()
30         authors = models.Author.objects.filter(slug=slug).prefetch_related(
31             Prefetch("book_set"),
32             Prefetch("translated_book_set"),
33         )
34         ctx["author"] = authors.first()
35         return ctx
36
37
38 class BookView(DetailView):
39     model = models.Book
40
41
42 class TermSearchFilter(SearchFilter):
43     search_param = 'term'
44
45
46 class Terms(ListAPIView):
47     filter_backends = [TermSearchFilter]
48     search_fields = ['name']
49
50     class serializer_class(serializers.Serializer):
51         label = serializers.CharField(source='name')
52
53
54 class EpochTerms(Terms):
55     queryset = models.Epoch.objects.all()
56 class KindTerms(Terms):
57     queryset = models.Kind.objects.all()
58 class GenreTerms(Terms):
59     queryset = models.Genre.objects.all()
60
61 class AuthorTerms(Terms):
62     search_fields = ['first_name', 'last_name']
63     queryset = models.Author.objects.all()
64
65 class EditorTerms(Terms):
66     search_fields = ['first_name', 'last_name', 'username']
67     queryset = User.objects.all()
68
69     class serializer_class(serializers.Serializer):
70         label = serializers.SerializerMethodField()
71
72         def get_label(self, obj):
73             return f'{obj.last_name}, {obj.first_name}'
74     
75 class BookTitleTerms(Terms):
76     queryset = models.Book.objects.all()
77     search_fields = ['title', 'slug']
78
79     class serializer_class(serializers.Serializer):
80         label = serializers.CharField(source='title')
81
82 class WLURITerms(Terms):
83     queryset = models.Book.objects.all()
84     search_fields = ['title', 'slug']
85     
86     class serializer_class(serializers.Serializer):
87         label = serializers.CharField(source='wluri')
88