移除示例项目初始化并修复前后端稳定性问题
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from typing import Annotated, AsyncGenerator, Callable, Iterable
|
||||
import uuid
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi import Depends, HTTPException, Request, status
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
@@ -139,11 +139,71 @@ async def get_cra_site_scope(
|
||||
def require_study_not_locked():
|
||||
"""检查项目是否被锁定,如果锁定则拒绝所有修改请求"""
|
||||
from app.crud import study as study_crud
|
||||
from app.crud import faq_category as faq_category_crud
|
||||
from app.crud import faq_item as faq_item_crud
|
||||
|
||||
async def resolve_study_id(request: Request, db: AsyncSession) -> uuid.UUID:
|
||||
raw_study_id = request.path_params.get("study_id") or request.query_params.get("study_id")
|
||||
if raw_study_id:
|
||||
try:
|
||||
return uuid.UUID(str(raw_study_id))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="项目 ID 格式错误",
|
||||
) from exc
|
||||
|
||||
try:
|
||||
body = await request.json()
|
||||
except Exception:
|
||||
body = None
|
||||
if isinstance(body, dict) and body.get("study_id"):
|
||||
try:
|
||||
return uuid.UUID(str(body["study_id"]))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="项目 ID 格式错误",
|
||||
) from exc
|
||||
|
||||
raw_category_id = request.path_params.get("category_id")
|
||||
if raw_category_id:
|
||||
try:
|
||||
category_id = uuid.UUID(str(raw_category_id))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="分类 ID 格式错误",
|
||||
) from exc
|
||||
category = await faq_category_crud.get_category(db, category_id)
|
||||
if not category or not category.study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="分类不存在")
|
||||
return category.study_id
|
||||
|
||||
raw_item_id = request.path_params.get("item_id")
|
||||
if raw_item_id:
|
||||
try:
|
||||
item_id = uuid.UUID(str(raw_item_id))
|
||||
except ValueError as exc:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="FAQ ID 格式错误",
|
||||
) from exc
|
||||
item = await faq_item_crud.get_item(db, item_id)
|
||||
if not item or not item.study_id:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="FAQ 不存在")
|
||||
return item.study_id
|
||||
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="缺少项目 ID",
|
||||
)
|
||||
|
||||
async def dependency(
|
||||
study_id: uuid.UUID,
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db_session),
|
||||
):
|
||||
study_id = await resolve_study_id(request, db)
|
||||
study = await study_crud.get(db, study_id)
|
||||
if not study:
|
||||
raise HTTPException(
|
||||
|
||||
Reference in New Issue
Block a user