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", "documents_book_link"]
109 actions = [export_as_csv_action()]
111 (None, {"fields": [("wikidata", "wikidata_link")]}),
117 ("slug", 'documents_book_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'
181 def documents_book_link(self, obj):
182 for book in obj.document_books.all():
183 return mark_safe('<a style="position: absolute" href="{}"><img height="100" width="70" src="/cover/preview/{}/?height=100&width=70"></a>'.format(book.get_absolute_url(), book.slug))
184 documents_book_link.short_description = _('Book')
187 admin.site.register(models.Book, BookAdmin)
190 admin.site.register(models.CollectionCategory)
193 class AuthorInline(admin.TabularInline):
194 model = models.Author.collections.through
195 autocomplete_fields = ["author"]
198 class BookInline(admin.TabularInline):
199 model = models.Book.collections.through
200 autocomplete_fields = ["book"]
203 class CollectionAdmin(admin.ModelAdmin):
204 list_display = ["name"]
205 autocomplete_fields = []
206 prepopulated_fields = {"slug": ("name",)}
207 search_fields = ["name"]
208 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
209 readonly_fields = ['estimated_costs']
210 inlines = [AuthorInline, BookInline]
212 def estimated_costs(self, obj):
218 for work_type, cost in obj.get_estimated_costs().items()
222 admin.site.register(models.Collection, CollectionAdmin)
226 class CategoryAdmin(admin.ModelAdmin):
227 search_fields = ["name"]
229 admin.site.register(models.Epoch, CategoryAdmin)
230 admin.site.register(models.Genre, CategoryAdmin)
231 admin.site.register(models.Kind, CategoryAdmin)
235 class WorkRateInline(admin.TabularInline):
236 model = models.WorkRate
237 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
240 class WorkTypeAdmin(admin.ModelAdmin):
241 inlines = [WorkRateInline]
243 admin.site.register(models.WorkType, WorkTypeAdmin)