From eac5b2617368e6161d5cfa63eaaee0d9fabc655e Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Tue, 21 Jul 2026 11:27:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(api):=20=E6=A8=A1=E6=9D=BF=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=99=A8=E5=85=BC=E5=AE=B9=E6=97=A7=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=EF=BC=8C=E8=A7=A3=E5=86=B3GET=20/editor=2050?= =?UTF-8?q?0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 两套模板系统(新edit_templates表 + 旧templates表)之前未打通, 导致用户自建模板(旧系统)访问编辑器时报500错误。 修复:get_draft_plan_id 依赖函数增加兼容逻辑: 1. 优先查新系统模板 + 草稿 2. 新系统找不到时回退到旧 templates 表 3. 基于旧模板 segments 自动创建草稿计划 --- apps/api/app/api/routes/templates_editor.py | 74 +++++++++++++++++++-- 1 file changed, 69 insertions(+), 5 deletions(-) mode change 100644 => 100755 apps/api/app/api/routes/templates_editor.py diff --git a/apps/api/app/api/routes/templates_editor.py b/apps/api/app/api/routes/templates_editor.py old mode 100644 new mode 100755 index 1fb6ae6b7..944f93742 --- a/apps/api/app/api/routes/templates_editor.py +++ b/apps/api/app/api/routes/templates_editor.py @@ -639,19 +639,83 @@ def get_draft_plan_id( template_id: str, services: tuple[EditTemplateService, EditPlanService] = Depends(get_editor_services), current_user: AuthenticatedUser = Depends(get_current_user), + db: Session = Depends(get_db_session), ) -> str: """ 路径依赖:根据 template_id 获取或创建草稿,返回 plan_id。 这是模板编辑器路由的核心依赖——所有编辑器端点都先经过这里, 确保 template_id → plan_id 的映射始终存在。 + + 兼容策略:优先从新模板系统(edit_templates 表)查找, + 若不存在则回退到旧模板系统(templates 表),确保用户自建模板可用。 """ - tpl_svc, _ = services - draft = tpl_svc.get_or_create_draft( - template_id, - user_id=str(current_user.user.id), + tpl_svc, plan_svc = services + user_id = str(current_user.user.id) + + # 1. 草稿已存在 → 直接返回 + draft = tpl_svc.get_template_draft(template_id) + if draft is not None: + return draft.id + + # 2. 新系统有模板 → 用新服务创建草稿 + if tpl_svc.get_template(template_id) is not None: + draft = tpl_svc.create_template_draft(template_id, user_id=user_id) + return draft.id + + # 3. 回退到旧模板系统(templates 表) + old_repo = SQLAlchemyTemplateRepository(db) + old_template = old_repo.get(template_id, user_id=user_id) + if old_template is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="模板不存在") + + # 4. 基于旧模板创建草稿计划 + from app.services.plan_generator_service import PlanGeneratorService + from packages.domain.edit_template import EditTemplate, EditTemplateStatus + from packages.domain.template_clip_config import TemplateClipConfig, ClipType + + # 构造伪 EditTemplate 对象(只填 generate_from_template 需要的字段) + pseudo_template = EditTemplate( + id=old_template.id, + name=old_template.name, + editing_mode=old_template.mode, + status=EditTemplateStatus.ACTIVE, ) - return draft.id + + # 将旧模板 segments 转换为 clip_configs + clip_configs: list[TemplateClipConfig] = [] + for seg in old_template.segments or []: + clip_configs.append( + TemplateClipConfig( + id=f"seg_{seg.id}", + template_id=old_template.id, + clip_type=ClipType.MAIN, + order=seg.segment_order, + min_duration=seg.duration_min, + max_duration=seg.duration_max, + ) + ) + + generator = PlanGeneratorService(db) + result = generator.generate_from_template( + template=pseudo_template, + clip_configs=clip_configs, + asset_ids=[], + created_by_user_id=user_id, + name=f"{old_template.name} - 草稿", + ) + plan = result["plan"] + + # 标记为模板草稿(后续可复用 tpl_svc.get_template_draft 的查找逻辑) + plan_svc.update_plan_config(plan.id, {"is_template_draft": True}) + + logger.info( + "旧模板自动创建草稿: template_id=%s draft_plan_id=%s user_id=%s", + template_id, + plan.id, + user_id, + ) + return plan.id # ── 草稿核心端点 ──────────────────────────────────────────────────────────── -- 2.54.0 From 1630c022fd40854d423b17b9ead7e890926e3913 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Tue, 21 Jul 2026 12:55:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(api):=20=E6=A8=A1=E6=9D=BF=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E5=99=A8=E5=85=BC=E5=AE=B9=E6=97=A7=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=EF=BC=8C=E8=A7=A3=E5=86=B3GET=20/editor=2050?= =?UTF-8?q?0=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/app/api/routes/templates_editor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100755 => 100644 apps/api/app/api/routes/templates_editor.py diff --git a/apps/api/app/api/routes/templates_editor.py b/apps/api/app/api/routes/templates_editor.py old mode 100755 new mode 100644 index 944f93742..58b5310c9 --- a/apps/api/app/api/routes/templates_editor.py +++ b/apps/api/app/api/routes/templates_editor.py @@ -671,8 +671,9 @@ def get_draft_plan_id( # 4. 基于旧模板创建草稿计划 from app.services.plan_generator_service import PlanGeneratorService + from packages.domain.edit_template import EditTemplate, EditTemplateStatus - from packages.domain.template_clip_config import TemplateClipConfig, ClipType + from packages.domain.template_clip_config import ClipType, TemplateClipConfig # 构造伪 EditTemplate 对象(只填 generate_from_template 需要的字段) pseudo_template = EditTemplate( -- 2.54.0