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
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 fnpdjango.actions import export_as_csv_action
11 import documents.models
12 from .wikidata import WikidataAdminMixin
15 class AuthorAdmin(WikidataAdminMixin, admin.ModelAdmin):
27 list_filter = ["year_of_death", "priority", "collections", "status", "gender", "nationality"]
28 list_per_page = 10000000
29 search_fields = ["first_name", "last_name", "wikidata"]
30 prepopulated_fields = {"slug": ("first_name", "last_name")}
31 autocomplete_fields = ["collections"]
34 admin.site.register(models.Author, AuthorAdmin)
37 class LicenseFilter(admin.SimpleListFilter):
39 parameter_name = 'book_license'
40 license_url_field = 'document_book__dc__license'
41 license_name_field = 'document_book__dc__license_description'
43 def lookups(self, requesrt, model_admin):
47 ('pd', 'domena publiczna'),
50 def queryset(self, request, queryset):
53 return queryset.filter(**{
54 self.license_url_field + '__icontains': 'creativecommons.org'
57 return queryset.filter(**{
58 self.license_url_field + '__icontains': 'artlibre.org'
61 return queryset.filter(**{
62 self.license_name_field + '__icontains': 'domena publiczna'
68 class CoverLicenseFilter(LicenseFilter):
69 title = 'Licencja okładki'
70 parameter_name = 'cover_license'
71 license_url_field = 'document_book__dc_cover_image__license_url'
72 license_name_field = 'document_book__dc_cover_image__license_name'
75 def add_title(base_class, suffix):
76 class TitledCategoryFilter(base_class):
77 def __init__(self, *args, **kwargs):
78 super().__init__(*args, **kwargs)
80 return TitledCategoryFilter
84 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
96 "authors__first_name", "authors__last_name",
97 "translators__first_name", "translators__last_name",
98 "scans_source", "text_source", "notes", "estimate_source",
100 autocomplete_fields = ["authors", "translators", "based_on", "collections", "epochs", "genres", "kinds"]
101 prepopulated_fields = {"slug": ("title",)}
104 "based_on__language",
105 ("pd_year", RangeNumericFilter),
107 "collections__category",
108 ("authors__collections", add_title(admin.RelatedFieldListFilter, ' autora')),
109 ("authors__collections__category", add_title(admin.RelatedFieldListFilter, ' autora')),
110 ("translators__collections", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
111 ("translators__collections__category", add_title(admin.RelatedFieldListFilter, ' tłumacza')),
112 "epochs", "kinds", "genres",
114 "authors__gender", "authors__nationality",
115 "translators__gender", "translators__nationality",
116 "document_book__chunk__stage",
117 #"document_book__chunk__user",
122 list_per_page = 1000000
124 readonly_fields = ["wikidata_link", "estimated_costs", "documents_book_link"]
125 actions = [export_as_csv_action()]
127 (None, {"fields": [("wikidata", "wikidata_link")]}),
133 ("slug", 'documents_book_link'),
161 ("estimated_chars", "estimated_verses", "estimate_source"),
168 def get_queryset(self, request):
169 qs = super().get_queryset(request)
170 if request.resolver_match.view_name.endswith("changelist"):
171 qs = qs.prefetch_related("authors", "translators")
174 def estimated_costs(self, obj):
180 for work_type, cost in obj.get_estimated_costs().items()
183 def smart_title(self, obj):
191 '<em><small>' + escape(n) + '</small></em>'
194 smart_title.short_description = _('Title')
195 smart_title.admin_order_field = 'title'
197 def documents_book_link(self, obj):
198 for book in obj.document_books.all():
199 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))
200 documents_book_link.short_description = _('Book')
203 admin.site.register(models.Book, BookAdmin)
206 admin.site.register(models.CollectionCategory)
209 class AuthorInline(admin.TabularInline):
210 model = models.Author.collections.through
211 autocomplete_fields = ["author"]
214 class BookInline(admin.TabularInline):
215 model = models.Book.collections.through
216 autocomplete_fields = ["book"]
219 class CollectionAdmin(admin.ModelAdmin):
220 list_display = ["name"]
221 autocomplete_fields = []
222 prepopulated_fields = {"slug": ("name",)}
223 search_fields = ["name"]
224 fields = ['name', 'slug', 'category', 'notes', 'estimated_costs']
225 readonly_fields = ['estimated_costs']
226 inlines = [AuthorInline, BookInline]
228 def estimated_costs(self, obj):
234 for work_type, cost in obj.get_estimated_costs().items()
238 admin.site.register(models.Collection, CollectionAdmin)
242 class CategoryAdmin(admin.ModelAdmin):
243 search_fields = ["name"]
245 admin.site.register(models.Epoch, CategoryAdmin)
246 admin.site.register(models.Genre, CategoryAdmin)
247 admin.site.register(models.Kind, CategoryAdmin)
251 class WorkRateInline(admin.TabularInline):
252 model = models.WorkRate
253 autocomplete_fields = ['kinds', 'genres', 'epochs', 'collections']
256 class WorkTypeAdmin(admin.ModelAdmin):
257 inlines = [WorkRateInline]
259 admin.site.register(models.WorkType, WorkTypeAdmin)