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.translation import gettext_lazy as _
6 from admin_numeric_filter.admin import RangeNumericFilter, NumericFilterModelAdmin
7 from fnpdjango.actions import export_as_csv_action
9 from .wikidata import WikidataAdminMixin
12 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
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"]
30 admin.site.register(models.Author, AuthorAdmin)
33 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
45 "authors__first_name", "authors__last_name",
46 "translators__first_name", "translators__last_name",
47 "scans_source", "text_source", "notes", "estimate_source",
49 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
50 prepopulated_fields = {"slug": ("title",)}
54 ("pd_year", RangeNumericFilter),
56 "collections__category",
57 "epochs", "kinds", "genres",
59 "authors__gender", "authors__nationality",
60 "translators__gender", "translators__nationality",
61 "document_book__chunk__stage",
62 "document_book__chunk__user",
64 readonly_fields = ["wikidata_link", "estimated_costs"]
65 actions = [export_as_csv_action()]
67 (None, {"fields": [("wikidata", "wikidata_link")]}),
101 ("estimated_chars", "estimated_verses", "estimate_source"),
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")
114 def estimated_costs(self, obj):
120 for work_type, cost in obj.get_estimated_costs().items()
124 admin.site.register(models.Book, BookAdmin)
127 admin.site.register(models.CollectionCategory)
130 class AuthorInline(admin.TabularInline):
131 model = models.Author.collections.through
132 autocomplete_fields = ["author"]
135 class BookInline(admin.TabularInline):
136 model = models.Book.collections.through
137 autocomplete_fields = ["book"]
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]
149 def estimated_costs(self, obj):
155 for work_type, cost in obj.get_estimated_costs().items()
159 admin.site.register(models.Collection, CollectionAdmin)
163 class CategoryAdmin(admin.ModelAdmin):
164 search_fields = ["name"]
166 admin.site.register(models.Epoch, CategoryAdmin)
167 admin.site.register(models.Genre, CategoryAdmin)
168 admin.site.register(models.Kind, CategoryAdmin)
172 class WorkRateInline(admin.TabularInline):
173 model = models.WorkRate
174 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
177 class WorkTypeAdmin(admin.ModelAdmin):
178 inlines = [WorkRateInline]
180 admin.site.register(models.WorkType, WorkTypeAdmin)