mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-03-20 03:57:30 +08:00
Add workflow sharing, forking, and related API endpoints
Co-authored-by: jxxghp <jxxghp@163.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Any, Optional
|
||||
import json
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -14,6 +15,7 @@ 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
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -86,8 +88,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:
|
||||
"""
|
||||
删除工作流
|
||||
"""
|
||||
@@ -103,6 +105,92 @@ def delete_workflow(workflow_id: int,
|
||||
return schemas.Response(success=True, message="删除成功")
|
||||
|
||||
|
||||
@router.post("/share", summary="分享工作流", response_model=schemas.Response)
|
||||
def workflow_share(
|
||||
workflow_share: 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:
|
||||
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 "")
|
||||
return schemas.Response(success=state, message=errmsg)
|
||||
|
||||
|
||||
@router.delete("/share/{share_id}", summary="删除分享", response_model=schemas.Response)
|
||||
def workflow_share_delete(
|
||||
share_id: int,
|
||||
_: schemas.TokenPayload = Depends(get_current_active_user)) -> Any:
|
||||
"""
|
||||
删除分享
|
||||
"""
|
||||
state, errmsg = WorkflowHelper().share_delete(share_id=share_id)
|
||||
return schemas.Response(success=state, message=errmsg)
|
||||
|
||||
|
||||
@router.post("/fork", summary="复用工作流", response_model=schemas.Response)
|
||||
def workflow_fork(
|
||||
workflow_share: schemas.WorkflowShare,
|
||||
current_user: schemas.User = Depends(get_current_active_user)) -> Any:
|
||||
"""
|
||||
复用工作流
|
||||
"""
|
||||
# 获取分享的工作流数据
|
||||
shares = WorkflowHelper().get_shares()
|
||||
target_share = None
|
||||
for share in shares:
|
||||
if share.get("id") == workflow_share.id:
|
||||
target_share = share
|
||||
break
|
||||
|
||||
if not target_share:
|
||||
return schemas.Response(success=False, message="分享的工作流不存在")
|
||||
|
||||
# 创建工作流
|
||||
workflow_dict = {
|
||||
"name": target_share.get("name"),
|
||||
"description": target_share.get("description"),
|
||||
"timer": target_share.get("timer"),
|
||||
"actions": json.loads(target_share.get("actions", "[]")),
|
||||
"flows": json.loads(target_share.get("flows", "[]")),
|
||||
"context": json.loads(target_share.get("context", "{}")),
|
||||
"state": "P" # 默认暂停状态
|
||||
}
|
||||
|
||||
# 检查名称是否重复
|
||||
db = next(get_db())
|
||||
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.create(db)
|
||||
|
||||
# 更新复用次数
|
||||
if workflow_share.id:
|
||||
WorkflowHelper().workflow_fork(share_id=workflow_share.id)
|
||||
|
||||
return schemas.Response(success=True, message="复用成功")
|
||||
|
||||
|
||||
@router.get("/shares", summary="查询分享的工作流", response_model=List[schemas.WorkflowShare])
|
||||
def workflow_shares(
|
||||
name: Optional[str] = None,
|
||||
page: Optional[int] = 1,
|
||||
count: Optional[int] = 30,
|
||||
_: schemas.TokenPayload = Depends(get_current_active_user)) -> Any:
|
||||
"""
|
||||
查询分享的工作流
|
||||
"""
|
||||
return WorkflowHelper().get_shares(name=name, page=page, count=count)
|
||||
|
||||
|
||||
@router.post("/{workflow_id}/run", summary="执行工作流", response_model=schemas.Response)
|
||||
def run_workflow(workflow_id: int,
|
||||
from_begin: Optional[bool] = True,
|
||||
|
||||
Reference in New Issue
Block a user