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"),
75 prepopulated_fields = {"slug": ("first_name", "last_name")}
76 autocomplete_fields = ["collections", "place_of_birth", "place_of_death"]
81 def description_preview(self, obj):
82 return obj.generate_description()
85 admin.site.register(models.Author, AuthorAdmin)
88 class LicenseFilter(admin.SimpleListFilter):
90 parameter_name = 'book_license'
91 license_url_field = 'document_book__dc__license'
92 license_name_field = 'document_book__dc__license_description'
94 def lookups(self, requesrt, model_admin):
98 ('pd', 'domena publiczna'),
101 def queryset(self, request, queryset):
104 return queryset.filter(**{
105 self.license_url_field + '__icontains': 'creativecommons.org'
108 return queryset.filter(**{
109 self.license_url_field + '__icontains': 'artlibre.org'
112 return queryset.filter(**{
113 self.license_name_field + '__icontains': 'domena publiczna'
119 class CoverLicenseFilter(LicenseFilter):
120 title = 'Licencja okładki'
121 parameter_name = 'cover_license'
122 license_url_field = 'document_book__dc_cover_image__license_url'
123 license_name_field = 'document_book__dc_cover_image__license_name'
126 def add_title(base_class, suffix):
127 class TitledCategoryFilter(base_class):
128 def __init__(self, *args, **kwargs):
129 super().__init__(*args, **kwargs)
131 return TitledCategoryFilter
135 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
147 "authors__first_name", "authors__last_name",
148 "translators__first_name", "translators__last_name",
149 "scans_source", "text_source", "notes", "estimate_source",
151 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
152 prepopulated_fields = {"slug": ("title",)}
155 "based_on__language",
156 ("pd_year", RangeNumericFilter),
158 "collections__category",
159 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
160 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
161 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
162 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
163 "epochs", "kinds", "genres",
165 "authors__gender", "authors__nationality",
166 "translators__gender", "translators__nationality",
167 "document_book__chunk__stage",
174 list_per_page = 1000000
179 "documents_book_link",
182 actions = [export_as_csv_action(
188 "authors_str", # authors?
189 "translators_str", # translators?
204 (None, {"fields": [("wikidata", "wikidata_link")]}),
210 ("slug", 'documents_book_link'),
234 ("free_license", "polona_missing"),
235 ("scans_source", "scans_source_link"),
240 ("estimated_chars", "estimated_verses", "estimate_source"),
247 def get_queryset(self, request):
248 qs = super().get_queryset(request)
249 if request.resolver_match.view_name.endswith("changelist"):
250 qs = qs.prefetch_related("authors", "translators")
253 def estimated_costs(self, obj):
259 for work_type, cost in obj.get_estimated_costs().items()
262 def smart_title(self, obj):
270 '<em><small>' + escape(n) + '</small></em>'
273 smart_title.short_description = _('Title')
274 smart_title.admin_order_field = 'title'
276 def documents_book_link(self, obj):
277 for book in obj.document_books.all():
278 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))
279 documents_book_link.short_description = _('Book')
281 def scans_source_link(self, obj):
284 '<a href="{url}" target="_blank">{url}</a>',
285 url=obj.scans_source,
289 scans_source_link.short_description = _('scans source')
292 admin.site.register(models.Book, BookAdmin)
295 admin.site.register(models.CollectionCategory)
298 class AuthorInline(admin.TabularInline):
299 model = models.Author.collections.through
300 autocomplete_fields = ["author"]
303 class BookInline(admin.TabularInline):
304 model = models.Book.collections.through
305 autocomplete_fields = ["book"]
308 class CollectionAdmin(admin.ModelAdmin):
309 list_display = ["name"]
310 autocomplete_fields = []
311 prepopulated_fields = {"slug": ("name",)}
312 search_fields = ["name"]
313 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
314 readonly_fields = ['estimated_costs']
315 inlines = [AuthorInline, BookInline]
317 def estimated_costs(self, obj):
323 for work_type, cost in obj.get_estimated_costs().items()
327 admin.site.register(models.Collection, CollectionAdmin)
331 class CategoryAdmin(admin.ModelAdmin):
332 search_fields = ["name"]
334 admin.site.register(models.Epoch, CategoryAdmin)
335 admin.site.register(models.Genre, CategoryAdmin)
336 admin.site.register(models.Kind, CategoryAdmin)
340 class WorkRateInline(admin.TabularInline):
341 model = models.WorkRate
342 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
345 class WorkTypeAdmin(admin.ModelAdmin):
346 inlines = [WorkRateInline]
348 admin.site.register(models.WorkType, WorkTypeAdmin)
352 @admin.register(models.Place)
353 class PlaceAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
354 search_fields = ['name']