233e23016c
- 文章 HTML 存储从文件系统迁移至 articles 表,删除 releases 目录 - 合规审查从 DB 读取 HTML,审查结果写回 DB,通过后自动推进至待发布 - 新增 todayCount 筛选按钮,与系统概览统计数据一致 - 全屏预览修复:提升 z-index 超过侧边栏,添加退出全屏/关闭按钮 - 统一 '优化' → '审查' 命名,消除前后端术语不一致 - 调度器创作完成后自动触发审查(生成 → 审查 → 待发布) - 清理旧备份/调试文件、过期大纲和研究笔记
84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
||
"""发布管理 API"""
|
||
from fastapi import APIRouter, HTTPException, Depends, Request
|
||
from pydantic import BaseModel
|
||
from datetime import datetime
|
||
from typing import Optional
|
||
|
||
from ..database import get_db
|
||
from ..models import Topic, PublishRecord, User
|
||
from sqlalchemy.orm import Session
|
||
from .auth import get_current_admin
|
||
from ..core.audit_logger import audit_log
|
||
|
||
router = APIRouter(prefix="/api/publishing", tags=["publishing"])
|
||
|
||
class PublishRequest(BaseModel):
|
||
topic_id: str
|
||
|
||
class PublishResponse(BaseModel):
|
||
ok: bool
|
||
topic_id: str
|
||
message: str
|
||
|
||
@router.post("/create", response_model=PublishResponse)
|
||
async def create_publish_record(
|
||
req: PublishRequest,
|
||
request: Request,
|
||
db: Session = Depends(get_db),
|
||
current_user: User = Depends(get_current_admin)
|
||
):
|
||
"""标记选题为已发布,并创建发布记录(管理员)"""
|
||
try:
|
||
# 查找选题
|
||
topic = db.query(Topic).filter(Topic.id == req.topic_id).first()
|
||
if not topic:
|
||
raise HTTPException(status_code=404, detail=f"选题 {req.topic_id} 不存在")
|
||
|
||
if topic.status not in ('ready', '待发布'):
|
||
raise HTTPException(status_code=400, detail=f"选题 {req.topic_id} 状态不是待发布(当前: {topic.status})")
|
||
|
||
# 更新选题状态
|
||
topic.status = '已发布'
|
||
topic.updated_at = datetime.now()
|
||
topic.published_at = datetime.now().date() # 设置发布时间为今天
|
||
|
||
# 创建发布记录
|
||
operator = current_user.username
|
||
record = PublishRecord(
|
||
topic_id=req.topic_id,
|
||
platform='all',
|
||
action='publish',
|
||
status='success',
|
||
operator=operator,
|
||
description=f"选题 {req.topic_id} 已发布"
|
||
)
|
||
db.add(record)
|
||
db.commit()
|
||
# 强制刷新会话缓存,确保后续读取最新数据
|
||
db.expire_all()
|
||
db.refresh(topic)
|
||
|
||
# 审计日志
|
||
audit_log(
|
||
action="publish",
|
||
user=current_user,
|
||
resource_type="topic",
|
||
resource_id=req.topic_id,
|
||
details={"operator": operator, "status": "success"},
|
||
ip_address=request.client.host if request.client else None,
|
||
user_agent=request.headers.get("user-agent", ""),
|
||
db=db
|
||
)
|
||
|
||
return PublishResponse(
|
||
ok=True,
|
||
topic_id=req.topic_id,
|
||
message=f"选题 {req.topic_id} 已成功发布"
|
||
)
|
||
except HTTPException:
|
||
raise
|
||
except Exception as e:
|
||
db.rollback()
|
||
raise HTTPException(status_code=500, detail=str(e))
|