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"]
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"),
75 prepopulated_fields = {"slug": ("first_name", "last_name")}
76 autocomplete_fields = ["collections", "place_of_birth", "place_of_death"]
82 admin.site.register(models.Author, AuthorAdmin)
85 class LicenseFilter(admin.SimpleListFilter):
87 parameter_name = 'book_license'
88 license_url_field = 'document_book__dc__license'
89 license_name_field = 'document_book__dc__license_description'
91 def lookups(self, requesrt, model_admin):
95 ('pd', 'domena publiczna'),
98 def queryset(self, request, queryset):
101 return queryset.filter(**{
102 self.license_url_field + '__icontains': 'creativecommons.org'
105 return queryset.filter(**{
106 self.license_url_field + '__icontains': 'artlibre.org'
109 return queryset.filter(**{
110 self.license_name_field + '__icontains': 'domena publiczna'
116 class CoverLicenseFilter(LicenseFilter):
117 title = 'Licencja okładki'
118 parameter_name = 'cover_license'
119 license_url_field = 'document_book__dc_cover_image__license_url'
120 license_name_field = 'document_book__dc_cover_image__license_name'
123 def add_title(base_class, suffix):
124 class TitledCategoryFilter(base_class):
125 def __init__(self, *args, **kwargs):
126 super().__init__(*args, **kwargs)
128 return TitledCategoryFilter
132 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
144 "authors__first_name", "authors__last_name",
145 "translators__first_name", "translators__last_name",
146 "scans_source", "text_source", "notes", "estimate_source",
148 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
149 prepopulated_fields = {"slug": ("title",)}
152 "based_on__language",
153 ("pd_year", RangeNumericFilter),
155 "collections__category",
156 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
157 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
158 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
159 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
160 "epochs", "kinds", "genres",
162 "authors__gender", "authors__nationality",
163 "translators__gender", "translators__nationality",
164 "document_book__chunk__stage",
171 list_per_page = 1000000
176 "documents_book_link",
179 actions = [export_as_csv_action(
185 "authors_str", # authors?
186 "translators_str", # translators?
201 (None, {"fields": [("wikidata", "wikidata_link")]}),
207 ("slug", 'documents_book_link'),
230 ("free_license", "polona_missing"),
231 ("scans_source", "scans_source_link"),
236 ("estimated_chars", "estimated_verses", "estimate_source"),
243 def get_queryset(self, request):
244 qs = super().get_queryset(request)
245 if request.resolver_match.view_name.endswith("changelist"):
246 qs = qs.prefetch_related("authors", "translators")
249 def estimated_costs(self, obj):
255 for work_type, cost in obj.get_estimated_costs().items()
258 def smart_title(self, obj):
266 '<em><small>' + escape(n) + '</small></em>'
269 smart_title.short_description = _('Title')
270 smart_title.admin_order_field = 'title'
272 def documents_book_link(self, obj):
273 for book in obj.document_books.all():
274 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))
275 documents_book_link.short_description = _('Book')
277 def scans_source_link(self, obj):
280 '<a href="{url}" target="_blank">{url}</a>',
281 url=obj.scans_source,
285 scans_source_link.short_description = _('scans source')
288 admin.site.register(models.Book, BookAdmin)
291 admin.site.register(models.CollectionCategory)
294 class AuthorInline(admin.TabularInline):
295 model = models.Author.collections.through
296 autocomplete_fields = ["author"]
299 class BookInline(admin.TabularInline):
300 model = models.Book.collections.through
301 autocomplete_fields = ["book"]
304 class CollectionAdmin(admin.ModelAdmin):
305 list_display = ["name"]
306 autocomplete_fields = []
307 prepopulated_fields = {"slug": ("name",)}
308 search_fields = ["name"]
309 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
310 readonly_fields = ['estimated_costs']
311 inlines = [AuthorInline, BookInline]
313 def estimated_costs(self, obj):
319 for work_type, cost in obj.get_estimated_costs().items()
323 admin.site.register(models.Collection, CollectionAdmin)
327 class CategoryAdmin(admin.ModelAdmin):
328 search_fields = ["name"]
330 admin.site.register(models.Epoch, CategoryAdmin)
331 admin.site.register(models.Genre, CategoryAdmin)
332 admin.site.register(models.Kind, CategoryAdmin)
336 class WorkRateInline(admin.TabularInline):
337 model = models.WorkRate
338 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
341 class WorkTypeAdmin(admin.ModelAdmin):
342 inlines = [WorkRateInline]
344 admin.site.register(models.WorkType, WorkTypeAdmin)
348 @admin.register(models.Place)
349 class PlaceAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
350 search_fields = ['name']