608d625d533daa77eedf5f50b5f1df7d1dac3372
[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 django.utils.translation import gettext_lazy as _
6 from . import models
7 from .wikidata import WikidataAdminMixin
8
9
10 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
11     list_display = [
12         "first_name",
13         "last_name",
14         "status",
15         "year_of_death",
16         "gender",
17         "nationality",
18         "priority",
19         "wikidata_link",
20         "slug",
21     ]
22     list_filter = ["year_of_death", "priority", "collections", "status", "gender", "nationality"]
23     search_fields = ["first_name", "last_name", "wikidata"]
24     prepopulated_fields = {"slug": ("first_name", "last_name")}
25     autocomplete_fields = ["collections"]
26
27
28 admin.site.register(models.Author, AuthorAdmin)
29
30
31 class BookAdmin(WikidataAdminMixin, admin.ModelAdmin):
32     list_display = [
33         "title",
34         "authors_str",
35         "translators_str",
36         "language",
37         "pd_year",
38         "priority",
39         "wikidata_link",
40     ]
41     search_fields = ["title", "wikidata"]
42     autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
43     prepopulated_fields = {"slug": ("title",)}
44     list_filter = ["language", "pd_year", "collections"]
45     readonly_fields = ["wikidata_link"]
46     fieldsets = [
47         (None, {"fields": [("wikidata", "wikidata_link")]}),
48         (
49             _("Identification"),
50             {
51                 "fields": [
52                     "title",
53                     "slug",
54                     "authors",
55                     "translators",
56                     "language",
57                     "based_on",
58                     "pd_year",
59                 ]
60             },
61         ),
62         (
63             _("Features"),
64             {
65                 "fields": [
66                     "epochs",
67                     "genres",
68                     "kinds",
69                 ]
70             },
71         ),
72         (
73             _("Plan"),
74             {
75                 "fields": [
76                     "scans_source",
77                     "text_source",
78                     "priority",
79                     "collections",
80                     "notes",
81                 ]
82             },
83         ),
84     ]
85
86     def get_queryset(self, request):
87         qs = super().get_queryset(request)
88         if request.resolver_match.view_name.endswith("changelist"):
89             qs = qs.prefetch_related("authors", "translators")
90         return qs
91
92
93 admin.site.register(models.Book, BookAdmin)
94
95
96 class AuthorInline(admin.TabularInline):
97     model = models.Author.collections.through
98     autocomplete_fields = ["author"]
99
100
101 class BookInline(admin.TabularInline):
102     model = models.Book.collections.through
103     autocomplete_fields = ["book"]
104
105
106 class CollectionAdmin(admin.ModelAdmin):
107     list_display = ["name"]
108     autocomplete_fields = []
109     prepopulated_fields = {"slug": ("name",)}
110     search_fields = ["name"]
111     inlines = [AuthorInline, BookInline]
112
113
114 admin.site.register(models.Collection, CollectionAdmin)
115
116
117 class CategoryAdmin(admin.ModelAdmin):
118     search_fields = ["name"]
119
120 admin.site.register(models.Epoch, CategoryAdmin)
121 admin.site.register(models.Genre, CategoryAdmin)
122 admin.site.register(models.Kind, CategoryAdmin)