feat: audit ai config changes

This commit is contained in:
QiuSW
2026-07-02 11:42:39 +08:00
parent 18ac054dc5
commit c0020e8d29
16 changed files with 520 additions and 23 deletions
+39
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
@@ -133,3 +134,41 @@ class ModelAlias(models.Model):
def save(self, *args, **kwargs) -> None:
self.full_clean()
super().save(*args, **kwargs)
class AiConfigAuditLog(models.Model):
class TargetType(models.TextChoices):
AI_MODEL = "ai_model", "AI model"
MODEL_ALIAS = "model_alias", "Model alias"
class Action(models.TextChoices):
CREATE = "create", "Create"
UPDATE = "update", "Update"
DELETE = "delete", "Delete"
actor = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
action = models.CharField(max_length=16, choices=Action.choices)
target_type = models.CharField(max_length=32, choices=TargetType.choices)
target_id = models.PositiveBigIntegerField(null=True, blank=True)
target_repr = models.CharField(max_length=255)
changed_fields = models.JSONField(default=list, blank=True)
changes = models.JSONField(default=dict, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "ai_config_audit_log"
ordering = ("-created_at", "-id")
indexes = [
models.Index(fields=("target_type", "target_id")),
models.Index(fields=("action", "created_at")),
models.Index(fields=("actor", "created_at")),
]
def __str__(self) -> str:
return f"{self.created_at:%Y-%m-%d %H:%M:%S} {self.action} {self.target_repr}"