35345216c37411bfa9af31be98f322c7739b5e96
[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", "estimated_costs"]
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                     ("estimated_chars", "estimated_verses", "estimate_source"),
84                     "estimated_costs",
85                 ]
86             },
87         ),
88     ]
89
90     def get_queryset(self, request):
91         qs = super().get_queryset(request)
92         if request.resolver_match.view_name.endswith("changelist"):
93             qs = qs.prefetch_related("authors", "translators")
94         return qs
95
96
97 admin.site.register(models.Book, BookAdmin)
98
99
100 class AuthorInline(admin.TabularInline):
101     model = models.Author.collections.through
102     autocomplete_fields = ["author"]
103
104
105 class BookInline(admin.TabularInline):
106     model = models.Book.collections.through
107     autocomplete_fields = ["book"]
108
109
110 class CollectionAdmin(admin.ModelAdmin):
111     list_display = ["name"]
112     autocomplete_fields = []
113     prepopulated_fields = {"slug": ("name",)}
114     search_fields = ["name"]
115     inlines = [AuthorInline, BookInline]
116
117
118 admin.site.register(models.Collection, CollectionAdmin)
119
120
121 class CategoryAdmin(admin.ModelAdmin):
122     search_fields = ["name"]
123
124 admin.site.register(models.Epoch, CategoryAdmin)
125 admin.site.register(models.Genre, CategoryAdmin)
126 admin.site.register(models.Kind, CategoryAdmin)
127
128
129
130 class WorkRateInline(admin.TabularInline):
131     model = models.WorkRate
132     autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
133
134
135 class WorkTypeAdmin(admin.ModelAdmin):
136     inlines = [WorkRateInline]
137
138 admin.site.register(models.WorkType, WorkTypeAdmin)
139