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