fix:1.修复导入导出缺少user_id导致报错 2.修复导出获取时间错误

This commit is contained in:
xiamuceer
2025-11-12 09:20:57 +08:00
parent d57bbf0553
commit c7fa5eb402
2 changed files with 20 additions and 5 deletions
+15 -4
View File
@@ -366,7 +366,11 @@ async def export_project_chapters(
txt_content.append("\n\n" + "=" * 80 + "\n\n")
txt_content.append(f"--- 全文完 ---")
txt_content.append(f"\n导出时间: {func.now()}")
# 获取当前时间
from datetime import datetime
export_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
txt_content.append(f"\n导出时间: {export_time}")
final_content = "\n".join(txt_content)
@@ -671,6 +675,7 @@ async def validate_import_file(
@router.post("/import", response_model=ImportResult, summary="导入项目")
async def import_project(
file: UploadFile = File(...),
request: Request = None,
db: AsyncSession = Depends(get_db)
):
"""
@@ -683,7 +688,13 @@ async def import_project(
导入结果
"""
try:
logger.info(f"开始导入项目: {file.filename}")
# 从认证中间件获取用户ID
user_id = getattr(request.state, 'user_id', None)
if not user_id:
logger.warning("未登录用户尝试导入项目")
raise HTTPException(status_code=401, detail="未登录")
logger.info(f"开始导入项目: {file.filename}, user_id={user_id}")
# 检查文件类型
if not file.filename.endswith('.json'):
@@ -703,8 +714,8 @@ async def import_project(
except json.JSONDecodeError as e:
raise HTTPException(status_code=400, detail=f"无效的JSON格式: {str(e)}")
# 导入数据
import_result = await ImportExportService.import_project(data, db)
# 导入数据(传入user_id
import_result = await ImportExportService.import_project(data, db, user_id)
if import_result.success:
logger.info(f"项目导入成功: {import_result.project_id}")
@@ -77,6 +77,7 @@ class ImportExportService:
"chapter_count": project.chapter_count,
"narrative_perspective": project.narrative_perspective,
"character_count": project.character_count,
"user_id": project.user_id,
"created_at": project.created_at.isoformat() if project.created_at else None,
}
@@ -423,7 +424,8 @@ class ImportExportService:
@staticmethod
async def import_project(
data: Dict,
db: AsyncSession
db: AsyncSession,
user_id: str
) -> ImportResult:
"""
导入项目数据(创建新项目)
@@ -431,6 +433,7 @@ class ImportExportService:
Args:
data: 导入的JSON数据
db: 数据库会话
user_id: 目标用户ID(导入后的项目归属)
Returns:
ImportResult: 导入结果
@@ -456,6 +459,7 @@ class ImportExportService:
# 创建项目
project_data = data["project"]
new_project = Project(
user_id=user_id, # 设置为当前用户ID
title=project_data.get("title"),
description=project_data.get("description"),
theme=project_data.get("theme"),