Files
getDomain/domain-api/app/services/sensitive_words_service.py
Your Name ebf632e651 first
2026-04-16 21:35:47 +08:00

61 lines
1.5 KiB
Python

from __future__ import annotations
from app.core.db import get_db
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()
return {
"total": len(words),
"text": "\n".join(words),
}