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.
 
 

221 lines
7.2 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 utils.json_editor_field import JsonEditorWidget
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__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Schema for title JSON field
title_schema = {
"type": "array",
"title": "Titles",
"items": {
"type": "object",
"title": "Title",
"properties": {
"language_code": {
"type": "string",
"title": "Language Code",
"enum": ["en", "fa", "ar", "ur", "ru"],
"options": {
"enum_titles": ["English", "Persian", "Arabic", "Urdu", "Russian"]
}
},
"text": {
"type": "string",
"title": "Title Text"
}
},
"required": ["language_code", "text"]
}
}
# Schema for description JSON field
description_schema = {
"type": "array",
"title": "Descriptions",
"items": {
"type": "object",
"title": "Description",
"properties": {
"language_code": {
"type": "string",
"title": "Language Code",
"enum": ["en", "fa", "ar", "ur", "ru"],
"options": {
"enum_titles": ["English", "Persian", "Arabic", "Urdu", "Russian"]
}
},
"text": {
"type": "string",
"title": "Description Text"
}
},
"required": ["language_code", "text"]
}
}
# Apply JSON editor widgets
self.fields['title'].widget = JsonEditorWidget(attrs={
'schema': json.dumps(title_schema),
'title': 'Titles'
})
self.fields['description'].widget = JsonEditorWidget(attrs={
'schema': json.dumps(description_schema),
'title': 'Descriptions'
})
class HadisCategoryAdminForm(forms.ModelForm):
"""Custom form for HadisCategory with JSON editor widgets"""
class Meta:
model = HadisCategory
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Schema for title JSON field
title_schema = {
"type": "array",
"title": "Titles",
"items": {
"type": "object",
"title": "Title",
"properties": {
"language_code": {
"type": "string",
"title": "Language Code",
"enum": ["en", "fa", "ar", "ur", "ru"],
"options": {
"enum_titles": ["English", "Persian", "Arabic", "Urdu", "Russian"]
}
},
"text": {
"type": "string",
"title": "Title Text"
}
},
"required": ["language_code", "text"]
}
}
# Schema for description JSON field
description_schema = {
"type": "array",
"title": "Descriptions",
"items": {
"type": "object",
"title": "Description",
"properties": {
"language_code": {
"type": "string",
"title": "Language Code",
"enum": ["en", "fa", "ar", "ur", "ru"],
"options": {
"enum_titles": ["English", "Persian", "Arabic", "Urdu", "Russian"]
}
},
"text": {
"type": "string",
"title": "Description Text"
}
},
"required": ["language_code", "text"]
}
}
# Apply JSON editor widgets
self.fields['title'].widget = JsonEditorWidget(attrs={
'schema': json.dumps(title_schema),
'title': 'Titles'
})
self.fields['description'].widget = JsonEditorWidget(attrs={
'schema': json.dumps(description_schema),
'title': 'Descriptions'
})
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]['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"""
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]['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
dovoodi_admin_site.register(HadisSect, HadisSectAdmin)
dovoodi_admin_site.register(HadisCategory, HadisCategoryAdmin)