diff --git a/.gitignore b/.gitignore index db5816e..f19cdcf 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ include/ keys/ .venv/ .idea/ - *.kate-swp node_modules/ package-lock.json @@ -16,3 +15,4 @@ package.json # Diagram cache directory media/diagram_cache/ .env +data/db.sqlite3 diff --git a/dokumente/admin.py b/dokumente/admin.py index 82331ae..4aee2c9 100644 --- a/dokumente/admin.py +++ b/dokumente/admin.py @@ -2,6 +2,7 @@ from django.contrib import admin #from nested_inline.admin import NestedStackedInline, NestedModelAdmin from nested_admin import NestedStackedInline, NestedModelAdmin, NestedTabularInline from django import forms +from django.utils.html import format_html from mptt.forms import TreeNodeMultipleChoiceField from mptt.admin import DraggableMPTTAdmin from adminsortable2.admin import SortableInlineAdminMixin, SortableAdminBase @@ -132,9 +133,57 @@ class StichworterklaerungInline(NestedTabularInline): @admin.register(Stichwort) class StichwortAdmin(NestedModelAdmin): + list_display = ('stichwort', 'vorgaben_count') search_fields = ('stichwort',) ordering=('stichwort',) inlines=[StichworterklaerungInline] + readonly_fields = ('vorgaben_list',) + fieldsets = ( + (None, { + 'fields': ('stichwort', 'vorgaben_list') + }), + ) + + def vorgaben_count(self, obj): + """Count the number of Vorgaben that have this Stichwort""" + count = obj.vorgabe_set.count() + return f"{count} Vorgabe{'n' if count != 1 else ''}" + vorgaben_count.short_description = "Anzahl Vorgaben" + + def vorgaben_list(self, obj): + """Display list of Vorgaben that use this Stichwort""" + vorgaben = obj.vorgabe_set.select_related('dokument', 'thema').order_by('dokument__nummer', 'nummer') + vorgaben_list = list(vorgaben) # Evaluate queryset once + count = len(vorgaben_list) + + if count == 0: + return format_html("Keine Vorgaben gefunden

Gesamt: 0 Vorgaben

") + + html = "
" + html += "" + html += "" + html += "" + html += "" + html += "" + html += "" + html += "" + + for vorgabe in vorgaben_list: + html += "" + html += f"" + html += f"" + html += f"" + html += "" + + html += "
VorgabeTitelDokument
{vorgabe.Vorgabennummer()}{vorgabe.titel}{vorgabe.dokument.nummer} – {vorgabe.dokument.name}
" + html += f"

Gesamt: {count} Vorgabe{'n' if count != 1 else ''}

" + + return format_html(html) + vorgaben_list.short_description = "Zugeordnete Vorgaben" + + def get_queryset(self, request): + """Optimize queryset with related data""" + return super().get_queryset(request).prefetch_related('vorgabe_set') @admin.register(Person) class PersonAdmin(admin.ModelAdmin):