from django.shortcuts import redirect from django.urls import reverse from unfold.admin import ModelAdmin from utils.admin import dovoodi_admin_site, project_admin_site from apps.agent.models import AgentSettings class AgentSettingsAdmin(ModelAdmin): """ Singleton Admin for Agent Configuration. Acts as a 'Settings Page' by redirecting list view to the edit page of ID=1. """ def has_add_permission(self, request): # Disable 'Add' button to prevent creating multiple configs return False def has_delete_permission(self, request, obj=None): # Disable 'Delete' button to ensure settings always exist return False def changelist_view(self, request, extra_context=None): """ Redirect the 'List View' directly to the 'Edit View' of ID=1. Auto-creates the default config if it doesn't exist. """ # Ensure ID=1 exists obj, created = self.model.objects.get_or_create(pk=1, defaults={ "system_prompt": "You are a helpful assistant.", }) # Build the URL dynamically based on the registered admin site url = reverse( f"{self.admin_site.name}:{self.model._meta.app_label}_{self.model._meta.model_name}_change", args=[obj.pk] ) return redirect(url) fieldsets = ( ("Status", { "fields": ("is_maintenance_mode",), "classes": ("tab",), }), ("Brain (System Instructions)", { "fields": ("system_prompt",), "classes": ("tab",), "description": "Define the core personality and rules for the AI Agent." }), ("Model Parameters", { "fields": ("model_id", "temperature"), "classes": ("tab",), "description": "Technical settings for the inference engine." }), ) # Register with your custom admin site dovoodi_admin_site.register(AgentSettings, AgentSettingsAdmin) project_admin_site.register(AgentSettings, AgentSettingsAdmin)