Use unmanaged keys instead of some magic.
[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         ]
57     readonly_fields = ["wikidata_link", "estimated_costs"]
58     actions = [export_as_csv_action()]
59     fieldsets = [
60         (None, {"fields": [("wikidata", "wikidata_link")]}),
61         (
62             _("Identification"),
63             {
64                 "fields": [
65                     "title",
66                     "slug",
67                     "authors",
68                     "translators",
69                     "language",
70                     "based_on",
71                     "pd_year",
72                 ]
73             },
74         ),
75         (
76             _("Features"),
77             {
78                 "fields": [
79                     "epochs",
80                     "genres",
81                     "kinds",
82                 ]
83             },
84         ),
85         (
86             _("Plan"),
87             {
88                 "fields": [
89                     "scans_source",
90                     "text_source",
91                     "priority",
92                     "collections",
93                     "notes",
94                     ("estimated_chars", "estimated_verses", "estimate_source"),
95                     "estimated_costs",
96                 ]
97             },
98         ),
99     ]
100
101     def get_queryset(self, request):
102         qs = super().get_queryset(request)
103         if request.resolver_match.view_name.endswith("changelist"):
104             qs = qs.prefetch_related("authors", "translators")
105         return qs
106
107     def estimated_costs(self, obj):
108         return "\n".join(
109             "{}: {} zł".format(
110                 work_type.name,
111                 cost or '—'
112             )
113             for work_type, cost in obj.get_estimated_costs().items()
114         )
115
116
117 admin.site.register(models.Book, BookAdmin)
118
119
120 admin.site.register(models.CollectionCategory)
121
122
123 class AuthorInline(admin.TabularInline):
124     model = models.Author.collections.through
125     autocomplete_fields = ["author"]
126
127
128 class BookInline(admin.TabularInline):
129     model = models.Book.collections.through
130     autocomplete_fields = ["book"]
131
132
133 class CollectionAdmin(admin.ModelAdmin):
134     list_display = ["name"]
135     autocomplete_fields = []
136     prepopulated_fields = {"slug": ("name",)}
137     search_fields = ["name"]
138     fields = ['name', 'slug', 'category', 'estimated_costs']
139     readonly_fields = ['estimated_costs']
140     inlines = [AuthorInline, BookInline]
141
142     def estimated_costs(self, obj):
143         return "\n".join(
144             "{}: {} zł".format(
145                 work_type.name,
146                 cost or '—'
147             )
148             for work_type, cost in obj.get_estimated_costs().items()
149         )
150
151
152 admin.site.register(models.Collection, CollectionAdmin)
153
154
155
156 class CategoryAdmin(admin.ModelAdmin):
157     search_fields = ["name"]
158
159 admin.site.register(models.Epoch, CategoryAdmin)
160 admin.site.register(models.Genre, CategoryAdmin)
161 admin.site.register(models.Kind, CategoryAdmin)
162
163
164
165 class WorkRateInline(admin.TabularInline):
166     model = models.WorkRate
167     autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
168
169
170 class WorkTypeAdmin(admin.ModelAdmin):
171     inlines = [WorkRateInline]
172
173 admin.site.register(models.WorkType, WorkTypeAdmin)
174