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'
39 license_url_field = 'document_book__dc__license'
40 license_name_field = 'document_book__dc__license_description'
42 def lookups(self, requesrt, model_admin):
46 ('pd', 'domena publiczna'),
49 def queryset(self, request, queryset):
52 return queryset.filter(**{
53 self.license_url_field + '__icontains': 'creativecommons.org'
56 return queryset.filter(**{
57 self.license_url_field + '__icontains': 'artlibre.org'
60 return queryset.filter(**{
61 self.license_name_field + '__icontains': 'domena publiczna'
67 class CoverLicenseFilter(LicenseFilter):
68 title = 'Licencja okładki'
69 parameter_name = 'cover_license'
70 license_url_field = 'document_book__dc_cover_image__license_url'
71 license_name_field = 'document_book__dc_cover_image__license_name'
74 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
86 "authors__first_name", "authors__last_name",
87 "translators__first_name", "translators__last_name",
88 "scans_source", "text_source", "notes", "estimate_source",
90 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
91 prepopulated_fields = {"slug": ("title",)}
95 ("pd_year", RangeNumericFilter),
97 "collections__category",
98 "epochs", "kinds", "genres",
100 "authors__gender", "authors__nationality",
101 "translators__gender", "translators__nationality",
102 "document_book__chunk__stage",
103 "document_book__chunk__user",
108 readonly_fields = ["wikidata_link", "estimated_costs"]
109 actions = [export_as_csv_action()]
111 (None, {"fields": [("wikidata", "wikidata_link")]}),
145 ("estimated_chars", "estimated_verses", "estimate_source"),
152 def get_queryset(self, request):
153 qs = super().get_queryset(request)
154 if request.resolver_match.view_name.endswith("changelist"):
155 qs = qs.prefetch_related("authors", "translators")
158 def estimated_costs(self, obj):
164 for work_type, cost in obj.get_estimated_costs().items()
167 def smart_title(self, obj):
175 '<em><small>' + escape(n) + '</small></em>'
178 smart_title.short_description = _('Title')
179 smart_title.admin_order_field = 'title'
182 admin.site.register(models.Book, BookAdmin)
185 admin.site.register(models.CollectionCategory)
188 class AuthorInline(admin.TabularInline):
189 model = models.Author.collections.through
190 autocomplete_fields = ["author"]
193 class BookInline(admin.TabularInline):
194 model = models.Book.collections.through
195 autocomplete_fields = ["book"]
198 class CollectionAdmin(admin.ModelAdmin):
199 list_display = ["name"]
200 autocomplete_fields = []
201 prepopulated_fields = {"slug": ("name",)}
202 search_fields = ["name"]
203 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
204 readonly_fields = ['estimated_costs']
205 inlines = [AuthorInline, BookInline]
207 def estimated_costs(self, obj):
213 for work_type, cost in obj.get_estimated_costs().items()
217 admin.site.register(models.Collection, CollectionAdmin)
221 class CategoryAdmin(admin.ModelAdmin):
222 search_fields = ["name"]
224 admin.site.register(models.Epoch, CategoryAdmin)
225 admin.site.register(models.Genre, CategoryAdmin)
226 admin.site.register(models.Kind, CategoryAdmin)
230 class WorkRateInline(admin.TabularInline):
231 model = models.WorkRate
232 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
235 class WorkTypeAdmin(admin.ModelAdmin):
236 inlines = [WorkRateInline]
238 admin.site.register(models.WorkType, WorkTypeAdmin)