3434from common .db .search import native_search , native_page_search
3535from common .exception .app_exception import AppApiException
3636from common .field .common import UploadedFileField
37- from common .utils .common import get_file_content , valid_license , restricted_loads , generate_uuid
37+ from common .utils .common import get_file_content , restricted_loads , generate_uuid
3838from knowledge .models import Knowledge , KnowledgeScope
3939from knowledge .serializers .knowledge import KnowledgeSerializer , KnowledgeModelSerializer
4040from maxkb .conf import PROJECT_DIR
@@ -377,6 +377,7 @@ def page(self, current_page: int, page_size: int, instance: Dict):
377377
378378class ApplicationImportRequest (serializers .Serializer ):
379379 file = UploadedFileField (required = True , label = _ ("file" ))
380+ folder_id = serializers .CharField (required = True , label = _ ("Folder ID" ))
380381
381382
382383class ApplicationEditSerializer (serializers .Serializer ):
@@ -478,16 +479,14 @@ def insert_simple(self, instance: Dict):
478479 QuerySet (ApplicationKnowledgeMapping ).bulk_create (application_knowledge_mapping_model_list )
479480 return ApplicationCreateSerializer .ApplicationResponse (application_model ).data
480481
481- @valid_license (model = Application , count = 5 ,
482- message = _ (
483- 'The community version supports up to 5 applications. If you need more applications, please contact us (https://fit2cloud.com/).' ))
484482 @transaction .atomic
485- def import_ (self , instance : dict , with_valid = True ):
483+ def import_ (self , instance : dict , is_import_tool , with_valid = True ):
486484 if with_valid :
487485 self .is_valid ()
488486 ApplicationImportRequest (data = instance ).is_valid (raise_exception = True )
489487 user_id = self .data .get ('user_id' )
490488 workspace_id = self .data .get ("workspace_id" )
489+ folder_id = instance .get ('folder_id' )
491490 mk_instance_bytes = instance .get ('file' ).read ()
492491 try :
493492 mk_instance = restricted_loads (mk_instance_bytes )
@@ -498,15 +497,15 @@ def import_(self, instance: dict, with_valid=True):
498497 update_tool_map = {}
499498 if len (tool_list ) > 0 :
500499 tool_id_list = reduce (lambda x , y : [* x , * y ],
501- [[tool .get ('id' ), generate_uuid ((tool .get ('id' ) + tool . get ( ' workspace_id' ) or '' ))]
500+ [[tool .get ('id' ), generate_uuid ((tool .get ('id' ) + workspace_id or '' ))]
502501 for tool
503502 in
504503 tool_list ], [])
505504 # 存在的工具列表
506505 exits_tool_id_list = [str (tool .id ) for tool in
507506 QuerySet (Tool ).filter (id__in = tool_id_list , workspace_id = workspace_id )]
508507 # 需要更新的工具集合
509- update_tool_map = {tool .get ('id' ): generate_uuid ((tool .get ('id' ) + tool . get ( ' workspace_id' ) or '' )) for tool
508+ update_tool_map = {tool .get ('id' ): generate_uuid ((tool .get ('id' ) + workspace_id or '' )) for tool
510509 in
511510 tool_list if
512511 not exits_tool_id_list .__contains__ (
@@ -515,8 +514,8 @@ def import_(self, instance: dict, with_valid=True):
515514 tool_list = [{** tool , 'id' : update_tool_map .get (tool .get ('id' ))} for tool in tool_list if
516515 not exits_tool_id_list .__contains__ (
517516 tool .get ('id' )) and not exits_tool_id_list .__contains__ (
518- generate_uuid ((tool .get ('id' ) + tool . get ( ' workspace_id' ) or '' )))]
519- application_model = self .to_application (application , workspace_id , user_id , update_tool_map )
517+ generate_uuid ((tool .get ('id' ) + workspace_id or '' )))]
518+ application_model = self .to_application (application , workspace_id , user_id , update_tool_map , folder_id )
520519 tool_model_list = [self .to_tool (f , workspace_id , user_id ) for f in tool_list ]
521520 application_model .save ()
522521 # 插入授权数据
@@ -528,7 +527,14 @@ def import_(self, instance: dict, with_valid=True):
528527 # 插入认证信息
529528 ApplicationAccessToken (application_id = application_model .id ,
530529 access_token = hashlib .md5 (str (uuid .uuid7 ()).encode ()).hexdigest ()[8 :24 ]).save ()
531- QuerySet (Tool ).bulk_create (tool_model_list ) if len (tool_model_list ) > 0 else None
530+ if is_import_tool :
531+ if len (tool_model_list ) > 0 :
532+ QuerySet (Tool ).bulk_create (tool_model_list )
533+ UserResourcePermissionSerializer (data = {
534+ 'workspace_id' : self .data .get ('workspace_id' ),
535+ 'user_id' : self .data .get ('user_id' ),
536+ 'auth_target_type' : AuthTargetType .APPLICATION .value
537+ }).auth_resource_batch ([t .id for t in tool_model_list ])
532538 return True
533539
534540 @staticmethod
@@ -550,7 +556,7 @@ def to_tool(tool, workspace_id, user_id):
550556 workspace_id = workspace_id )
551557
552558 @staticmethod
553- def to_application (application , workspace_id , user_id , update_tool_map ):
559+ def to_application (application , workspace_id , user_id , update_tool_map , folder_id ):
554560 work_flow = application .get ('work_flow' )
555561 for node in work_flow .get ('nodes' , []):
556562 if node .get ('type' ) == 'tool-lib-node' :
@@ -563,7 +569,7 @@ def to_application(application, workspace_id, user_id, update_tool_map):
563569 user_id = user_id ,
564570 name = application .get ('name' ),
565571 workspace_id = workspace_id ,
566- folder_id = workspace_id ,
572+ folder_id = folder_id ,
567573 desc = application .get ('desc' ),
568574 prologue = application .get ('prologue' ), dialogue_number = application .get ('dialogue_number' ),
569575 knowledge_setting = application .get ('knowledge_setting' ),
0 commit comments