from django.contrib import admin from django.utils.translation import gettext_lazy as _ from django.utils.html import format_html from unfold.admin import ModelAdmin from unfold.decorators import display, action from mptt.admin import DraggableMPTTAdmin from utils.admin import project_admin_site from ..models import HadisSect, HadisCategory class HadisSectAdmin(ModelAdmin): """Admin for HadisSect model""" list_display = ('sect_type', 'display_title', 'is_active', 'order') list_filter = ('sect_type', 'is_active') search_fields = ('title',) ordering = ('order',) fieldsets = ( (None, { 'fields': ('sect_type', 'title', 'is_active', 'order') }), ) def display_title(self, obj): """Extracts text from the title JSON list""" try: return obj.title[0]['text'] except (IndexError, KeyError, TypeError, AttributeError): return "No Title" display_title.short_description = _('Title') class HadisCategoryAdmin(DraggableMPTTAdmin, ModelAdmin): """Admin for HadisCategory model with MPTT tree support""" list_display = ('indented_title', 'sect', 'source_type', 'order') list_display_links = ('indented_title',) list_filter = ('sect', 'source_type') search_fields = ('title',) ordering = ('tree_id', 'lft') fieldsets = ( (None, { 'fields': ('parent', 'sect', 'source_type', 'title', 'order') }), (_('Files'), { 'fields': ('xmind_file',), 'classes': ('collapse',) }), ) def indented_title(self, instance): """Display indented title for tree structure using JSON text""" try: # Extract text from the first element of the title list title_text = instance.title[0]['text'] except (IndexError, KeyError, TypeError, AttributeError): title_text = "No Title" # DraggableMPTTAdmin works best if you don't mess with the HTML too much, # but here is your requested dash indentation style combined with clean text: return f"{'—' * instance.level} {title_text}" indented_title.short_description = _('Title') # Register models with the custom admin site project_admin_site.register(HadisSect, HadisSectAdmin) project_admin_site.register(HadisCategory, HadisCategoryAdmin)