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 import documents.models
12 from .wikidata import WikidataAdminMixin
15 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
27 list_filter = ["year_of_death", "priority", "collections", "status", "gender", "nationality"]
28 search_fields = ["first_name", "last_name", "wikidata"]
29 prepopulated_fields = {"slug": ("first_name", "last_name")}
30 autocomplete_fields = ["collections"]
33 admin.site.register(models.Author, AuthorAdmin)
36 class LicenseFilter(admin.SimpleListFilter):
38 parameter_name = 'book_license'
40 def lookups(self, requesrt, model_admin):
44 ('pd', 'domena publiczna'),
47 def queryset(self, request, queryset):
50 return queryset.filter(document_book__dc__license__icontains='creativecommons.org')
52 return queryset.filter(document_book__dc__license__icontains='artlibre.org')
54 return queryset.filter(document_book__dc__license_description__icontains='domena publiczna')
59 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
71 "authors__first_name", "authors__last_name",
72 "translators__first_name", "translators__last_name",
73 "scans_source", "text_source", "notes", "estimate_source",
75 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
76 prepopulated_fields = {"slug": ("title",)}
80 ("pd_year", RangeNumericFilter),
82 "collections__category",
83 "epochs", "kinds", "genres",
85 "authors__gender", "authors__nationality",
86 "translators__gender", "translators__nationality",
87 "document_book__chunk__stage",
88 "document_book__chunk__user",
92 readonly_fields = ["wikidata_link", "estimated_costs"]
93 actions = [export_as_csv_action()]
95 (None, {"fields": [("wikidata", "wikidata_link")]}),
129 ("estimated_chars", "estimated_verses", "estimate_source"),
136 def get_queryset(self, request):
137 qs = super().get_queryset(request)
138 if request.resolver_match.view_name.endswith("changelist"):
139 qs = qs.prefetch_related("authors", "translators")
142 def estimated_costs(self, obj):
148 for work_type, cost in obj.get_estimated_costs().items()
151 def smart_title(self, obj):
159 '<em><small>' + escape(n) + '</small></em>'
162 smart_title.short_description = _('Title')
163 smart_title.admin_order_field = 'title'
166 admin.site.register(models.Book, BookAdmin)
169 admin.site.register(models.CollectionCategory)
172 class AuthorInline(admin.TabularInline):
173 model = models.Author.collections.through
174 autocomplete_fields = ["author"]
177 class BookInline(admin.TabularInline):
178 model = models.Book.collections.through
179 autocomplete_fields = ["book"]
182 class CollectionAdmin(admin.ModelAdmin):
183 list_display = ["name"]
184 autocomplete_fields = []
185 prepopulated_fields = {"slug": ("name",)}
186 search_fields = ["name"]
187 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
188 readonly_fields = ['estimated_costs']
189 inlines = [AuthorInline, BookInline]
191 def estimated_costs(self, obj):
197 for work_type, cost in obj.get_estimated_costs().items()
201 admin.site.register(models.Collection, CollectionAdmin)
205 class CategoryAdmin(admin.ModelAdmin):
206 search_fields = ["name"]
208 admin.site.register(models.Epoch, CategoryAdmin)
209 admin.site.register(models.Genre, CategoryAdmin)
210 admin.site.register(models.Kind, CategoryAdmin)
214 class WorkRateInline(admin.TabularInline):
215 model = models.WorkRate
216 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
219 class WorkTypeAdmin(admin.ModelAdmin):
220 inlines = [WorkRateInline]
222 admin.site.register(models.WorkType, WorkTypeAdmin)