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),
47 ("genitive", admin.EmptyFieldListFilter)
49 list_per_page = 10000000
50 search_fields = ["first_name", "last_name", "wikidata"]
51 readonly_fields = ["wikidata_link", "description_preview"]
54 (None, {"fields": [("wikidata", "wikidata_link")]}),
59 ("first_name", "last_name"),
64 ("date_of_birth", "year_of_birth", "year_of_birth_inexact", "year_of_birth_range", "place_of_birth"),
65 ("date_of_death", "year_of_death", "year_of_death_inexact", "year_of_death_range", "place_of_death"),
66 ("description", "description_preview"),
75 "photo", "photo_source", "photo_attribution",
81 prepopulated_fields = {"slug": ("first_name", "last_name")}
82 autocomplete_fields = ["collections", "place_of_birth", "place_of_death"]
87 def description_preview(self, obj):
88 return obj.generate_description()
91 admin.site.register(models.Author, AuthorAdmin)
94 class LicenseFilter(admin.SimpleListFilter):
96 parameter_name = 'book_license'
97 license_url_field = 'document_book__dc__license'
98 license_name_field = 'document_book__dc__license_description'
100 def lookups(self, requesrt, model_admin):
104 ('pd', 'domena publiczna'),
107 def queryset(self, request, queryset):
110 return queryset.filter(**{
111 self.license_url_field + '__icontains': 'creativecommons.org'
114 return queryset.filter(**{
115 self.license_url_field + '__icontains': 'artlibre.org'
118 return queryset.filter(**{
119 self.license_name_field + '__icontains': 'domena publiczna'
125 class CoverLicenseFilter(LicenseFilter):
126 title = 'Licencja okładki'
127 parameter_name = 'cover_license'
128 license_url_field = 'document_book__dc_cover_image__license_url'
129 license_name_field = 'document_book__dc_cover_image__license_name'
132 def add_title(base_class, suffix):
133 class TitledCategoryFilter(base_class):
134 def __init__(self, *args, **kwargs):
135 super().__init__(*args, **kwargs)
137 return TitledCategoryFilter
141 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
153 "authors__first_name", "authors__last_name",
154 "translators__first_name", "translators__last_name",
155 "scans_source", "text_source", "notes", "estimate_source",
157 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
158 prepopulated_fields = {"slug": ("title",)}
161 "based_on__language",
162 ("pd_year", RangeNumericFilter),
164 "collections__category",
165 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
166 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
167 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
168 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
169 "epochs", "kinds", "genres",
171 "authors__gender", "authors__nationality",
172 "translators__gender", "translators__nationality",
174 ("authors__place_of_birth", add_title(admin.RelatedFieldListFilter, ' autora')),
175 ("authors__place_of_death", add_title(admin.RelatedFieldListFilter, ' autora')),
176 ("translators__place_of_birth", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
177 ("translators__place_of_death", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
179 "document_book__chunk__stage",
186 list_per_page = 1000000
191 "documents_book_link",
193 "monthly_views_page",
194 "monthly_views_reader",
196 actions = [export_as_csv_action(
202 "authors_first_names",
203 "authors_last_names",
204 "translators_first_names",
205 "translators_last_names",
220 (None, {"fields": [("wikidata", "wikidata_link")]}),
226 ("slug", 'documents_book_link'),
250 ("free_license", "polona_missing"),
251 ("scans_source", "scans_source_link"),
256 ("estimated_chars", "estimated_verses", "estimate_source"),
258 ("monthly_views_page", "monthly_views_reader"),
264 def get_queryset(self, request):
265 qs = super().get_queryset(request)
266 if request.resolver_match.view_name.endswith("changelist"):
267 qs = qs.prefetch_related("authors", "translators")
270 def estimated_costs(self, obj):
276 for work_type, cost in obj.get_estimated_costs().items()
279 def smart_title(self, obj):
287 '<em><small>' + escape(n) + '</small></em>'
290 smart_title.short_description = _('Title')
291 smart_title.admin_order_field = 'title'
293 def documents_book_link(self, obj):
294 for book in obj.document_books.all():
295 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))
296 documents_book_link.short_description = _('Book')
298 def scans_source_link(self, obj):
301 '<a href="{url}" target="_blank">{url}</a>',
302 url=obj.scans_source,
306 scans_source_link.short_description = _('scans source')
309 admin.site.register(models.Book, BookAdmin)
312 admin.site.register(models.CollectionCategory)
315 class AuthorInline(admin.TabularInline):
316 model = models.Author.collections.through
317 autocomplete_fields = ["author"]
320 class BookInline(admin.TabularInline):
321 model = models.Book.collections.through
322 autocomplete_fields = ["book"]
325 class CollectionAdmin(admin.ModelAdmin):
326 list_display = ["name"]
327 autocomplete_fields = []
328 prepopulated_fields = {"slug": ("name",)}
329 search_fields = ["name"]
330 fields = ['name', 'slug', 'category', 'description', 'notes', 'estimated_costs']
331 readonly_fields = ['estimated_costs']
332 inlines = [AuthorInline, BookInline]
334 def estimated_costs(self, obj):
340 for work_type, cost in obj.get_estimated_costs().items()
344 admin.site.register(models.Collection, CollectionAdmin)
348 class CategoryAdmin(admin.ModelAdmin):
349 search_fields = ["name"]
352 @admin.register(models.Epoch)
353 class EpochAdmin(CategoryAdmin):
354 list_display = ['name', 'adjective_feminine_singular', 'adjective_nonmasculine_plural']
357 @admin.register(models.Genre)
358 class GenreAdmin(CategoryAdmin):
359 list_display = ['name', 'plural', 'is_epoch_specific']
362 @admin.register(models.Kind)
363 class KindAdmin(CategoryAdmin):
364 list_display = ['name', 'collective_noun']
368 class WorkRateInline(admin.TabularInline):
369 model = models.WorkRate
370 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
373 class WorkTypeAdmin(admin.ModelAdmin):
374 inlines = [WorkRateInline]
376 admin.site.register(models.WorkType, WorkTypeAdmin)
380 @admin.register(models.Place)
381 class PlaceAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
382 search_fields = ['name']