71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from app.core.db import get_db
|
|
from app.core.redis_client import get_redis
|
|
|
|
|
|
def get_sensitive_words_payload() -> dict:
|
|
with get_db() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
select word, category, priority
|
|
from sensitive_words
|
|
order by priority desc, word asc
|
|
"""
|
|
)
|
|
rows = cur.fetchall()
|
|
|
|
items = [
|
|
{
|
|
"word": row[0],
|
|
"category": row[1] or "default",
|
|
"priority": row[2] or 1,
|
|
}
|
|
for row in rows
|
|
]
|
|
return {
|
|
"items": items,
|
|
"text": "\n".join(item["word"] for item in items),
|
|
"total": len(items),
|
|
}
|
|
|
|
|
|
def save_sensitive_words_payload(payload: dict) -> dict:
|
|
raw_text = str(payload.get("text") or "")
|
|
words = []
|
|
seen: set[str] = set()
|
|
for line in raw_text.splitlines():
|
|
word = line.strip()
|
|
if not word or word in seen:
|
|
continue
|
|
seen.add(word)
|
|
words.append(word)
|
|
|
|
with get_db() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute("delete from sensitive_words")
|
|
if words:
|
|
cur.executemany(
|
|
"""
|
|
insert into sensitive_words (word, category, priority, create_time)
|
|
values (%s, 'default', 1, now())
|
|
""",
|
|
[(word,) for word in words],
|
|
)
|
|
conn.commit()
|
|
|
|
try:
|
|
redis_client = get_redis()
|
|
redis_client.set("domain_tool:sensitive_words", json.dumps(words, ensure_ascii=False))
|
|
redis_client.publish("domain_tool:config_update", "sensitive_words")
|
|
except Exception:
|
|
pass
|
|
|
|
return {
|
|
"total": len(words),
|
|
"text": "\n".join(words),
|
|
}
|