|
|
|
@ -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',) |
|
|
|
|