105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
"""合规正文:隐私政策、使用条款、网站声明。"""
|
|
|
|
from sqlalchemy import Boolean, String, Text, select
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.db import Base
|
|
|
|
|
|
def _mysql_args() -> dict:
|
|
return {"mysql_charset": "utf8mb4", "mysql_collate": "utf8mb4_unicode_ci"}
|
|
|
|
|
|
LEGAL_PAGE_KEYS = ("privacy", "terms", "statement")
|
|
LEGAL_PAGE_TITLES = {
|
|
"privacy": "隐私政策",
|
|
"terms": "使用条款",
|
|
"statement": "网站声明",
|
|
}
|
|
LEGAL_PAGE_PLACEHOLDERS = {
|
|
"privacy": "隐私政策整理中",
|
|
"terms": "使用条款整理中",
|
|
"statement": "网站声明整理中",
|
|
}
|
|
LEGAL_PAGE_PATHS = {
|
|
"privacy": "/legal/privacy",
|
|
"terms": "/legal/terms",
|
|
"statement": "/legal/statement",
|
|
}
|
|
|
|
|
|
class LegalPage(Base):
|
|
__tablename__ = "legal_page"
|
|
__table_args__ = _mysql_args()
|
|
|
|
page_key: Mapped[str] = mapped_column(String(32), primary_key=True)
|
|
body_html: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
published: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
|
|
|
|
def save_legal_page(db, page_key: str, payload: dict) -> None:
|
|
if page_key not in LEGAL_PAGE_KEYS:
|
|
raise ValueError("合规页键不正确")
|
|
from app.core.config import get_settings
|
|
from app.models.content import AdminFile
|
|
from app.services.html_filter import filter_html
|
|
|
|
keys = list(db.scalars(select(AdminFile.object_key).where(AdminFile.bucket == "public")))
|
|
cleaned = filter_html(
|
|
payload.get("bodyHtml") or "",
|
|
list(keys),
|
|
get_settings().minio_base_url,
|
|
)
|
|
row = db.get(LegalPage, page_key)
|
|
if row is None:
|
|
row = LegalPage(page_key=page_key)
|
|
db.add(row)
|
|
row.body_html = cleaned
|
|
row.published = bool(payload.get("published", False))
|
|
db.commit()
|
|
|
|
|
|
def admin_legal_page(db, page_key: str) -> dict | None:
|
|
if page_key not in LEGAL_PAGE_KEYS:
|
|
return None
|
|
row = db.get(LegalPage, page_key)
|
|
return {
|
|
"key": page_key,
|
|
"title": LEGAL_PAGE_TITLES[page_key],
|
|
"published": bool(row.published) if row else False,
|
|
"bodyHtml": row.body_html if row else "",
|
|
}
|
|
|
|
|
|
def public_legal_page(db, page_key: str) -> dict | None:
|
|
if page_key not in LEGAL_PAGE_KEYS:
|
|
return None
|
|
placeholder = LEGAL_PAGE_PLACEHOLDERS[page_key]
|
|
row = db.get(LegalPage, page_key)
|
|
body = (row.body_html if row else "").strip()
|
|
if row is None or not row.published or not body:
|
|
return {
|
|
"key": page_key,
|
|
"title": LEGAL_PAGE_TITLES[page_key],
|
|
"placeholder": placeholder,
|
|
}
|
|
return {
|
|
"key": page_key,
|
|
"title": LEGAL_PAGE_TITLES[page_key],
|
|
"bodyHtml": row.body_html,
|
|
}
|
|
|
|
|
|
def public_compliance_links() -> dict:
|
|
"""页脚固定三项,链到站内合规正文页。"""
|
|
return {
|
|
"links": [
|
|
{
|
|
"label": LEGAL_PAGE_TITLES[key],
|
|
"url": LEGAL_PAGE_PATHS[key],
|
|
"navigation": "sameWindow",
|
|
}
|
|
for key in LEGAL_PAGE_KEYS
|
|
]
|
|
}
|