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 admin_ordering.admin import OrderableAdmin
10 from fnpdjango.actions import export_as_csv_action
11 from modeltranslation.admin import TabbedTranslationAdmin
13 import documents.models
14 from .wikidata import WikidataAdminMixin
17 class NotableBookInline(OrderableAdmin, admin.TabularInline):
18 model = models.NotableBook
19 autocomplete_fields = ['book']
20 ordering_field_hide_input = True
23 class AuthorAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
35 list_display_links = [
36 "first_name", "last_name"
39 ("year_of_death", RangeNumericFilter),
45 ("genitive", admin.EmptyFieldListFilter)
47 list_per_page = 10000000
48 search_fields = ["first_name", "last_name", "wikidata"]
49 readonly_fields = ["wikidata_link", "description_preview"]
52 (None, {"fields": [("wikidata", "wikidata_link")]}),
57 ("first_name", "last_name"),
62 ("date_of_birth", "year_of_birth", "year_of_birth_inexact", "year_of_birth_range", "place_of_birth"),
63 ("date_of_death", "year_of_death", "year_of_death_inexact", "year_of_death_range", "place_of_death"),
64 ("description", "description_preview"),
73 "photo", "photo_source", "photo_attribution",
79 prepopulated_fields = {"slug": ("first_name", "last_name")}
80 autocomplete_fields = ["collections", "place_of_birth", "place_of_death"]
85 def description_preview(self, obj):
86 return obj.generate_description()
89 admin.site.register(models.Author, AuthorAdmin)
92 class LicenseFilter(admin.SimpleListFilter):
94 parameter_name = 'book_license'
95 license_url_field = 'document_book__dc__license'
96 license_name_field = 'document_book__dc__license_description'
98 def lookups(self, requesrt, model_admin):
102 ('pd', 'domena publiczna'),
105 def queryset(self, request, queryset):
108 return queryset.filter(**{
109 self.license_url_field + '__icontains': 'creativecommons.org'
112 return queryset.filter(**{
113 self.license_url_field + '__icontains': 'artlibre.org'
116 return queryset.filter(**{
117 self.license_name_field + '__icontains': 'domena publiczna'
123 class CoverLicenseFilter(LicenseFilter):
124 title = 'Licencja okładki'
125 parameter_name = 'cover_license'
126 license_url_field = 'document_book__dc_cover_image__license_url'
127 license_name_field = 'document_book__dc_cover_image__license_name'
130 def add_title(base_class, suffix):
131 class TitledCategoryFilter(base_class):
132 def __init__(self, *args, **kwargs):
133 super().__init__(*args, **kwargs)
135 return TitledCategoryFilter
139 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
151 "authors__first_name", "authors__last_name",
152 "translators__first_name", "translators__last_name",
153 "scans_source", "text_source", "notes", "estimate_source",
155 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
156 prepopulated_fields = {"slug": ("title",)}
159 "based_on__language",
160 ("pd_year", RangeNumericFilter),
162 "collections__category",
163 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
164 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
165 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
166 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
167 "epochs", "kinds", "genres",
169 "authors__gender", "authors__nationality",
170 "translators__gender", "translators__nationality",
171 "document_book__chunk__stage",
178 list_per_page = 1000000
183 "documents_book_link",
186 actions = [export_as_csv_action(
192 "authors_first_names",
193 "authors_last_names",
194 "translators_first_names",
195 "translators_last_names",
210 (None, {"fields": [("wikidata", "wikidata_link")]}),
216 ("slug", 'documents_book_link'),
240 ("free_license", "polona_missing"),
241 ("scans_source", "scans_source_link"),
246 ("estimated_chars", "estimated_verses", "estimate_source"),
253 def get_queryset(self, request):
254 qs = super().get_queryset(request)
255 if request.resolver_match.view_name.endswith("changelist"):
256 qs = qs.prefetch_related("authors", "translators")
259 def estimated_costs(self, obj):
265 for work_type, cost in obj.get_estimated_costs().items()
268 def smart_title(self, obj):
276 '<em><small>' + escape(n) + '</small></em>'
279 smart_title.short_description = _('Title')
280 smart_title.admin_order_field = 'title'
282 def documents_book_link(self, obj):
283 for book in obj.document_books.all():
284 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))
285 documents_book_link.short_description = _('Book')
287 def scans_source_link(self, obj):
290 '<a href="{url}" target="_blank">{url}</a>',
291 url=obj.scans_source,
295 scans_source_link.short_description = _('scans source')
298 admin.site.register(models.Book, BookAdmin)
301 admin.site.register(models.CollectionCategory)
304 class AuthorInline(admin.TabularInline):
305 model = models.Author.collections.through
306 autocomplete_fields = ["author"]
309 class BookInline(admin.TabularInline):
310 model = models.Book.collections.through
311 autocomplete_fields = ["book"]
314 class CollectionAdmin(admin.ModelAdmin):
315 list_display = ["name"]
316 autocomplete_fields = []
317 prepopulated_fields = {"slug": ("name",)}
318 search_fields = ["name"]
319 fields = ['name', 'slug', 'category', 'description', 'notes', 'estimated_costs']
320 readonly_fields = ['estimated_costs']
321 inlines = [AuthorInline, BookInline]
323 def estimated_costs(self, obj):
329 for work_type, cost in obj.get_estimated_costs().items()
333 admin.site.register(models.Collection, CollectionAdmin)
337 class CategoryAdmin(admin.ModelAdmin):
338 search_fields = ["name"]
341 @admin.register(models.Epoch)
342 class EpochAdmin(CategoryAdmin):
343 list_display = ['name', 'adjective_feminine_singular', 'adjective_nonmasculine_plural']
346 @admin.register(models.Genre)
347 class GenreAdmin(CategoryAdmin):
348 list_display = ['name', 'plural', 'is_epoch_specific']
351 @admin.register(models.Kind)
352 class KindAdmin(CategoryAdmin):
353 list_display = ['name', 'collective_noun']
357 class WorkRateInline(admin.TabularInline):
358 model = models.WorkRate
359 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
362 class WorkTypeAdmin(admin.ModelAdmin):
363 inlines = [WorkRateInline]
365 admin.site.register(models.WorkType, WorkTypeAdmin)
369 @admin.register(models.Place)
370 class PlaceAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
371 search_fields = ['name']