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 raw_id_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 prepopulated_fields = {"slug": ("first_name", "last_name")}
49 autocomplete_fields = ["collections"]
55 admin.site.register(models.Author, AuthorAdmin)
58 class LicenseFilter(admin.SimpleListFilter):
60 parameter_name = 'book_license'
61 license_url_field = 'document_book__dc__license'
62 license_name_field = 'document_book__dc__license_description'
64 def lookups(self, requesrt, model_admin):
68 ('pd', 'domena publiczna'),
71 def queryset(self, request, queryset):
74 return queryset.filter(**{
75 self.license_url_field + '__icontains': 'creativecommons.org'
78 return queryset.filter(**{
79 self.license_url_field + '__icontains': 'artlibre.org'
82 return queryset.filter(**{
83 self.license_name_field + '__icontains': 'domena publiczna'
89 class CoverLicenseFilter(LicenseFilter):
90 title = 'Licencja okładki'
91 parameter_name = 'cover_license'
92 license_url_field = 'document_book__dc_cover_image__license_url'
93 license_name_field = 'document_book__dc_cover_image__license_name'
96 def add_title(base_class, suffix):
97 class TitledCategoryFilter(base_class):
98 def __init__(self, *args, **kwargs):
99 super().__init__(*args, **kwargs)
101 return TitledCategoryFilter
105 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
117 "authors__first_name", "authors__last_name",
118 "translators__first_name", "translators__last_name",
119 "scans_source", "text_source", "notes", "estimate_source",
121 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
122 prepopulated_fields = {"slug": ("title",)}
125 "based_on__language",
126 ("pd_year", RangeNumericFilter),
128 "collections__category",
129 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
130 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
131 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
132 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
133 "epochs", "kinds", "genres",
135 "authors__gender", "authors__nationality",
136 "translators__gender", "translators__nationality",
137 "document_book__chunk__stage",
144 list_per_page = 1000000
149 "documents_book_link",
152 actions = [export_as_csv_action(
158 "authors_str", # authors?
159 "translators_str", # translators?
174 (None, {"fields": [("wikidata", "wikidata_link")]}),
180 ("slug", 'documents_book_link'),
203 ("free_license", "polona_missing"),
204 ("scans_source", "scans_source_link"),
209 ("estimated_chars", "estimated_verses", "estimate_source"),
216 def get_queryset(self, request):
217 qs = super().get_queryset(request)
218 if request.resolver_match.view_name.endswith("changelist"):
219 qs = qs.prefetch_related("authors", "translators")
222 def estimated_costs(self, obj):
228 for work_type, cost in obj.get_estimated_costs().items()
231 def smart_title(self, obj):
239 '<em><small>' + escape(n) + '</small></em>'
242 smart_title.short_description = _('Title')
243 smart_title.admin_order_field = 'title'
245 def documents_book_link(self, obj):
246 for book in obj.document_books.all():
247 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))
248 documents_book_link.short_description = _('Book')
250 def scans_source_link(self, obj):
253 '<a href="{url}" target="_blank">{url}</a>',
254 url=obj.scans_source,
258 scans_source_link.short_description = _('scans source')
261 admin.site.register(models.Book, BookAdmin)
264 admin.site.register(models.CollectionCategory)
267 class AuthorInline(admin.TabularInline):
268 model = models.Author.collections.through
269 autocomplete_fields = ["author"]
272 class BookInline(admin.TabularInline):
273 model = models.Book.collections.through
274 autocomplete_fields = ["book"]
277 class CollectionAdmin(admin.ModelAdmin):
278 list_display = ["name"]
279 autocomplete_fields = []
280 prepopulated_fields = {"slug": ("name",)}
281 search_fields = ["name"]
282 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
283 readonly_fields = ['estimated_costs']
284 inlines = [AuthorInline, BookInline]
286 def estimated_costs(self, obj):
292 for work_type, cost in obj.get_estimated_costs().items()
296 admin.site.register(models.Collection, CollectionAdmin)
300 class CategoryAdmin(admin.ModelAdmin):
301 search_fields = ["name"]
303 admin.site.register(models.Epoch, CategoryAdmin)
304 admin.site.register(models.Genre, CategoryAdmin)
305 admin.site.register(models.Kind, CategoryAdmin)
309 class WorkRateInline(admin.TabularInline):
310 model = models.WorkRate
311 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
314 class WorkTypeAdmin(admin.ModelAdmin):
315 inlines = [WorkRateInline]
317 admin.site.register(models.WorkType, WorkTypeAdmin)