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",
57 readonly_fields = ["wikidata_link", "estimated_costs"]
58 actions = [export_as_csv_action()]
60 (None, {"fields": [("wikidata", "wikidata_link")]}),
94 ("estimated_chars", "estimated_verses", "estimate_source"),
101 def get_queryset(self, request):
102 qs = super().get_queryset(request)
103 if request.resolver_match.view_name.endswith("changelist"):
104 qs = qs.prefetch_related("authors", "translators")
107 def estimated_costs(self, obj):
113 for work_type, cost in obj.get_estimated_costs().items()
117 admin.site.register(models.Book, BookAdmin)
120 admin.site.register(models.CollectionCategory)
123 class AuthorInline(admin.TabularInline):
124 model = models.Author.collections.through
125 autocomplete_fields = ["author"]
128 class BookInline(admin.TabularInline):
129 model = models.Book.collections.through
130 autocomplete_fields = ["book"]
133 class CollectionAdmin(admin.ModelAdmin):
134 list_display = ["name"]
135 autocomplete_fields = []
136 prepopulated_fields = {"slug": ("name",)}
137 search_fields = ["name"]
138 fields = ['name', 'slug', 'category', 'estimated_costs']
139 readonly_fields = ['estimated_costs']
140 inlines = [AuthorInline, BookInline]
142 def estimated_costs(self, obj):
148 for work_type, cost in obj.get_estimated_costs().items()
152 admin.site.register(models.Collection, CollectionAdmin)
156 class CategoryAdmin(admin.ModelAdmin):
157 search_fields = ["name"]
159 admin.site.register(models.Epoch, CategoryAdmin)
160 admin.site.register(models.Genre, CategoryAdmin)
161 admin.site.register(models.Kind, CategoryAdmin)
165 class WorkRateInline(admin.TabularInline):
166 model = models.WorkRate
167 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
170 class WorkTypeAdmin(admin.ModelAdmin):
171 inlines = [WorkRateInline]
173 admin.site.register(models.WorkType, WorkTypeAdmin)