新增更新token,提交素材,前测模板管理
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
import os
|
||||
from typing import Any, Optional
|
||||
from typing import Any, Optional, Dict
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.dependencies import get_current_user, get_db
|
||||
from app.models.user import User
|
||||
from app.models.pre_test_template import PreTestTemplate
|
||||
from app.services.upload_material_service import upload_material_to_platform
|
||||
from sqlalchemy import select
|
||||
from app.utils.douyinApi import DouyinApi
|
||||
|
||||
|
||||
router = APIRouter(prefix="/upload-material", tags=["上传素材"])
|
||||
|
||||
@@ -15,8 +19,8 @@ class UploadTask(BaseModel):
|
||||
advertiser_ids: list[str] = Field(..., description="广告主id数组,支持多条")
|
||||
resource_ids: list[str] = Field(..., description="资源id数组(generated_resources表主键)")
|
||||
oauth_id: str = Field(..., description="授权表id")
|
||||
is_pre_test: Optional[str] = Field(None, description="是否开启前测:是/否/0/1,预留字段")
|
||||
pre_test_template: Optional[str] = Field(None, description="前测模板id,预留字段")
|
||||
is_pre_test: Optional[str] = Field(None, description="是否开启前测:1=是/2=否")
|
||||
pre_test_template: Optional[str] = Field(None, description="前测模板id")
|
||||
|
||||
|
||||
class BatchUploadRequest(BaseModel):
|
||||
@@ -33,11 +37,35 @@ async def batch_upload_material(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> Any | dict:
|
||||
# try:
|
||||
# params = {
|
||||
# "advertiser_id": 1836693172153543,
|
||||
# "video_ids": ["tos-cn-i-sd07hgqsbj/2db9a53ee3ff4b6f8a2763b3bb463047"],
|
||||
# "diagnose_config": {"platform": "AD", "external_action": "AD_APP_ACTIVATE"},
|
||||
# }
|
||||
# response = await DouyinApi().pre_test_material("0019ecab9b8bc57d964", params)
|
||||
# return response
|
||||
# except Exception as e:
|
||||
# import traceback
|
||||
# return {
|
||||
# "code": 500,
|
||||
# "message": "请求失败",
|
||||
# "error": str(e),
|
||||
# "traceback": traceback.format_exc()
|
||||
# }
|
||||
if not req.tasks:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="上传任务列表不能为空",
|
||||
)
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "批量上传完成",
|
||||
"summary": {
|
||||
"total_tasks": 0,
|
||||
"total_success_count": 0,
|
||||
"total_fail_count": 0,
|
||||
"total_requested": 0,
|
||||
},
|
||||
"details": [],
|
||||
"error": "上传任务列表不能为空"
|
||||
}
|
||||
|
||||
all_results = []
|
||||
total_success = 0
|
||||
@@ -56,9 +84,73 @@ async def batch_upload_material(
|
||||
|
||||
try:
|
||||
if not task.advertiser_ids:
|
||||
raise ValueError("广告主id数组不能为空")
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": "广告主id数组不能为空",
|
||||
"success_count": 0,
|
||||
"fail_count": 0,
|
||||
"total_count": 0,
|
||||
"results": [],
|
||||
}
|
||||
all_results.append(task_result)
|
||||
continue
|
||||
|
||||
if not task.resource_ids:
|
||||
raise ValueError("资源id数组不能为空")
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": "资源id数组不能为空",
|
||||
"success_count": 0,
|
||||
"fail_count": 0,
|
||||
"total_count": 0,
|
||||
"results": [],
|
||||
}
|
||||
all_results.append(task_result)
|
||||
continue
|
||||
|
||||
if (task.is_pre_test == "1") and (not task.pre_test_template):
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": "开启前测功能时,必须指定前测模板id",
|
||||
"success_count": 0,
|
||||
"fail_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"total_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"results": [{
|
||||
"resource_id": rid,
|
||||
"advertiser_id": aid,
|
||||
"filename": "",
|
||||
"success": False,
|
||||
"error": "开启前测功能时,必须指定前测模板id"
|
||||
} for rid in task.resource_ids for aid in task.advertiser_ids],
|
||||
}
|
||||
total_fail += len(task.resource_ids) * len(task.advertiser_ids)
|
||||
all_results.append(task_result)
|
||||
continue
|
||||
|
||||
if task.is_pre_test == "1":
|
||||
template = await db.execute(
|
||||
select(PreTestTemplate).where(PreTestTemplate.id == task.pre_test_template).
|
||||
where(PreTestTemplate.deleted_at.is_(None)).
|
||||
where(PreTestTemplate.user_id == current_user.id)
|
||||
)
|
||||
template = template.scalar_one_or_none()
|
||||
if not template:
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": "前测模板id不存在",
|
||||
"success_count": 0,
|
||||
"fail_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"total_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"results": [{
|
||||
"resource_id": rid,
|
||||
"advertiser_id": aid,
|
||||
"filename": "",
|
||||
"success": False,
|
||||
"error": "前测模板id不存在"
|
||||
} for rid in task.resource_ids for aid in task.advertiser_ids],
|
||||
}
|
||||
total_fail += len(task.resource_ids) * len(task.advertiser_ids)
|
||||
all_results.append(task_result)
|
||||
continue
|
||||
|
||||
result = await upload_material_to_platform(
|
||||
task.resource_ids,
|
||||
@@ -66,6 +158,7 @@ async def batch_upload_material(
|
||||
task.oauth_id,
|
||||
db,
|
||||
current_user.id,
|
||||
task.pre_test_template if task.is_pre_test == "1" else None,
|
||||
)
|
||||
|
||||
task_result["result"] = {
|
||||
@@ -75,23 +168,37 @@ async def batch_upload_material(
|
||||
total_success += result["success_count"]
|
||||
total_fail += result["fail_count"]
|
||||
|
||||
if task.is_pre_test and task.is_pre_test in ["是", "1", "true", True]:
|
||||
task_result["pre_test_reserved"] = {
|
||||
"status": "reserved",
|
||||
"message": "前测功能已预留,待后续开通",
|
||||
"template": task.pre_test_template,
|
||||
}
|
||||
|
||||
|
||||
except ValueError as e:
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"success_count": 0,
|
||||
"fail_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"total_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"results": [{
|
||||
"resource_id": rid,
|
||||
"advertiser_id": aid,
|
||||
"filename": "",
|
||||
"success": False,
|
||||
"error": str(e)
|
||||
} for rid in task.resource_ids for aid in task.advertiser_ids],
|
||||
}
|
||||
total_fail += len(task.resource_ids) * len(task.advertiser_ids)
|
||||
except Exception as e:
|
||||
task_result["result"] = {
|
||||
"success": False,
|
||||
"error": f"上传失败: {str(e)}",
|
||||
"success_count": 0,
|
||||
"fail_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"total_count": len(task.resource_ids) * len(task.advertiser_ids),
|
||||
"results": [{
|
||||
"resource_id": rid,
|
||||
"advertiser_id": aid,
|
||||
"filename": "",
|
||||
"success": False,
|
||||
"error": f"上传失败: {str(e)}"
|
||||
} for rid in task.resource_ids for aid in task.advertiser_ids],
|
||||
}
|
||||
total_fail += len(task.resource_ids) * len(task.advertiser_ids)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user