CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-django-dynamic-preferences

Dynamic global and instance settings for your django project

Pending
Overview
Eval results
Files

registries.mddocs/

Registries

Registration and organization system for preferences with section support and manager instantiation. Registries provide centralized preference management and act as the bridge between preference definitions and runtime access.

Capabilities

Preference Registry

Main registry class for organizing and managing preference objects with section support and manager creation.

class PreferenceRegistry:
    """
    Main registry for preference objects.
    
    Attributes:
    - name (str): Registry identifier
    - preference_model: Associated model class
    - section_url_namespace: URL namespace for sections
    """
    name: str
    preference_model = None
    section_url_namespace: str = None
    
    def register(self, preference_class):
        """
        Store preference class in registry.
        
        Args:
        - preference_class: Preference class to register
        
        Returns:
        The registered preference class (allows use as decorator)
        """
    
    def get(self, name: str, section: str = None, fallback: bool = False):
        """
        Get registered preference by name and optional section.
        
        Args:
        - name: Preference name
        - section: Section name (optional)
        - fallback: If True, return None instead of raising exception
        
        Returns:
        Preference class instance
        
        Raises:
        - NotFoundInRegistry: If preference not found and fallback=False
        """
    
    def get_by_name(self, name: str):
        """
        Get preference by name only (without section).
        
        Args:
        - name: Preference name
        
        Returns:
        Preference class instance
        
        Raises:
        - NotFoundInRegistry: If preference not found or name not unique
        """
    
    def manager(self, **kwargs) -> PreferencesManager:
        """
        Return preference manager instance.
        
        Args:
        - **kwargs: Additional options (instance for per-instance preferences)
        
        Returns:
        PreferencesManager instance
        """
    
    def sections(self) -> list:
        """
        Return list of registered sections.
        
        Returns:
        List of Section objects
        """
    
    def preferences(self, section: str = None) -> list:
        """
        Return list of preferences, optionally filtered by section.
        
        Args:
        - section: Section name to filter by (optional)
        
        Returns:
        List of preference class instances
        """

Global Preference Registry

Registry specifically for global (site-wide) preferences with appropriate URL namespace configuration.

class GlobalPreferenceRegistry(PreferenceRegistry):
    """
    Registry for global preferences.
    
    Attributes:
    - section_url_namespace: "dynamic_preferences:global.section"
    """
    section_url_namespace = "dynamic_preferences:global.section"

# Global instance for registering global preferences
global_preferences_registry: GlobalPreferenceRegistry

Per Instance Preference Registry

Base registry for preferences tied to specific model instances (e.g., user preferences).

class PerInstancePreferenceRegistry(PreferenceRegistry):
    """
    Registry for per-instance preferences.
    
    Used as base class for registries that manage preferences
    tied to specific model instances.
    """

Preference Models Registry

Registry that manages relationships between preference models and their corresponding preference registries.

class PreferenceModelsRegistry:
    """
    Store relationships between preference models and registries.
    
    Manages the mapping between Django models and preference registries,
    enabling automatic manager attachment and preference lookups.
    """
    
    def register(self, preference_model, preference_registry):
        """
        Register a model-registry relationship.
        
        Args:
        - preference_model: Django model class for storing preferences
        - preference_registry: Registry class for managing preferences
        """
    
    def attach_manager(self, model, registry):
        """
        Attach preference manager to model instances.
        
        Args:
        - model: Django model class
        - registry: Preference registry
        """
    
    def get_by_preference(self, preference):
        """
        Get registry by preference instance.
        
        Args:
        - preference: Preference class instance
        
        Returns:
        Associated registry
        """
    
    def get_by_instance(self, instance):
        """
        Get registry by model instance.
        
        Args:
        - instance: Django model instance
        
        Returns:
        Associated registry
        """

Usage Examples

Basic Registry Usage

from dynamic_preferences.preferences import Section
from dynamic_preferences.registries import global_preferences_registry
from dynamic_preferences.types import BooleanPreference, StringPreference

# Define sections
general = Section('general')
ui = Section('ui')

# Register preferences using decorator
@global_preferences_registry.register
class SiteTitle(StringPreference):
    section = general
    name = 'title'
    default = 'My Site'
    verbose_name = 'Site Title'

@global_preferences_registry.register
class MaintenanceMode(BooleanPreference):
    section = general
    name = 'maintenance_mode'
    default = False
    verbose_name = 'Maintenance Mode'

# Alternative registration without decorator
class DarkMode(BooleanPreference):
    section = ui
    name = 'dark_mode'
    default = False

global_preferences_registry.register(DarkMode)

Registry Inspection

# Get all registered sections
sections = global_preferences_registry.sections()
print([section.name for section in sections])  # ['general', 'ui']

# Get all preferences
all_prefs = global_preferences_registry.preferences()

# Get preferences in specific section
general_prefs = global_preferences_registry.preferences(section='general')

# Get specific preference
title_pref = global_preferences_registry.get('title', section='general')
# Or by unique name
title_pref = global_preferences_registry.get_by_name('title')

Manager Creation

# Create manager for accessing preferences
global_preferences = global_preferences_registry.manager()

# Use manager to access values
site_title = global_preferences['general__title']
maintenance_mode = global_preferences['general__maintenance_mode']

# Update values
global_preferences['general__title'] = 'New Site Title'

Custom Registry Creation

from dynamic_preferences.registries import PreferenceRegistry
from dynamic_preferences.models import GlobalPreferenceModel

class CustomPreferenceRegistry(PreferenceRegistry):
    name = 'custom'
    preference_model = GlobalPreferenceModel
    section_url_namespace = 'custom:section'

# Create registry instance
custom_registry = CustomPreferenceRegistry()

# Register preferences
@custom_registry.register
class CustomSetting(StringPreference):
    section = Section('custom')
    name = 'setting'
    default = 'value'

Working with Sections

from dynamic_preferences.preferences import Section

# Define sections with verbose names
general = Section('general', 'General Settings')
ui = Section('ui', 'User Interface')
email = Section('email', 'Email Configuration')

# Use in preference definitions
@global_preferences_registry.register
class SmtpHost(StringPreference):
    section = email
    name = 'smtp_host'
    default = 'localhost'
    verbose_name = 'SMTP Host'
    help_text = 'SMTP server hostname for sending emails'

# Access section information
smtp_pref = global_preferences_registry.get('smtp_host', section='email')
print(smtp_pref.section.verbose_name)  # "Email Configuration"

Registry Discovery

# Get registry for a preference
from dynamic_preferences.registries import preference_models_registry

# Find registry by preference instance
pref = global_preferences_registry.get('title', section='general')
registry = preference_models_registry.get_by_preference(pref)

# Find registry by model instance (for per-instance preferences)
user = User.objects.get(pk=1)
user_registry = preference_models_registry.get_by_instance(user)

Install with Tessl CLI

npx tessl i tessl/pypi-django-dynamic-preferences

docs

admin-integration.md

core-models.md

django-integration.md

forms-views.md

index.md

preference-types.md

registries.md

rest-api.md

serialization.md

signals.md

user-preferences.md

tile.json