mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-04-24 02:32:01 +08:00
fix workflow
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import List, Any, Optional
|
||||
import json
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app import schemas
|
||||
from app.chain.workflow import WorkflowChain
|
||||
from app.core.config import global_vars
|
||||
from app.core.plugin import PluginManager
|
||||
from app.core.workflow import WorkFlowManager
|
||||
from app.db import get_db
|
||||
from app.db.models.workflow import Workflow
|
||||
from app.db.models import Workflow
|
||||
from app.db.systemconfig_oper import SystemConfigOper
|
||||
from app.db.user_oper import get_current_active_user
|
||||
from app.chain.workflow import WorkflowChain
|
||||
from app.scheduler import Scheduler
|
||||
from app.helper.workflow import WorkflowHelper
|
||||
from app.scheduler import Scheduler
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -95,8 +95,8 @@ def update_workflow(workflow: schemas.Workflow,
|
||||
|
||||
@router.delete("/{workflow_id}", summary="删除工作流", response_model=schemas.Response)
|
||||
def delete_workflow(workflow_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
_: schemas.TokenPayload = Depends(get_current_active_user)) -> Any:
|
||||
db: Session = Depends(get_db),
|
||||
_: schemas.TokenPayload = Depends(get_current_active_user)) -> Any:
|
||||
"""
|
||||
删除工作流
|
||||
"""
|
||||
@@ -116,18 +116,18 @@ def delete_workflow(workflow_id: int,
|
||||
|
||||
@router.post("/share", summary="分享工作流", response_model=schemas.Response)
|
||||
def workflow_share(
|
||||
workflow_share: schemas.WorkflowShare,
|
||||
workflow: schemas.WorkflowShare,
|
||||
_: schemas.TokenPayload = Depends(get_current_active_user)) -> Any:
|
||||
"""
|
||||
分享工作流
|
||||
"""
|
||||
if not workflow_share.id or not workflow_share.share_title or not workflow_share.share_user:
|
||||
if not workflow.id or not workflow.share_title or not workflow.share_user:
|
||||
return schemas.Response(success=False, message="请填写工作流ID、分享标题和分享人")
|
||||
|
||||
state, errmsg = WorkflowHelper().workflow_share(workflow_id=workflow_share.id,
|
||||
share_title=workflow_share.share_title or "",
|
||||
share_comment=workflow_share.share_comment or "",
|
||||
share_user=workflow_share.share_user or "")
|
||||
|
||||
state, errmsg = WorkflowHelper().workflow_share(workflow_id=workflow.id,
|
||||
share_title=workflow.share_title or "",
|
||||
share_comment=workflow.share_comment or "",
|
||||
share_user=workflow.share_user or "")
|
||||
return schemas.Response(success=state, message=errmsg)
|
||||
|
||||
|
||||
@@ -144,56 +144,54 @@ def workflow_share_delete(
|
||||
|
||||
@router.post("/fork", summary="复用工作流", response_model=schemas.Response)
|
||||
def workflow_fork(
|
||||
workflow_share: schemas.WorkflowShare,
|
||||
workflow: schemas.WorkflowShare,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: schemas.User = Depends(get_current_active_user)) -> Any:
|
||||
_: schemas.User = Depends(get_current_active_user)) -> Any:
|
||||
"""
|
||||
复用工作流
|
||||
"""
|
||||
if not workflow_share.name:
|
||||
if not workflow.name:
|
||||
return schemas.Response(success=False, message="工作流名称不能为空")
|
||||
|
||||
|
||||
# 解析JSON数据,添加错误处理
|
||||
try:
|
||||
actions = json.loads(workflow_share.actions or "[]")
|
||||
actions = json.loads(workflow.actions or "[]")
|
||||
except json.JSONDecodeError:
|
||||
return schemas.Response(success=False, message="actions字段JSON格式错误")
|
||||
|
||||
|
||||
try:
|
||||
flows = json.loads(workflow_share.flows or "[]")
|
||||
flows = json.loads(workflow.flows or "[]")
|
||||
except json.JSONDecodeError:
|
||||
return schemas.Response(success=False, message="flows字段JSON格式错误")
|
||||
|
||||
|
||||
try:
|
||||
context = json.loads(workflow_share.context or "{}")
|
||||
context = json.loads(workflow.context or "{}")
|
||||
except json.JSONDecodeError:
|
||||
return schemas.Response(success=False, message="context字段JSON格式错误")
|
||||
|
||||
|
||||
# 创建工作流
|
||||
workflow_dict = {
|
||||
"name": workflow_share.name,
|
||||
"description": workflow_share.description,
|
||||
"timer": workflow_share.timer,
|
||||
"name": workflow.name,
|
||||
"description": workflow.description,
|
||||
"timer": workflow.timer,
|
||||
"actions": actions,
|
||||
"flows": flows,
|
||||
"context": context,
|
||||
"state": "P" # 默认暂停状态
|
||||
}
|
||||
|
||||
|
||||
# 检查名称是否重复
|
||||
from app.db.workflow_oper import WorkflowOper
|
||||
if WorkflowOper(db).get_by_name(workflow_dict["name"]):
|
||||
if Workflow.get_by_name(db, workflow_dict["name"]):
|
||||
return schemas.Response(success=False, message="已存在相同名称的工作流")
|
||||
|
||||
|
||||
# 创建新工作流
|
||||
from app.db.models.workflow import Workflow as WorkflowModel
|
||||
workflow = WorkflowModel(**workflow_dict)
|
||||
workflow = Workflow(**workflow_dict)
|
||||
workflow.create(db)
|
||||
|
||||
|
||||
# 更新复用次数
|
||||
if workflow_share.id:
|
||||
WorkflowHelper().workflow_fork(share_id=workflow_share.id)
|
||||
|
||||
|
||||
return schemas.Response(success=True, message="复用成功")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user