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.
 
 

108 lines
3.2 KiB

import json
from django import forms
from django.utils.translation import gettext_lazy as _
from dynamic_preferences.preferences import Section
from dynamic_preferences.registries import global_preferences_registry
from dynamic_preferences.types import (
BasePreferenceType,
BaseSerializer,
LongStringPreference,
StringPreference,
BooleanPreference,
)
from unfold.contrib.forms.widgets import WysiwygWidget
from unfold.widgets import UnfoldAdminTextareaWidget
# ---------------------------------------------------------
# Preference Types & Custom Serializers
# ---------------------------------------------------------
class EditorPreferences(LongStringPreference):
widget = WysiwygWidget(attrs={'class': 'editor-field'})
class EditorTextPreferences(LongStringPreference):
widget = UnfoldAdminTextareaWidget(attrs={'class': 'editor-field', 'rows': 15})
class JsonSerializer(BaseSerializer):
@classmethod
def serialize(cls, value, **kwargs):
return json.dumps(value, ensure_ascii=False)
@classmethod
def to_python(cls, value, **kwargs):
if isinstance(value, str) and len(value.strip()) > 0:
try:
return json.loads(value)
except json.JSONDecodeError:
return {}
return value if isinstance(value, dict) else {}
# ---------------------------------------------------------
# Sections
# ---------------------------------------------------------
general_section = Section('general', verbose_name=_('General Settings'))
contact_section = Section('contact', verbose_name=_('Contact & Support'))
content_section = Section('content', verbose_name=_('Content Pages'))
# ---------------------------------------------------------
# Registered Preferences
# ---------------------------------------------------------
@global_preferences_registry.register
class SiteName(StringPreference):
section = general_section
name = 'site_name'
verbose_name = _('Site Name')
default = 'My Application'
required = True
@global_preferences_registry.register
class MaintenanceMode(BooleanPreference):
section = general_section
name = 'maintenance_mode'
verbose_name = _('Maintenance Mode')
default = False
@global_preferences_registry.register
class ContactEmail(StringPreference):
section = contact_section
name = 'contact_email'
verbose_name = _('Support Email')
default = '[email protected]'
required = False
@global_preferences_registry.register
class SupportPhone(StringPreference):
section = contact_section
name = 'support_phone'
verbose_name = _('Support Phone Number')
default = '+1 555 1234567'
required = False
@global_preferences_registry.register
class AboutUs(EditorPreferences):
section = content_section
name = 'about_us'
verbose_name = _('About Us Page')
default = '<h2>About Us</h2><p>Welcome to our application.</p>'
required = False
@global_preferences_registry.register
class TermsAndConditions(EditorPreferences):
section = content_section
name = 'terms_and_conditions'
verbose_name = _('Terms & Conditions')
default = '<h2>Terms and Conditions</h2><p>Standard terms and conditions.</p>'
required = False