08f9a72a2f876e488f31b535823c88a2a5c778dc
[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.html import escape
6 from django.utils.safestring import mark_safe
7 from django.utils.translation import gettext_lazy as _
8 from admin_numeric_filter.admin import RangeNumericFilter, NumericFilterModelAdmin
9 from fnpdjango.actions import export_as_csv_action
10 from . import models
11 import documents.models
12 from .wikidata import WikidataAdminMixin
13
14
15 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
16     list_display = [
17         "first_name",
18         "last_name",
19         "status",
20         "year_of_death",
21         "gender",
22         "nationality",
23         "priority",
24         "wikidata_link",
25         "slug",
26     ]
27     list_filter = ["year_of_death", "priority", "collections", "status", "gender", "nationality"]
28     search_fields = ["first_name", "last_name", "wikidata"]
29     prepopulated_fields = {"slug": ("first_name", "last_name")}
30     autocomplete_fields = ["collections"]
31
32
33 admin.site.register(models.Author, AuthorAdmin)
34
35
36 class LicenseFilter(admin.SimpleListFilter):
37     title = 'Licencja'
38     parameter_name = 'book_license'
39
40     def lookups(self, requesrt, model_admin):
41         return [
42             ('cc', 'CC'),
43             ('fal', 'FAL'),
44             ('pd', 'domena publiczna'),
45         ]
46
47     def queryset(self, request, queryset):
48         v = self.value()
49         if v == 'cc': 
50             return queryset.filter(document_book__dc__license__icontains='creativecommons.org')
51         elif v == 'fal':
52             return queryset.filter(document_book__dc__license__icontains='artlibre.org')
53         elif v == 'pd':
54             return queryset.filter(document_book__dc__license_description__icontains='domena publiczna')
55         else:
56             return queryset
57
58
59 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
60     list_display = [
61         "smart_title",
62         "authors_str",
63         "translators_str",
64         "language",
65         "pd_year",
66         "priority",
67         "wikidata_link",
68     ]
69     search_fields = [
70         "title", "wikidata",
71         "authors__first_name", "authors__last_name",
72         "translators__first_name", "translators__last_name",
73         "scans_source", "text_source", "notes", "estimate_source",
74     ]
75     autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
76     prepopulated_fields = {"slug": ("title",)}
77     list_filter = [
78         "language",
79         "based_on__language",
80         ("pd_year", RangeNumericFilter),
81         "collections",
82         "collections__category",
83         "epochs", "kinds", "genres",
84         "priority",
85         "authors__gender", "authors__nationality",
86         "translators__gender", "translators__nationality",
87         "document_book__chunk__stage",
88         "document_book__chunk__user",
89
90         LicenseFilter,
91     ]
92     readonly_fields = ["wikidata_link", "estimated_costs"]
93     actions = [export_as_csv_action()]
94     fieldsets = [
95         (None, {"fields": [("wikidata", "wikidata_link")]}),
96         (
97             _("Identification"),
98             {
99                 "fields": [
100                     "title",
101                     "slug",
102                     "authors",
103                     "translators",
104                     "language",
105                     "based_on",
106                     "pd_year",
107                 ]
108             },
109         ),
110         (
111             _("Features"),
112             {
113                 "fields": [
114                     "epochs",
115                     "genres",
116                     "kinds",
117                 ]
118             },
119         ),
120         (
121             _("Plan"),
122             {
123                 "fields": [
124                     "scans_source",
125                     "text_source",
126                     "priority",
127                     "collections",
128                     "notes",
129                     ("estimated_chars", "estimated_verses", "estimate_source"),
130                     "estimated_costs",
131                 ]
132             },
133         ),
134     ]
135
136     def get_queryset(self, request):
137         qs = super().get_queryset(request)
138         if request.resolver_match.view_name.endswith("changelist"):
139             qs = qs.prefetch_related("authors", "translators")
140         return qs
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     def smart_title(self, obj):
152         if obj.title:
153             return obj.title
154         if obj.notes:
155             n = obj.notes
156             if len(n) > 100:
157                 n = n[:100] + '…'
158             return mark_safe(
159                 '<em><small>' + escape(n) + '</small></em>'
160             )
161         return '---'
162     smart_title.short_description = _('Title')
163     smart_title.admin_order_field = 'title'
164     
165
166 admin.site.register(models.Book, BookAdmin)
167
168
169 admin.site.register(models.CollectionCategory)
170
171
172 class AuthorInline(admin.TabularInline):
173     model = models.Author.collections.through
174     autocomplete_fields = ["author"]
175
176
177 class BookInline(admin.TabularInline):
178     model = models.Book.collections.through
179     autocomplete_fields = ["book"]
180
181
182 class CollectionAdmin(admin.ModelAdmin):
183     list_display = ["name"]
184     autocomplete_fields = []
185     prepopulated_fields = {"slug": ("name",)}
186     search_fields = ["name"]
187     fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
188     readonly_fields = ['estimated_costs']
189     inlines = [AuthorInline, BookInline]
190
191     def estimated_costs(self, obj):
192         return "\n".join(
193             "{}: {} zł".format(
194                 work_type.name,
195                 cost or '—'
196             )
197             for work_type, cost in obj.get_estimated_costs().items()
198         )
199
200
201 admin.site.register(models.Collection, CollectionAdmin)
202
203
204
205 class CategoryAdmin(admin.ModelAdmin):
206     search_fields = ["name"]
207
208 admin.site.register(models.Epoch, CategoryAdmin)
209 admin.site.register(models.Genre, CategoryAdmin)
210 admin.site.register(models.Kind, CategoryAdmin)
211
212
213
214 class WorkRateInline(admin.TabularInline):
215     model = models.WorkRate
216     autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
217
218
219 class WorkTypeAdmin(admin.ModelAdmin):
220     inlines = [WorkRateInline]
221
222 admin.site.register(models.WorkType, WorkTypeAdmin)
223