You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.4 KiB
102 lines
3.4 KiB
from django.contrib import admin
|
|
from django import forms
|
|
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 unfold.widgets import UnfoldAdminTextInputWidget, UnfoldAdminTextareaWidget, UnfoldAdminExpandableTextareaWidget
|
|
from utils.multilang_json_widget import MultiLanguageJSONWidget
|
|
import json
|
|
|
|
from utils.admin import dovoodi_admin_site
|
|
from ..models import HadisSect, HadisCategory
|
|
|
|
|
|
# Custom Forms for JSON Fields
|
|
class HadisSectAdminForm(forms.ModelForm):
|
|
"""Custom form for HadisSect with JSON editor widgets"""
|
|
|
|
class Meta:
|
|
model = HadisSect
|
|
fields = '__all__'
|
|
widgets = {
|
|
'title': MultiLanguageJSONWidget(input_widget_class=UnfoldAdminTextInputWidget),
|
|
'description': MultiLanguageJSONWidget(input_widget_class=UnfoldAdminTextareaWidget),
|
|
}
|
|
|
|
|
|
class HadisCategoryAdminForm(forms.ModelForm):
|
|
"""Custom form for HadisCategory with JSON editor widgets"""
|
|
|
|
class Meta:
|
|
model = HadisCategory
|
|
fields = '__all__'
|
|
widgets = {
|
|
'title': MultiLanguageJSONWidget(input_widget_class=UnfoldAdminTextInputWidget),
|
|
'description': MultiLanguageJSONWidget(input_widget_class=UnfoldAdminTextareaWidget),
|
|
}
|
|
|
|
|
|
class HadisSectAdmin(ModelAdmin):
|
|
"""Admin for HadisSect model"""
|
|
form = HadisSectAdminForm
|
|
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','description')
|
|
}),
|
|
)
|
|
|
|
def display_title(self, obj):
|
|
"""Extracts text from the title JSON list"""
|
|
try:
|
|
return obj.title[0]['title']
|
|
except (IndexError, KeyError, TypeError, AttributeError):
|
|
return "No Title"
|
|
|
|
display_title.short_description = _('Title')
|
|
|
|
|
|
class HadisCategoryAdmin(ModelAdmin):
|
|
"""Admin for HadisCategory model with MPTT tree support"""
|
|
form = HadisCategoryAdminForm
|
|
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','description')
|
|
}),
|
|
(_('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]['title']
|
|
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
|
|
dovoodi_admin_site.register(HadisSect, HadisSectAdmin)
|
|
dovoodi_admin_site.register(HadisCategory, HadisCategoryAdmin)
|