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 collections import Counter
5 from django.contrib import admin
6 from django.utils.translation import gettext_lazy as _
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, admin.ModelAdmin):
43 search_fields = ["title", "wikidata"]
44 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
45 prepopulated_fields = {"slug": ("title",)}
46 list_filter = ["language", "pd_year", "collections"]
47 readonly_fields = ["wikidata_link", "estimated_costs"]
48 actions = [export_as_csv_action()]
50 (None, {"fields": [("wikidata", "wikidata_link")]}),
84 ("estimated_chars", "estimated_verses", "estimate_source"),
91 def get_queryset(self, request):
92 qs = super().get_queryset(request)
93 if request.resolver_match.view_name.endswith("changelist"):
94 qs = qs.prefetch_related("authors", "translators")
97 def estimated_costs(self, obj):
103 for work_type, cost in obj.get_estimated_costs().items()
107 admin.site.register(models.Book, BookAdmin)
110 class AuthorInline(admin.TabularInline):
111 model = models.Author.collections.through
112 autocomplete_fields = ["author"]
115 class BookInline(admin.TabularInline):
116 model = models.Book.collections.through
117 autocomplete_fields = ["book"]
120 class CollectionAdmin(admin.ModelAdmin):
121 list_display = ["name"]
122 autocomplete_fields = []
123 prepopulated_fields = {"slug": ("name",)}
124 search_fields = ["name"]
125 fields = ['name', 'slug', 'estimated_costs']
126 readonly_fields = ['estimated_costs']
127 inlines = [AuthorInline, BookInline]
129 def estimated_costs(self, obj):
131 for book in obj.book_set.all():
132 for k, v in book.get_estimated_costs().items():
135 for author in obj.author_set.all():
136 for book in author.book_set.all():
137 for k, v in book.get_estimated_costs().items():
139 for book in author.translated_book_set.all():
140 for k, v in book.get_estimated_costs().items():
148 for work_type, cost in costs.items()
152 admin.site.register(models.Collection, CollectionAdmin)
155 class CategoryAdmin(admin.ModelAdmin):
156 search_fields = ["name"]
158 admin.site.register(models.Epoch, CategoryAdmin)
159 admin.site.register(models.Genre, CategoryAdmin)
160 admin.site.register(models.Kind, CategoryAdmin)
164 class WorkRateInline(admin.TabularInline):
165 model = models.WorkRate
166 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
169 class WorkTypeAdmin(admin.ModelAdmin):
170 inlines = [WorkRateInline]
172 admin.site.register(models.WorkType, WorkTypeAdmin)