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),
46 list_per_page = 10000000
47 search_fields = ["first_name", "last_name", "wikidata"]
48 readonly_fields = ["wikidata_link", "description_preview"]
51 (None, {"fields": [("wikidata", "wikidata_link")]}),
56 ("first_name", "last_name"),
60 ("date_of_birth", "year_of_birth", "year_of_birth_inexact", "year_of_birth_range", "place_of_birth"),
61 ("date_of_death", "year_of_death", "year_of_death_inexact", "year_of_death_range", "place_of_death"),
62 ("description", "description_preview"),
71 "photo", "photo_source", "photo_attribution",
77 prepopulated_fields = {"slug": ("first_name", "last_name")}
78 autocomplete_fields = ["collections", "place_of_birth", "place_of_death"]
83 def description_preview(self, obj):
84 return obj.generate_description()
87 admin.site.register(models.Author, AuthorAdmin)
90 class LicenseFilter(admin.SimpleListFilter):
92 parameter_name = 'book_license'
93 license_url_field = 'document_book__dc__license'
94 license_name_field = 'document_book__dc__license_description'
96 def lookups(self, requesrt, model_admin):
100 ('pd', 'domena publiczna'),
103 def queryset(self, request, queryset):
106 return queryset.filter(**{
107 self.license_url_field + '__icontains': 'creativecommons.org'
110 return queryset.filter(**{
111 self.license_url_field + '__icontains': 'artlibre.org'
114 return queryset.filter(**{
115 self.license_name_field + '__icontains': 'domena publiczna'
121 class CoverLicenseFilter(LicenseFilter):
122 title = 'Licencja okładki'
123 parameter_name = 'cover_license'
124 license_url_field = 'document_book__dc_cover_image__license_url'
125 license_name_field = 'document_book__dc_cover_image__license_name'
128 def add_title(base_class, suffix):
129 class TitledCategoryFilter(base_class):
130 def __init__(self, *args, **kwargs):
131 super().__init__(*args, **kwargs)
133 return TitledCategoryFilter
137 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
149 "authors__first_name", "authors__last_name",
150 "translators__first_name", "translators__last_name",
151 "scans_source", "text_source", "notes", "estimate_source",
153 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
154 prepopulated_fields = {"slug": ("title",)}
157 "based_on__language",
158 ("pd_year", RangeNumericFilter),
160 "collections__category",
161 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
162 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
163 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
164 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
165 "epochs", "kinds", "genres",
167 "authors__gender", "authors__nationality",
168 "translators__gender", "translators__nationality",
169 "document_book__chunk__stage",
176 list_per_page = 1000000
181 "documents_book_link",
184 actions = [export_as_csv_action(
190 "authors_str", # authors?
191 "translators_str", # translators?
206 (None, {"fields": [("wikidata", "wikidata_link")]}),
212 ("slug", 'documents_book_link'),
236 ("free_license", "polona_missing"),
237 ("scans_source", "scans_source_link"),
242 ("estimated_chars", "estimated_verses", "estimate_source"),
249 def get_queryset(self, request):
250 qs = super().get_queryset(request)
251 if request.resolver_match.view_name.endswith("changelist"):
252 qs = qs.prefetch_related("authors", "translators")
255 def estimated_costs(self, obj):
261 for work_type, cost in obj.get_estimated_costs().items()
264 def smart_title(self, obj):
272 '<em><small>' + escape(n) + '</small></em>'
275 smart_title.short_description = _('Title')
276 smart_title.admin_order_field = 'title'
278 def documents_book_link(self, obj):
279 for book in obj.document_books.all():
280 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))
281 documents_book_link.short_description = _('Book')
283 def scans_source_link(self, obj):
286 '<a href="{url}" target="_blank">{url}</a>',
287 url=obj.scans_source,
291 scans_source_link.short_description = _('scans source')
294 admin.site.register(models.Book, BookAdmin)
297 admin.site.register(models.CollectionCategory)
300 class AuthorInline(admin.TabularInline):
301 model = models.Author.collections.through
302 autocomplete_fields = ["author"]
305 class BookInline(admin.TabularInline):
306 model = models.Book.collections.through
307 autocomplete_fields = ["book"]
310 class CollectionAdmin(admin.ModelAdmin):
311 list_display = ["name"]
312 autocomplete_fields = []
313 prepopulated_fields = {"slug": ("name",)}
314 search_fields = ["name"]
315 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
316 readonly_fields = ['estimated_costs']
317 inlines = [AuthorInline, BookInline]
319 def estimated_costs(self, obj):
325 for work_type, cost in obj.get_estimated_costs().items()
329 admin.site.register(models.Collection, CollectionAdmin)
333 class CategoryAdmin(admin.ModelAdmin):
334 search_fields = ["name"]
336 admin.site.register(models.Epoch, CategoryAdmin)
337 admin.site.register(models.Genre, CategoryAdmin)
338 admin.site.register(models.Kind, CategoryAdmin)
342 class WorkRateInline(admin.TabularInline):
343 model = models.WorkRate
344 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
347 class WorkTypeAdmin(admin.ModelAdmin):
348 inlines = [WorkRateInline]
350 admin.site.register(models.WorkType, WorkTypeAdmin)
354 @admin.register(models.Place)
355 class PlaceAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
356 search_fields = ['name']