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):
43 search_fields = ["title", "wikidata", "authors__first_name", "authors__last_name", "translators__first_name", "translators__last_name"]
44 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
45 prepopulated_fields = {"slug": ("title",)}
49 ("pd_year", RangeNumericFilter),
51 "collections__category",
52 "epochs", "kinds", "genres",
54 "authors__gender", "authors__nationality",
55 "translators__gender", "translators__nationality",
56 "document_book__chunk__stage",
57 "document_book__chunk__user",
59 readonly_fields = ["wikidata_link", "estimated_costs"]
60 actions = [export_as_csv_action()]
62 (None, {"fields": [("wikidata", "wikidata_link")]}),
96 ("estimated_chars", "estimated_verses", "estimate_source"),
103 def get_queryset(self, request):
104 qs = super().get_queryset(request)
105 if request.resolver_match.view_name.endswith("changelist"):
106 qs = qs.prefetch_related("authors", "translators")
109 def estimated_costs(self, obj):
115 for work_type, cost in obj.get_estimated_costs().items()
119 admin.site.register(models.Book, BookAdmin)
122 admin.site.register(models.CollectionCategory)
125 class AuthorInline(admin.TabularInline):
126 model = models.Author.collections.through
127 autocomplete_fields = ["author"]
130 class BookInline(admin.TabularInline):
131 model = models.Book.collections.through
132 autocomplete_fields = ["book"]
135 class CollectionAdmin(admin.ModelAdmin):
136 list_display = ["name"]
137 autocomplete_fields = []
138 prepopulated_fields = {"slug": ("name",)}
139 search_fields = ["name"]
140 fields = ['name', 'slug', 'category', 'estimated_costs']
141 readonly_fields = ['estimated_costs']
142 inlines = [AuthorInline, BookInline]
144 def estimated_costs(self, obj):
150 for work_type, cost in obj.get_estimated_costs().items()
154 admin.site.register(models.Collection, CollectionAdmin)
158 class CategoryAdmin(admin.ModelAdmin):
159 search_fields = ["name"]
161 admin.site.register(models.Epoch, CategoryAdmin)
162 admin.site.register(models.Genre, CategoryAdmin)
163 admin.site.register(models.Kind, CategoryAdmin)
167 class WorkRateInline(admin.TabularInline):
168 model = models.WorkRate
169 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
172 class WorkTypeAdmin(admin.ModelAdmin):
173 inlines = [WorkRateInline]
175 admin.site.register(models.WorkType, WorkTypeAdmin)