Selection fixes.
[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 admin_numeric_filter.admin import RangeNumericFilter, NumericFilterModelAdmin
7 from fnpdjango.actions import export_as_csv_action
8 from . import models
9 from .wikidata import WikidataAdminMixin
10
11
12 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
13     list_display = [
14         "first_name",
15         "last_name",
16         "status",
17         "year_of_death",
18         "gender",
19         "nationality",
20         "priority",
21         "wikidata_link",
22         "slug",
23     ]
24     list_filter = ["year_of_death", "priority", "collections", "status", "gender", "nationality"]
25     search_fields = ["first_name", "last_name", "wikidata"]
26     prepopulated_fields = {"slug": ("first_name", "last_name")}
27     autocomplete_fields = ["collections"]
28
29
30 admin.site.register(models.Author, AuthorAdmin)
31
32
33 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
34     list_display = [
35         "title",
36         "authors_str",
37         "translators_str",
38         "language",
39         "pd_year",
40         "priority",
41         "wikidata_link",
42     ]
43     search_fields = ["title", "wikidata", "authors__first_name", "authors__last_name", "translators__first_name", "translators__last_name"]
44     autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
45     prepopulated_fields = {"slug": ("title",)}
46     list_filter = [
47         "language",
48         "based_on__language",
49         ("pd_year", RangeNumericFilter),
50         "collections",
51         "collections__category",
52         "epochs", "kinds", "genres",
53         "priority",
54         "authors__gender", "authors__nationality",
55         "translators__gender", "translators__nationality",
56         "document_book__chunk__stage",
57         "document_book__chunk__user",
58     ]
59     readonly_fields = ["wikidata_link", "estimated_costs"]
60     actions = [export_as_csv_action()]
61     fieldsets = [
62         (None, {"fields": [("wikidata", "wikidata_link")]}),
63         (
64             _("Identification"),
65             {
66                 "fields": [
67                     "title",
68                     "slug",
69                     "authors",
70                     "translators",
71                     "language",
72                     "based_on",
73                     "pd_year",
74                 ]
75             },
76         ),
77         (
78             _("Features"),
79             {
80                 "fields": [
81                     "epochs",
82                     "genres",
83                     "kinds",
84                 ]
85             },
86         ),
87         (
88             _("Plan"),
89             {
90                 "fields": [
91                     "scans_source",
92                     "text_source",
93                     "priority",
94                     "collections",
95                     "notes",
96                     ("estimated_chars", "estimated_verses", "estimate_source"),
97                     "estimated_costs",
98                 ]
99             },
100         ),
101     ]
102
103     def get_queryset(self, request):
104         qs = super().get_queryset(request)
105         if request.resolver_match.view_name.endswith("changelist"):
106             qs = qs.prefetch_related("authors", "translators")
107         return qs
108
109     def estimated_costs(self, obj):
110         return "\n".join(
111             "{}: {} zł".format(
112                 work_type.name,
113                 cost or '—'
114             )
115             for work_type, cost in obj.get_estimated_costs().items()
116         )
117
118
119 admin.site.register(models.Book, BookAdmin)
120
121
122 admin.site.register(models.CollectionCategory)
123
124
125 class AuthorInline(admin.TabularInline):
126     model = models.Author.collections.through
127     autocomplete_fields = ["author"]
128
129
130 class BookInline(admin.TabularInline):
131     model = models.Book.collections.through
132     autocomplete_fields = ["book"]
133
134
135 class CollectionAdmin(admin.ModelAdmin):
136     list_display = ["name"]
137     autocomplete_fields = []
138     prepopulated_fields = {"slug": ("name",)}
139     search_fields = ["name"]
140     fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
141     readonly_fields = ['estimated_costs']
142     inlines = [AuthorInline, BookInline]
143
144     def estimated_costs(self, obj):
145         return "\n".join(
146             "{}: {} zł".format(
147                 work_type.name,
148                 cost or '—'
149             )
150             for work_type, cost in obj.get_estimated_costs().items()
151         )
152
153
154 admin.site.register(models.Collection, CollectionAdmin)
155
156
157
158 class CategoryAdmin(admin.ModelAdmin):
159     search_fields = ["name"]
160
161 admin.site.register(models.Epoch, CategoryAdmin)
162 admin.site.register(models.Genre, CategoryAdmin)
163 admin.site.register(models.Kind, CategoryAdmin)
164
165
166
167 class WorkRateInline(admin.TabularInline):
168     model = models.WorkRate
169     autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
170
171
172 class WorkTypeAdmin(admin.ModelAdmin):
173     inlines = [WorkRateInline]
174
175 admin.site.register(models.WorkType, WorkTypeAdmin)
176