Link catalogue to documents.
[redakcja.git] / src / catalogue / admin.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.contrib import admin
5 from . import models
6 from .wikidata import WikidataAdminMixin
7
8
9 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
10     list_display = [
11         "first_name",
12         "last_name",
13         "status",
14         "year_of_death",
15         "priority",
16         "wikidata_link",
17         "slug",
18     ]
19     list_filter = ["year_of_death", "priority", "collections", "status"]
20     search_fields = ["first_name", "last_name", "wikidata"]
21     prepopulated_fields = {"slug": ("first_name", "last_name")}
22     autocomplete_fields = ["collections"]
23
24
25 admin.site.register(models.Author, AuthorAdmin)
26
27
28 class BookAdmin(WikidataAdminMixin, admin.ModelAdmin):
29     list_display = [
30         "title",
31         "authors_str",
32         "translators_str",
33         "language",
34         "pd_year",
35         "priority",
36         "wikidata_link",
37     ]
38     search_fields = ["title", "wikidata"]
39     autocomplete_fields = ["authors", "translators", "based_on", "collections"]
40     prepopulated_fields = {"slug": ("title",)}
41     list_filter = ["language", "pd_year", "collections"]
42     readonly_fields = ["wikidata_link"]
43     fieldsets = [
44         (None, {"fields": [("wikidata", "wikidata_link")]}),
45         (
46             "Identification",
47             {
48                 "fields": [
49                     "title",
50                     "slug",
51                     "authors",
52                     "translators",
53                     "language",
54                     "based_on",
55                     "pd_year",
56                 ]
57             },
58         ),
59         (
60             "Plan",
61             {
62                 "fields": [
63                     "scans_source",
64                     "text_source",
65                     "priority",
66                     "collections",
67                     "notes",
68                 ]
69             },
70         ),
71     ]
72
73     def get_queryset(self, request):
74         qs = super().get_queryset(request)
75         if request.resolver_match.view_name.endswith("changelist"):
76             qs = qs.prefetch_related("authors", "translators")
77         return qs
78
79
80 admin.site.register(models.Book, BookAdmin)
81
82
83 class AuthorInline(admin.TabularInline):
84     model = models.Author.collections.through
85     autocomplete_fields = ["author"]
86
87
88 class BookInline(admin.TabularInline):
89     model = models.Book.collections.through
90     autocomplete_fields = ["book"]
91
92
93 class CollectionAdmin(admin.ModelAdmin):
94     list_display = ["name"]
95     autocomplete_fields = []
96     prepopulated_fields = {"slug": ("name",)}
97     search_fields = ["name"]
98     inlines = [AuthorInline, BookInline]
99
100
101 admin.site.register(models.Collection, CollectionAdmin)