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"),
61 ("date_of_birth", "year_of_birth", "year_of_birth_inexact", "year_of_birth_range", "place_of_birth"),
62 ("date_of_death", "year_of_death", "year_of_death_inexact", "year_of_death_range", "place_of_death"),
63 ("description", "description_preview"),
72 "photo", "photo_source", "photo_attribution",
78 prepopulated_fields = {"slug": ("first_name", "last_name")}
79 autocomplete_fields = ["collections", "place_of_birth", "place_of_death"]
84 def description_preview(self, obj):
85 return obj.generate_description()
88 admin.site.register(models.Author, AuthorAdmin)
91 class LicenseFilter(admin.SimpleListFilter):
93 parameter_name = 'book_license'
94 license_url_field = 'document_book__dc__license'
95 license_name_field = 'document_book__dc__license_description'
97 def lookups(self, requesrt, model_admin):
101 ('pd', 'domena publiczna'),
104 def queryset(self, request, queryset):
107 return queryset.filter(**{
108 self.license_url_field + '__icontains': 'creativecommons.org'
111 return queryset.filter(**{
112 self.license_url_field + '__icontains': 'artlibre.org'
115 return queryset.filter(**{
116 self.license_name_field + '__icontains': 'domena publiczna'
122 class CoverLicenseFilter(LicenseFilter):
123 title = 'Licencja okładki'
124 parameter_name = 'cover_license'
125 license_url_field = 'document_book__dc_cover_image__license_url'
126 license_name_field = 'document_book__dc_cover_image__license_name'
129 def add_title(base_class, suffix):
130 class TitledCategoryFilter(base_class):
131 def __init__(self, *args, **kwargs):
132 super().__init__(*args, **kwargs)
134 return TitledCategoryFilter
138 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
150 "authors__first_name", "authors__last_name",
151 "translators__first_name", "translators__last_name",
152 "scans_source", "text_source", "notes", "estimate_source",
154 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
155 prepopulated_fields = {"slug": ("title",)}
158 "based_on__language",
159 ("pd_year", RangeNumericFilter),
161 "collections__category",
162 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
163 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
164 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
165 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
166 "epochs", "kinds", "genres",
168 "authors__gender", "authors__nationality",
169 "translators__gender", "translators__nationality",
170 "document_book__chunk__stage",
177 list_per_page = 1000000
182 "documents_book_link",
185 actions = [export_as_csv_action(
191 "authors_first_names",
192 "authors_last_names",
193 "translators_first_names",
194 "translators_last_names",
209 (None, {"fields": [("wikidata", "wikidata_link")]}),
215 ("slug", 'documents_book_link'),
239 ("free_license", "polona_missing"),
240 ("scans_source", "scans_source_link"),
245 ("estimated_chars", "estimated_verses", "estimate_source"),
252 def get_queryset(self, request):
253 qs = super().get_queryset(request)
254 if request.resolver_match.view_name.endswith("changelist"):
255 qs = qs.prefetch_related("authors", "translators")
258 def estimated_costs(self, obj):
264 for work_type, cost in obj.get_estimated_costs().items()
267 def smart_title(self, obj):
275 '<em><small>' + escape(n) + '</small></em>'
278 smart_title.short_description = _('Title')
279 smart_title.admin_order_field = 'title'
281 def documents_book_link(self, obj):
282 for book in obj.document_books.all():
283 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))
284 documents_book_link.short_description = _('Book')
286 def scans_source_link(self, obj):
289 '<a href="{url}" target="_blank">{url}</a>',
290 url=obj.scans_source,
294 scans_source_link.short_description = _('scans source')
297 admin.site.register(models.Book, BookAdmin)
300 admin.site.register(models.CollectionCategory)
303 class AuthorInline(admin.TabularInline):
304 model = models.Author.collections.through
305 autocomplete_fields = ["author"]
308 class BookInline(admin.TabularInline):
309 model = models.Book.collections.through
310 autocomplete_fields = ["book"]
313 class CollectionAdmin(admin.ModelAdmin):
314 list_display = ["name"]
315 autocomplete_fields = []
316 prepopulated_fields = {"slug": ("name",)}
317 search_fields = ["name"]
318 fields = ['name', 'slug', 'category', 'description', 'notes', 'estimated_costs']
319 readonly_fields = ['estimated_costs']
320 inlines = [AuthorInline, BookInline]
322 def estimated_costs(self, obj):
328 for work_type, cost in obj.get_estimated_costs().items()
332 admin.site.register(models.Collection, CollectionAdmin)
336 class CategoryAdmin(admin.ModelAdmin):
337 search_fields = ["name"]
340 @admin.register(models.Epoch)
341 class EpochAdmin(CategoryAdmin):
342 list_display = ['name', 'adjective_feminine_singular', 'adjective_nonmasculine_plural']
345 @admin.register(models.Genre)
346 class GenreAdmin(CategoryAdmin):
347 list_display = ['name', 'plural', 'is_epoch_specific']
350 @admin.register(models.Kind)
351 class KindAdmin(CategoryAdmin):
352 list_display = ['name', 'collective_noun']
356 class WorkRateInline(admin.TabularInline):
357 model = models.WorkRate
358 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
361 class WorkTypeAdmin(admin.ModelAdmin):
362 inlines = [WorkRateInline]
364 admin.site.register(models.WorkType, WorkTypeAdmin)
368 @admin.register(models.Place)
369 class PlaceAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
370 search_fields = ['name']