Link catalogue to documents.
[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.views.generic import DetailView, TemplateView
6 from . import models
7 import documents.models
8
9
10 class CatalogueView(TemplateView):
11     template_name = "catalogue/catalogue.html"
12
13     def get_context_data(self):
14         ctx = super().get_context_data()
15         documents_books_queryset = models.Book.objects.prefetch_unrelated(
16             "document_books", "slug", documents.models.Book, "dc_slug"
17         )
18         ctx["authors"] = models.Author.objects.all().prefetch_related(
19             Prefetch("book_set", queryset=documents_books_queryset),
20             Prefetch("translated_book_set", queryset=documents_books_queryset),
21         )
22         return ctx
23
24
25 class AuthorView(TemplateView):
26     model = models.Author
27     template_name = "catalogue/author_detail.html"
28
29     def get_context_data(self, slug):
30         ctx = super().get_context_data()
31         documents_books_queryset = models.Book.objects.prefetch_unrelated(
32             "document_books", "slug", documents.models.Book, "dc_slug"
33         )
34         authors = models.Author.objects.filter(slug=slug).prefetch_related(
35             Prefetch("book_set", queryset=documents_books_queryset),
36             Prefetch("translated_book_set", queryset=documents_books_queryset),
37         )
38         ctx["author"] = authors.first()
39         return ctx
40
41
42 class BookView(DetailView):
43     model = models.Book