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