""" System-wide settings and configuration models """ from django.db import models from django.conf import settings from django.core.cache import cache class DebugConfiguration(models.Model): """ System-wide debug configuration (Singleton). Controls verbose logging and debugging features. """ # Debug settings enable_debug_logging = models.BooleanField( default=False, help_text="Enable verbose debug logging to console (AI steps, detailed execution)" ) log_ai_steps = models.BooleanField( default=True, help_text="Log AI function execution steps (only when debug logging enabled)" ) log_api_requests = models.BooleanField( default=False, help_text="Log all API requests and responses (only when debug logging enabled)" ) log_database_queries = models.BooleanField( default=False, help_text="Log database queries (only when debug logging enabled)" ) log_celery_tasks = models.BooleanField( default=True, help_text="Log Celery task execution (only when debug logging enabled)" ) # Audit fields updated_at = models.DateTimeField(auto_now=True) updated_by = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, help_text="Admin who last updated" ) class Meta: app_label = 'debug_system' db_table = 'igny8_debug_configuration' verbose_name = 'Debug Configuration' verbose_name_plural = 'Debug Configuration' def save(self, *args, **kwargs): """Enforce singleton pattern and clear cache on save""" self.pk = 1 super().save(*args, **kwargs) # Clear ALL debug-related caches when settings change cache.delete('debug_config') cache.delete('debug_enabled') cache.delete('debug_first_worker_pid') # Reset worker selection @classmethod def get_config(cls): """Get or create the singleton config (cached)""" config = cache.get('debug_config') if config is None: config, created = cls.objects.get_or_create(pk=1) cache.set('debug_config', config, 300) # Cache for 5 minutes return config @classmethod def is_debug_enabled(cls): """Fast check if debug logging is enabled (cached for performance)""" enabled = cache.get('debug_enabled') if enabled is None: config = cls.get_config() enabled = config.enable_debug_logging cache.set('debug_enabled', enabled, 60) # Cache for 1 minute return enabled def __str__(self): status = "ENABLED" if self.enable_debug_logging else "DISABLED" return f"Debug Configuration ({status})"