新增素材列表前测提交
This commit is contained in:
@@ -2,10 +2,15 @@ from typing import Any, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.models.resources_material import ResourcesMaterial
|
||||
from app.models.pre_test_template import PreTestTemplate
|
||||
|
||||
from app.dependencies import get_db
|
||||
from app.dependencies import get_db, get_current_user
|
||||
from app.schemas.resources_material import ResourcesMaterialListResponse
|
||||
from app.services.resources_material_service import get_resources_material_list
|
||||
from app.services.pre_test_queue import pre_test_queue
|
||||
from app.utils.id_gen import generate_id
|
||||
|
||||
router = APIRouter(prefix="/resources-material", tags=["resources-material"])
|
||||
|
||||
@@ -42,4 +47,98 @@ async def get_resources_material_list_api(
|
||||
"message": "查询成功",
|
||||
"data": items,
|
||||
"total": total,
|
||||
}
|
||||
}
|
||||
|
||||
#如果素材上传的时候没有指定前测,现在可以对已经上传好的素材进行前测
|
||||
@router.post(
|
||||
"/pre-commit",
|
||||
summary="素材列表提交前测",
|
||||
description="通过素材列表提交未前测的素材,异步处理,立即返回任务ID,结果稍后通过列表查询",
|
||||
)
|
||||
async def pre_test_material(
|
||||
resources_material_ids: list[str] = Query(..., description="资源素材表id"),
|
||||
pre_test_template_id: str = Query(..., description="前测模板id"),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: dict = Depends(get_current_user),
|
||||
) -> Any | dict:
|
||||
|
||||
try:
|
||||
invalid_ids = []
|
||||
grouped_videos = {}
|
||||
|
||||
#检查前测模板是否有效
|
||||
pre_test_template = await db.execute(
|
||||
select(PreTestTemplate)
|
||||
.where(
|
||||
PreTestTemplate.id == pre_test_template_id,
|
||||
PreTestTemplate.user_id == current_user.id,
|
||||
PreTestTemplate.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
pre_test_template = pre_test_template.scalar_one_or_none()
|
||||
if not pre_test_template:
|
||||
return {"code": 1, "message": f"前测模板{pre_test_template_id}不存在或不属于当前用户"}
|
||||
|
||||
for resource_material_id in resources_material_ids:
|
||||
resource_material = await db.execute(
|
||||
select(ResourcesMaterial)
|
||||
.where(
|
||||
ResourcesMaterial.id == resource_material_id,
|
||||
ResourcesMaterial.user_id == current_user.id,
|
||||
ResourcesMaterial.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
resource_material = resource_material.scalar_one_or_none()
|
||||
|
||||
if not resource_material:
|
||||
invalid_ids.append(f"{resource_material_id}: 资源不存在或不属于当前用户")
|
||||
continue
|
||||
|
||||
if resource_material.status is not None:
|
||||
invalid_ids.append(f"{resource_material_id}: 该资源已进行过前测")
|
||||
continue
|
||||
|
||||
if not resource_material.upload_id:
|
||||
invalid_ids.append(f"{resource_material_id}: 该资源未上传到平台")
|
||||
continue
|
||||
|
||||
if resource_material.resource_type != "video":
|
||||
invalid_ids.append(f"{resource_material_id}: 前测仅支持视频类型")
|
||||
continue
|
||||
|
||||
key = f"{resource_material.oauth_id}|{resource_material.advertiser_id}"
|
||||
if key not in grouped_videos:
|
||||
grouped_videos[key] = []
|
||||
grouped_videos[key].append({
|
||||
"id": resource_material.id,
|
||||
"upload_id": resource_material.upload_id,
|
||||
})
|
||||
|
||||
if invalid_ids:
|
||||
logger.error(f"前测素材提交失败,无效素材ID: {'; '.join(invalid_ids)}")
|
||||
return {"code": 1, "message": "; ".join(invalid_ids)}
|
||||
|
||||
if not grouped_videos:
|
||||
return {"code": 1, "message": "没有有效的视频资源"}
|
||||
|
||||
task_id = generate_id()
|
||||
|
||||
await pre_test_queue.enqueue({
|
||||
"task_id": task_id,
|
||||
"grouped_videos": grouped_videos,
|
||||
"pre_test_template_id": pre_test_template_id,
|
||||
})
|
||||
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "任务已提交,正在处理中",
|
||||
"data": {
|
||||
"task_id": task_id,
|
||||
"total_groups": len(grouped_videos),
|
||||
},
|
||||
}
|
||||
|
||||
except ValueError as e:
|
||||
return {"code": 1, "message": str(e)}
|
||||
except Exception as e:
|
||||
return {"code": 1, "message": str(e)}
|
||||
Reference in New Issue
Block a user