Catalogue: nicer browsing of books with just notes.
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 4 Apr 2022 10:41:41 +0000 (12:41 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 4 Apr 2022 10:41:41 +0000 (12:41 +0200)
src/catalogue/admin.py
src/catalogue/models.py

index 3e80557..d417f74 100644 (file)
@@ -2,6 +2,8 @@
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 from django.contrib import admin
+from django.utils.html import escape
+from django.utils.safestring import mark_safe
 from django.utils.translation import gettext_lazy as _
 from admin_numeric_filter.admin import RangeNumericFilter, NumericFilterModelAdmin
 from fnpdjango.actions import export_as_csv_action
@@ -32,7 +34,7 @@ admin.site.register(models.Author, AuthorAdmin)
 
 class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
     list_display = [
-        "title",
+        "smart_title",
         "authors_str",
         "translators_str",
         "language",
@@ -120,6 +122,20 @@ class BookAdmin(WikidataAdminMixin, NumericFilterModelAdmin):
             for work_type, cost in obj.get_estimated_costs().items()
         )
 
+    def smart_title(self, obj):
+        if obj.title:
+            return obj.title
+        if obj.notes:
+            n = obj.notes
+            if len(n) > 100:
+                n = n[:100] + '…'
+            return mark_safe(
+                '<em><small>' + escape(n) + '</small></em>'
+            )
+        return '---'
+    smart_title.short_description = _('Title')
+    smart_title.admin_order_field = 'title'
+    
 
 admin.site.register(models.Book, BookAdmin)
 
index 45250bc..75726a4 100644 (file)
@@ -164,10 +164,12 @@ class Book(WikidataMixin, models.Model):
     def authors_str(self):
         return ", ".join(str(author) for author in self.authors.all())
     authors_str.admin_order_field = 'authors__last_name'
+    authors_str.short_description = _('Author')
 
     def translators_str(self):
         return ", ".join(str(author) for author in self.translators.all())
     translators_str.admin_order_field = 'translators__last_name'
+    translators_str.short_description = _('Translator')
 
     def get_estimated_costs(self):
         return {