diff --git a/apps/hadis/admin/category.py b/apps/hadis/admin/category.py index ed4ea3f..e30f48d 100644 --- a/apps/hadis/admin/category.py +++ b/apps/hadis/admin/category.py @@ -1,16 +1,165 @@ 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 project_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',) @@ -18,7 +167,7 @@ class HadisSectAdmin(ModelAdmin): fieldsets = ( (None, { - 'fields': ('sect_type', 'title', 'is_active', 'order') + 'fields': ('sect_type', 'title', 'is_active', 'order','description') }), ) @@ -34,6 +183,7 @@ class HadisSectAdmin(ModelAdmin): 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') @@ -43,7 +193,7 @@ class HadisCategoryAdmin(DraggableMPTTAdmin, ModelAdmin): fieldsets = ( (None, { - 'fields': ('parent', 'sect', 'source_type', 'title', 'order') + 'fields': ('parent', 'sect', 'source_type', 'title', 'order','description') }), (_('Files'), { 'fields': ('xmind_file',), diff --git a/apps/hadis/admin/reference.py b/apps/hadis/admin/reference.py index 351b024..5175cf8 100644 --- a/apps/hadis/admin/reference.py +++ b/apps/hadis/admin/reference.py @@ -1,7 +1,10 @@ from django.contrib import admin +from django import forms from django.utils.translation import gettext_lazy as _ from unfold.admin import ModelAdmin, TabularInline from unfold.decorators import display +from utils.json_editor_field import JsonEditorWidget +import json # Import your custom admin site from utils.admin import project_admin_site @@ -14,6 +17,214 @@ from ..models import ( BookAttribute ) +# ----------------------------------------------------------------------------- +# Custom Forms for JSON Fields +# ----------------------------------------------------------------------------- + +class BookReferenceAdminForm(forms.ModelForm): + """Custom form for BookReference with JSON editor widgets""" + + class Meta: + model = BookReference + 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"] + } + } + + # Schema for language JSON field + language_schema = { + "type": "array", + "title": "Languages", + "items": { + "type": "object", + "title": "Language", + "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": "Language Text" + } + }, + "required": ["language_code", "text"] + } + } + + # Schema for publisher JSON field + publisher_schema = { + "type": "array", + "title": "Publishers", + "items": { + "type": "object", + "title": "Publisher", + "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": "Publisher 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' + }) + + self.fields['language'].widget = JsonEditorWidget(attrs={ + 'schema': json.dumps(language_schema), + 'title': 'Languages' + }) + + self.fields['publisher'].widget = JsonEditorWidget(attrs={ + 'schema': json.dumps(publisher_schema), + 'title': 'Publishers' + }) + + +class BookAttributeAdminForm(forms.ModelForm): + """Custom form for BookAttribute with JSON editor widgets""" + + class Meta: + model = BookAttribute + 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 value JSON field + value_schema = { + "type": "array", + "title": "Values", + "items": { + "type": "object", + "title": "Value", + "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": "Value Text" + } + }, + "required": ["language_code", "text"] + } + } + + # Apply JSON editor widgets + self.fields['title'].widget = JsonEditorWidget(attrs={ + 'schema': json.dumps(title_schema), + 'title': 'Titles' + }) + + self.fields['value'].widget = JsonEditorWidget(attrs={ + 'schema': json.dumps(value_schema), + 'title': 'Values' + }) + + # ----------------------------------------------------------------------------- # 1. Inlines # ----------------------------------------------------------------------------- @@ -44,7 +255,8 @@ class BookAttributeInline(TabularInline): class BookReferenceAdmin(ModelAdmin): """Admin for BookReference model""" - + form = BookReferenceAdminForm + # Use custom methods for JSON fields to show readable text list_display = ( 'get_title_display', @@ -140,6 +352,7 @@ class BookAttributeAdmin(ModelAdmin): Admin for managing Attributes independently. Useful if you want to see all attributes across all books. """ + form = BookAttributeAdminForm list_display = ('get_title_display', 'get_value_display', 'get_book_display', 'created_at') list_filter = ('created_at',) search_fields = ('book_reference__slug',) diff --git a/apps/hadis/views/category.py b/apps/hadis/views/category.py index 1ce1fc8..58ca1f7 100644 --- a/apps/hadis/views/category.py +++ b/apps/hadis/views/category.py @@ -107,7 +107,7 @@ class HadisCategoryTreeView(ListAPIView): 'id': category.sect.id, 'sect_type': category.sect.sect_type, 'title': get_localized_text(category.sect.title, request), - 'description': category.sect.description, + 'description': get_localized_text(category.sect.description,request), 'order': category.sect.order, 'source_types':list(set(source_types)) }