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, format_html
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_display_links = [
28 "first_name", "last_name"
31 ("year_of_death", RangeNumericFilter),
38 list_per_page = 10000000
39 search_fields = ["first_name", "last_name", "wikidata"]
40 prepopulated_fields = {"slug": ("first_name", "last_name")}
41 autocomplete_fields = ["collections"]
44 admin.site.register(models.Author, AuthorAdmin)
47 class LicenseFilter(admin.SimpleListFilter):
49 parameter_name = 'book_license'
50 license_url_field = 'document_book__dc__license'
51 license_name_field = 'document_book__dc__license_description'
53 def lookups(self, requesrt, model_admin):
57 ('pd', 'domena publiczna'),
60 def queryset(self, request, queryset):
63 return queryset.filter(**{
64 self.license_url_field + '__icontains': 'creativecommons.org'
67 return queryset.filter(**{
68 self.license_url_field + '__icontains': 'artlibre.org'
71 return queryset.filter(**{
72 self.license_name_field + '__icontains': 'domena publiczna'
78 class CoverLicenseFilter(LicenseFilter):
79 title = 'Licencja okładki'
80 parameter_name = 'cover_license'
81 license_url_field = 'document_book__dc_cover_image__license_url'
82 license_name_field = 'document_book__dc_cover_image__license_name'
85 def add_title(base_class, suffix):
86 class TitledCategoryFilter(base_class):
87 def __init__(self, *args, **kwargs):
88 super().__init__(*args, **kwargs)
90 return TitledCategoryFilter
94 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
106 "authors__first_name", "authors__last_name",
107 "translators__first_name", "translators__last_name",
108 "scans_source", "text_source", "notes", "estimate_source",
110 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
111 prepopulated_fields = {"slug": ("title",)}
114 "based_on__language",
115 ("pd_year", RangeNumericFilter),
117 "collections__category",
118 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
119 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
120 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
121 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
122 "epochs", "kinds", "genres",
124 "authors__gender", "authors__nationality",
125 "translators__gender", "translators__nationality",
126 "document_book__chunk__stage",
133 list_per_page = 1000000
138 "documents_book_link",
141 actions = [export_as_csv_action(
147 "authors_str", # authors?
148 "translators_str", # translators?
163 (None, {"fields": [("wikidata", "wikidata_link")]}),
169 ("slug", 'documents_book_link'),
192 ("free_license", "polona_missing"),
193 ("scans_source", "scans_source_link"),
198 ("estimated_chars", "estimated_verses", "estimate_source"),
205 def get_queryset(self, request):
206 qs = super().get_queryset(request)
207 if request.resolver_match.view_name.endswith("changelist"):
208 qs = qs.prefetch_related("authors", "translators")
211 def estimated_costs(self, obj):
217 for work_type, cost in obj.get_estimated_costs().items()
220 def smart_title(self, obj):
228 '<em><small>' + escape(n) + '</small></em>'
231 smart_title.short_description = _('Title')
232 smart_title.admin_order_field = 'title'
234 def documents_book_link(self, obj):
235 for book in obj.document_books.all():
236 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))
237 documents_book_link.short_description = _('Book')
239 def scans_source_link(self, obj):
242 '<a href="{url}" target="_blank">{url}</a>',
243 url=obj.scans_source,
247 scans_source_link.short_description = _('scans source')
250 admin.site.register(models.Book, BookAdmin)
253 admin.site.register(models.CollectionCategory)
256 class AuthorInline(admin.TabularInline):
257 model = models.Author.collections.through
258 autocomplete_fields = ["author"]
261 class BookInline(admin.TabularInline):
262 model = models.Book.collections.through
263 autocomplete_fields = ["book"]
266 class CollectionAdmin(admin.ModelAdmin):
267 list_display = ["name"]
268 autocomplete_fields = []
269 prepopulated_fields = {"slug": ("name",)}
270 search_fields = ["name"]
271 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
272 readonly_fields = ['estimated_costs']
273 inlines = [AuthorInline, BookInline]
275 def estimated_costs(self, obj):
281 for work_type, cost in obj.get_estimated_costs().items()
285 admin.site.register(models.Collection, CollectionAdmin)
289 class CategoryAdmin(admin.ModelAdmin):
290 search_fields = ["name"]
292 admin.site.register(models.Epoch, CategoryAdmin)
293 admin.site.register(models.Genre, CategoryAdmin)
294 admin.site.register(models.Kind, CategoryAdmin)
298 class WorkRateInline(admin.TabularInline):
299 model = models.WorkRate
300 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
303 class WorkTypeAdmin(admin.ModelAdmin):
304 inlines = [WorkRateInline]
306 admin.site.register(models.WorkType, WorkTypeAdmin)