@@ -758,7 +758,7 @@ def to_tool_workflow(work_flow, update_tool_map):
758758 return work_flow
759759
760760 @staticmethod
761- def to_tool (tool , workspace_id , user_id ):
761+ def to_tool (tool , workspace_id , user_id , folder_id ):
762762 return Tool (id = tool .get ('id' ),
763763 user_id = user_id ,
764764 name = tool .get ('name' ),
@@ -769,11 +769,28 @@ def to_tool(tool, workspace_id, user_id):
769769 is_active = False if len ((tool .get ('init_field_list' ) or [])) > 0 else tool .get ('is_active' ),
770770 tool_type = tool .get ('tool_type' , 'CUSTOM' ) or 'CUSTOM' ,
771771 scope = ToolScope .SHARED if workspace_id == 'None' else ToolScope .WORKSPACE ,
772- folder_id = 'default' if workspace_id == 'None' else workspace_id ,
772+ folder_id = folder_id if folder_id else 'default' if workspace_id == 'None' else workspace_id ,
773773 workspace_id = workspace_id )
774774
775- def import_workflow_tools (self , tool , workspace_id , user_id ):
776- tool_list = tool .get ('tool_list' ) or []
775+ def import_workflow_tools (self , tool , workspace_id , user_id , folder_id , new_child_policy ):
776+ """
777+
778+ @param tool: 工具对象
779+ @param workspace_id: 工作空间id
780+ @param user_id: 用户id
781+ @param folder_id: 文件夹id
782+ @param new_child_policy: 子工具创建策略
783+ 0: 不创建
784+ 1: 对比创建: 如果存在就不创建 不存在则创建
785+ 2: 全部创建
786+ @return:
787+ """
788+ if new_child_policy == 0 :
789+ tool_list = []
790+ elif new_child_policy == 1 :
791+ tool_list = tool .get ('tool_list' ) or []
792+ else :
793+ tool_list = [{** tool , 'id' : str (uuid .uuid7 ())} for tool in tool .get ('tool_list' ) or []]
777794 update_tool_map = {}
778795 if len (tool_list ) > 0 :
779796 tool_id_list = reduce (lambda x , y : [* x , * y ],
@@ -800,19 +817,31 @@ def import_workflow_tools(self, tool, workspace_id, user_id):
800817 tool .get ('work_flow' ),
801818 update_tool_map ,
802819 )
803- tool_model_list = [self .to_tool (tool , workspace_id , user_id ) for tool in tool_list ]
820+ QuerySet (ToolWorkflow ).update_or_create (tool_id = tool .get ('id' ),
821+ create_defaults = {'id' : uuid .uuid7 (),
822+ 'tool_id' : tool .get ('id' ),
823+ "workspace_id" : workspace_id ,
824+ 'work_flow' : work_flow , },
825+ defaults = {
826+ 'tool_id' : tool .get ('id' ),
827+ 'workspace_id' : workspace_id ,
828+ 'work_flow' : work_flow
829+ })
830+ tool_model_list = [self .to_tool (tool , workspace_id , user_id , folder_id ) for tool in tool_list ]
804831 workflow_tool_model_list = [{'tool_id' : t .get ('id' ), 'workflow' : self .to_tool_workflow (
805832 t .get ('work_flow' ),
806833 update_tool_map ,
807834 )} for t in tool_list if tool .get ('tool_type' ) == ToolType .WORKFLOW ]
808- workflow_tool_model_list . append ({ 'tool_id' : tool . get ( 'id' ), 'workflow' : work_flow })
835+
809836 existing_records = QuerySet (ToolWorkflow ).filter (
810837 tool_id__in = [wt .get ('tool_id' ) for wt in workflow_tool_model_list ],
811838 workspace_id = workspace_id )
839+
812840 existing_map = {
813841 record .tool_id : record
814842 for record in existing_records
815843 }
844+
816845 QuerySet (ToolWorkflow ).bulk_create (
817846 [ToolWorkflow (work_flow = wt .get ('workflow' ), workspace_id = workspace_id ,
818847 tool_id = wt .get ('tool_id' )) for wt in
@@ -826,6 +855,21 @@ def import_workflow_tools(self, tool, workspace_id, user_id):
826855 'auth_target_type' : AuthTargetType .TOOL .value
827856 }).auth_resource_batch ([t .id for t in tool_model_list ])
828857
858+ def update_template_workflow (self , tool_id : str ):
859+ self .is_valid (raise_exception = True )
860+ tool_instance_bytes = self .data .get ('file' ).read ()
861+ try :
862+ tool_instance = RestrictedUnpickler (io .BytesIO (tool_instance_bytes )).load ()
863+ except Exception as e :
864+ raise AppApiException (1001 , _ ("Unsupported file format" ))
865+ tool = tool_instance .tool
866+ tool ['id' ] = tool_id
867+ folder_id = self .data .get ('folder_id' )
868+ self .import_workflow_tools (tool , workspace_id = self .data .get ('workspace_id' ),
869+ user_id = self .data .get ('user_id' ),
870+ folder_id = folder_id , new_child_policy = 2 )
871+ return True
872+
829873 @transaction .atomic
830874 def import_ (self , scope = ToolScope .WORKSPACE , name = None ):
831875 self .is_valid ()
@@ -871,7 +915,8 @@ def import_(self, scope=ToolScope.WORKSPACE, name=None):
871915 tool_model .save ()
872916 if tool .get ('tool_type' ) == ToolType .WORKFLOW :
873917 tool ['id' ] = tool_id
874- self .import_workflow_tools (tool , workspace_id = self .data .get ('workspace_id' ), user_id = user_id )
918+ self .import_workflow_tools (tool , workspace_id = self .data .get ('workspace_id' ), user_id = user_id ,
919+ folder_id = folder_id , new_child_policy = 1 )
875920 # 自动授权给创建者
876921 UserResourcePermissionSerializer (data = {
877922 'workspace_id' : self .data .get ('workspace_id' ),
0 commit comments