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