52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import models
|
|
|
|
from .normalization import normalize_text
|
|
|
|
|
|
class SensitiveWord(models.Model):
|
|
class Action(models.TextChoices):
|
|
BLOCK = "block", "拦截"
|
|
|
|
word = models.CharField("敏感词", max_length=255)
|
|
normalized_word = models.CharField("归一化敏感词", max_length=255, editable=False)
|
|
category = models.CharField("分类", max_length=64, default="custom", blank=True)
|
|
action = models.CharField("动作", max_length=16, choices=Action.choices, default=Action.BLOCK)
|
|
is_active = models.BooleanField("启用", default=True)
|
|
created_at = models.DateTimeField("创建时间", auto_now_add=True)
|
|
updated_at = models.DateTimeField("更新时间", auto_now=True)
|
|
|
|
class Meta:
|
|
db_table = "sensitive_word"
|
|
verbose_name = "敏感词"
|
|
verbose_name_plural = "敏感词"
|
|
ordering = ("category", "word")
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=("category", "normalized_word"),
|
|
name="unique_sensitive_word_per_category",
|
|
),
|
|
]
|
|
indexes = [
|
|
models.Index(fields=("is_active", "category"), name="sw_active_category_idx"),
|
|
models.Index(fields=("normalized_word",), name="sw_normalized_word_idx"),
|
|
]
|
|
|
|
def __str__(self) -> str:
|
|
return self.word
|
|
|
|
def clean(self) -> None:
|
|
self.word = (self.word or "").strip()
|
|
self.category = (self.category or "custom").strip() or "custom"
|
|
self.normalized_word = normalize_text(self.word)
|
|
if not self.normalized_word:
|
|
raise ValidationError({"word": "敏感词归一化后不能为空"})
|
|
if self.action != self.Action.BLOCK:
|
|
raise ValidationError({"action": "MVP 只支持 block 动作"})
|
|
|
|
def save(self, *args, **kwargs) -> None:
|
|
self.full_clean()
|
|
super().save(*args, **kwargs)
|