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.
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
11 from .wikidata import WikidataAdminMixin
14 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
26 list_filter = ["year_of_death", "priority", "collections", "status", "gender", "nationality"]
27 search_fields = ["first_name", "last_name", "wikidata"]
28 prepopulated_fields = {"slug": ("first_name", "last_name")}
29 autocomplete_fields = ["collections"]
32 admin.site.register(models.Author, AuthorAdmin)
35 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
47 "authors__first_name", "authors__last_name",
48 "translators__first_name", "translators__last_name",
49 "scans_source", "text_source", "notes", "estimate_source",
51 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
52 prepopulated_fields = {"slug": ("title",)}
56 ("pd_year", RangeNumericFilter),
58 "collections__category",
59 "epochs", "kinds", "genres",
61 "authors__gender", "authors__nationality",
62 "translators__gender", "translators__nationality",
63 "document_book__chunk__stage",
64 "document_book__chunk__user",
66 readonly_fields = ["wikidata_link", "estimated_costs"]
67 actions = [export_as_csv_action()]
69 (None, {"fields": [("wikidata", "wikidata_link")]}),
103 ("estimated_chars", "estimated_verses", "estimate_source"),
110 def get_queryset(self, request):
111 qs = super().get_queryset(request)
112 if request.resolver_match.view_name.endswith("changelist"):
113 qs = qs.prefetch_related("authors", "translators")
116 def estimated_costs(self, obj):
122 for work_type, cost in obj.get_estimated_costs().items()
125 def smart_title(self, obj):
133 '<em><small>' + escape(n) + '</small></em>'
136 smart_title.short_description = _('Title')
137 smart_title.admin_order_field = 'title'
140 admin.site.register(models.Book, BookAdmin)
143 admin.site.register(models.CollectionCategory)
146 class AuthorInline(admin.TabularInline):
147 model = models.Author.collections.through
148 autocomplete_fields = ["author"]
151 class BookInline(admin.TabularInline):
152 model = models.Book.collections.through
153 autocomplete_fields = ["book"]
156 class CollectionAdmin(admin.ModelAdmin):
157 list_display = ["name"]
158 autocomplete_fields = []
159 prepopulated_fields = {"slug": ("name",)}
160 search_fields = ["name"]
161 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
162 readonly_fields = ['estimated_costs']
163 inlines = [AuthorInline, BookInline]
165 def estimated_costs(self, obj):
171 for work_type, cost in obj.get_estimated_costs().items()
175 admin.site.register(models.Collection, CollectionAdmin)
179 class CategoryAdmin(admin.ModelAdmin):
180 search_fields = ["name"]
182 admin.site.register(models.Epoch, CategoryAdmin)
183 admin.site.register(models.Genre, CategoryAdmin)
184 admin.site.register(models.Kind, CategoryAdmin)
188 class WorkRateInline(admin.TabularInline):
189 model = models.WorkRate
190 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
193 class WorkTypeAdmin(admin.ModelAdmin):
194 inlines = [WorkRateInline]
196 admin.site.register(models.WorkType, WorkTypeAdmin)