diff --git a/apps/api/app/api/routes/templates_editor/_fallback.py b/apps/api/app/api/routes/templates_editor/_fallback.py index 1b7974780..031cf7d12 100755 --- a/apps/api/app/api/routes/templates_editor/_fallback.py +++ b/apps/api/app/api/routes/templates_editor/_fallback.py @@ -27,18 +27,14 @@ from packages.domain.edit_plan import EditPlanStatus logger = logging.getLogger(__name__) -def _auto_fallback_draft_to_editing( - svc: EditPlanService, plan_id: str, plan_check -) -> None: +def _auto_fallback_draft_to_editing(svc: EditPlanService, plan_id: str, plan_check) -> None: """自动兜底 1: draft → editing""" if plan_check.status == EditPlanStatus.DRAFT: logger.info("模板编辑器自动兜底: plan=%s draft→editing", plan_id) svc.transition_status(plan_id, EditPlanStatus.EDITING) -def _auto_fallback_copy_template_clips( - svc: EditPlanService, plan_id: str, plan_check, db: Session -) -> None: +def _auto_fallback_copy_template_clips(svc: EditPlanService, plan_id: str, plan_check, db: Session) -> None: """自动兜底 2: 无片段 + 有 template_id → 从模板复制片段配置""" existing_clips = svc.count_clips(plan_id) if existing_clips == 0 and plan_check.template_id: @@ -53,15 +49,15 @@ def _auto_fallback_copy_template_clips( for cfg in configs: svc.create_clip( plan_id=plan_id, - clip_type=cfg.clip_type.value - if hasattr(cfg.clip_type, "value") - else cfg.clip_type, + clip_type=cfg.clip_type.value if hasattr(cfg.clip_type, "value") else cfg.clip_type, order=cfg.order, template_clip_config_id=cfg.id, duration=cfg.default_duration, - transition_effect=cfg.transition_effect.value - if hasattr(cfg.transition_effect, "value") - else cfg.transition_effect, + transition_effect=( + cfg.transition_effect.value + if hasattr(cfg.transition_effect, "value") + else cfg.transition_effect + ), ) logger.info( "模板编辑器自动兜底: plan=%s 从 template_clip_configs 复制了 %d 个片段", @@ -90,17 +86,14 @@ def _auto_fallback_copy_template_clips( ) -def _auto_fallback_assign_assets( - svc: EditPlanService, plan_id: str, plan_check -) -> list: +def _auto_fallback_assign_assets(svc: EditPlanService, plan_id: str, plan_check) -> list: """自动兜底 3: 为没有素材的片段分配素材。返回剩余无素材片段列表。""" all_clips = svc.list_clips(plan_id) clips_without_asset = [c for c in all_clips if not c.asset_id] config_asset_ids = (plan_check.config or {}).get("asset_ids", []) logger.info( - "模板编辑器自动兜底3 诊断: plan=%s total_clips=%d " - "clips_without_asset=%d config_asset_ids=%r", + "模板编辑器自动兜底3 诊断: plan=%s total_clips=%d " "clips_without_asset=%d config_asset_ids=%r", plan_id, len(all_clips), len(clips_without_asset), @@ -164,7 +157,7 @@ def _auto_fallback_auto_material_mode( user_id: str = "", ) -> None: """自动兜底 4: 自动选素材分配给无素材片段 - + 查找策略(按优先级): 1. plan 有 project_id → 从项目素材库查找 2. plan 无 project_id 但有 user_id → 从用户上传的素材中查找 diff --git a/apps/api/app/api/routes/templates_editor/generation.py b/apps/api/app/api/routes/templates_editor/generation.py index 57d1aec14..b4e152290 100755 --- a/apps/api/app/api/routes/templates_editor/generation.py +++ b/apps/api/app/api/routes/templates_editor/generation.py @@ -71,7 +71,12 @@ def generate_editor_draft( _auto_fallback_copy_template_clips(plan_svc, plan_id, plan_check, db) clips_without_asset = _auto_fallback_assign_assets(plan_svc, plan_id, plan_check) _auto_fallback_auto_material_mode( - plan_svc, plan_id, plan_check, clips_without_asset, asset_library_repo, asset_repo, + plan_svc, + plan_id, + plan_check, + clips_without_asset, + asset_library_repo, + asset_repo, user_id=str(current_user.user.id), ) @@ -79,13 +84,9 @@ def generate_editor_draft( try: can_gen, reason = plan_svc.can_generate(plan_id) except ValueError as exc: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, detail=str(exc) - ) from exc + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc if not can_gen: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, detail=reason - ) + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=reason) try: clip_count = plan_svc.mark_clips_ready(plan_id) @@ -124,9 +125,7 @@ def generate_editor_draft( return EditPlanGenerateResponse( plan_id=plan_id, - plan_status=updated_plan.status.value - if hasattr(updated_plan.status, "value") - else updated_plan.status, + plan_status=updated_plan.status.value if hasattr(updated_plan.status, "value") else updated_plan.status, generation_task_id=gen_task.id, clip_count=clip_count, ) @@ -161,9 +160,7 @@ def get_editor_generation_status( try: gen_status = plan_svc.get_generation_status(plan_id) except ValueError as exc: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, detail=str(exc) - ) from exc + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc plan = gen_status["plan"] clips = gen_status["clips"] @@ -185,21 +182,15 @@ def get_editor_generation_status( video_url = "" if raw_video_url: try: - video_url = storage_service.get_download_url( - raw_video_url, expires_seconds=86400 - ) + video_url = storage_service.get_download_url(raw_video_url, expires_seconds=86400) except Exception as e: - logger.warning( - "生成视频签名URL失败: template_id=%s error=%s", template_id, e - ) + logger.warning("生成视频签名URL失败: template_id=%s error=%s", template_id, e) video_url = raw_video_url progress = gen_status.get("progress", 0.0) error_message = gen_status.get("error_message", "") gen_task_status = gen_status.get("generation_task_status") - plan_status_val = ( - plan.status.value if hasattr(plan.status, "value") else plan.status - ) + plan_status_val = plan.status.value if hasattr(plan.status, "value") else plan.status if plan_status_val == "completed" and progress < 100: progress = 100.0