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.
 
   4 from django.db.models import Prefetch
 
   5 from django.contrib.auth.models import User
 
   6 from django.views.generic import DetailView, TemplateView
 
   8 import documents.models
 
   9 from rest_framework.generics import ListAPIView
 
  10 from rest_framework.filters import SearchFilter
 
  11 from rest_framework import serializers
 
  14 class CatalogueView(TemplateView):
 
  15     template_name = "catalogue/catalogue.html"
 
  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')
 
  24 class AuthorView(TemplateView):
 
  26     template_name = "catalogue/author_detail.html"
 
  28     def get_context_data(self, slug):
 
  29         ctx = super().get_context_data()
 
  30         authors = models.Author.objects.filter(slug=slug).prefetch_related(
 
  32             Prefetch("translated_book_set"),
 
  34         ctx["author"] = authors.first()
 
  38 class BookView(DetailView):
 
  42 class TermSearchFilter(SearchFilter):
 
  46 class Terms(ListAPIView):
 
  47     filter_backends = [TermSearchFilter]
 
  48     search_fields = ['name']
 
  50     class serializer_class(serializers.Serializer):
 
  51         label = serializers.CharField(source='name')
 
  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()
 
  61 class AuthorTerms(Terms):
 
  62     search_fields = ['first_name', 'last_name']
 
  63     queryset = models.Author.objects.all()
 
  65 class EditorTerms(Terms):
 
  66     search_fields = ['first_name', 'last_name', 'username']
 
  67     queryset = User.objects.all()
 
  69     class serializer_class(serializers.Serializer):
 
  70         label = serializers.SerializerMethodField()
 
  72         def get_label(self, obj):
 
  73             return f'{obj.last_name}, {obj.first_name}'
 
  75 class BookTitleTerms(Terms):
 
  76     queryset = models.Book.objects.all()
 
  77     search_fields = ['title', 'slug']
 
  79     class serializer_class(serializers.Serializer):
 
  80         label = serializers.CharField(source='title')
 
  82 class WLURITerms(Terms):
 
  83     queryset = models.Book.objects.all()
 
  84     search_fields = ['title', 'slug']
 
  86     class serializer_class(serializers.Serializer):
 
  87         label = serializers.CharField(source='wluri')