from __future__ import annotations from django.conf import settings from django.db import models class ActiveSite(models.Model): """ Registry of a farm-location dashboard-cpsp deployment that HQ can mirror from. Outbound API key is stored here for local/dev scaffolding. Prefer a secret manager or encrypted field before production. """ active_site_id = models.BigAutoField(primary_key=True) code = models.SlugField(max_length=64, unique=True, help_text="Stable id, e.g. sukawarna") name = models.CharField(max_length=120) source_site_id = models.SlugField( max_length=64, blank=True, default="", db_index=True, help_text="Farm-location Site.site_id (slug); equals HQ sites.site_id when claimed", ) api_base_url = models.URLField( max_length=255, blank=True, default="", help_text="Optional site API root including /api/v1 (for outbound sync/push)", ) api_key = models.CharField( max_length=255, blank=True, help_text="X-API-Key for pulling from the site API (keep server-side only)", ) managed_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="managed_active_sites", help_text="GM / GM Admin who manages this active site", ) is_active = models.BooleanField(default=True) last_synced_at = models.DateTimeField(null=True, blank=True) last_sync_status = models.CharField(max_length=32, blank=True, default="") last_sync_message = models.TextField(blank=True, default="") created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "active_sites" ordering = ["name"] constraints = [ models.UniqueConstraint( fields=["source_site_id"], condition=~models.Q(source_site_id=""), name="uniq_active_site_source_site_id", ), ] def __str__(self) -> str: return f"{self.name} ({self.code})"