From 60088e41e90c1eafba52a054cbe6781a09edd814 Mon Sep 17 00:00:00 2001 From: azhengzz <820108271@qq.com> Date: Sun, 28 Mar 2021 17:55:49 +0800 Subject: [PATCH] =?UTF-8?q?bug=20fix:=20=E4=BF=AE=E5=A4=8D=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=B7=A5=E5=85=B7=E5=8D=95=E7=8B=AC=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=97=B6=E6=B2=A1=E6=9C=89=E4=BC=A0=E5=85=A5project=5Fid?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ApiAutomationTest/app/__init__.py | 5 ++- .../app/cores/tool/dispatcher.py | 7 +++- ApiAutomationTest/app/models.py | 37 +++++++++++++++++++ ApiAutomationTest/app/routes/ajax/routes.py | 5 ++- ApiAutomationTest/app/utils/util.py | 11 ++++++ 5 files changed, 60 insertions(+), 5 deletions(-) diff --git a/ApiAutomationTest/app/__init__.py b/ApiAutomationTest/app/__init__.py index 3c04b1f..19eb7f2 100644 --- a/ApiAutomationTest/app/__init__.py +++ b/ApiAutomationTest/app/__init__.py @@ -77,8 +77,9 @@ def register_shell_context(app: Flask): HTTPCaseFileUpload, Project, Module, Scene, Dispatcher, DispatcherDetail, Report, ReportCaseData, ReportCaseExpectationData, SubElementInLogicController, ReportToolData, ReportScriptToolData, ReportTimerToolData, ReportVariableDefinitionToolData, - ReportVariableDefinitionToolListData) + ReportVariableDefinitionToolListData, Tool) from app.email import send_email + from app.utils.util import get_project_id_from_current_element @app.shell_context_processor def make_shell_context(): @@ -95,6 +96,7 @@ def register_shell_context(app: Flask): 'Project': Project, 'Module': Module, 'Scene': Scene, + 'Tool': Tool, 'Dispatcher': Dispatcher, 'DispatcherDetail': DispatcherDetail, 'SubElementInLogicController': SubElementInLogicController, @@ -103,6 +105,7 @@ def register_shell_context(app: Flask): 'ReportTimerToolData': ReportTimerToolData, 'ReportVariableDefinitionToolData': ReportVariableDefinitionToolData, 'ReportVariableDefinitionToolListData': ReportVariableDefinitionToolListData, + 'get_project_id_from_current_element': get_project_id_from_current_element, } diff --git a/ApiAutomationTest/app/cores/tool/dispatcher.py b/ApiAutomationTest/app/cores/tool/dispatcher.py index 097ebb2..a8d8d35 100644 --- a/ApiAutomationTest/app/cores/tool/dispatcher.py +++ b/ApiAutomationTest/app/cores/tool/dispatcher.py @@ -9,7 +9,7 @@ from app.cores.tool.http_request_header_manager import HTTPRequestHeaderManager class ScriptToolDispatcher(AbstractToolDispatcher): - def __init__(self, tool, dispatcher_type=DISPATCHER_TYPE.DEBUG, logger=None, dispatcher=None): + def __init__(self, tool, dispatcher_type=DISPATCHER_TYPE.DEBUG, logger=None, dispatcher=None, project_id=None): """ :param tool: 单个Script Tool工具组件 :type tool: Tool @@ -19,9 +19,12 @@ class ScriptToolDispatcher(AbstractToolDispatcher): :type logger: DispatcherLogger :param dispatcher: 当dispatcher_type为DISPATCHER_TYPE.BUILD时,需要传入调度数据 :type dispatcher: Dispatcher + :param project_id: 工具所属的项目id + :type project_id: int """ super().__init__(tool=tool, dispatcher_type=dispatcher_type, logger=logger, dispatcher=dispatcher) + self.project_id = project_id # 脚本执行日志 self.log_text = '' @@ -30,7 +33,7 @@ class ScriptToolDispatcher(AbstractToolDispatcher): def execute(self): super().execute() - self.log_text = Script(tool=self.tool, dispatcher_logger=self.dispatcher_logger).exec_tool() + self.log_text = Script(tool=self.tool, dispatcher_logger=self.dispatcher_logger, project_id=self.project_id).exec_tool() def tear_down(self): super().tear_down() diff --git a/ApiAutomationTest/app/models.py b/ApiAutomationTest/app/models.py index 2a0ff40..28c0809 100644 --- a/ApiAutomationTest/app/models.py +++ b/ApiAutomationTest/app/models.py @@ -578,6 +578,29 @@ class LogicController(db.Model): cls.copy(original_logic_controller=original_logic_controller, parent_logic_controller=new_logic_controller, scene_id=scene_id) + @classmethod + def get_project_id_from_current_logic_controller(cls, logic_controller): + """找到当前逻辑控制器所属的项目id""" + def _recursive_find_scene_controller(logic_controller_id): + """递归查找SceneController的logic_controller_id""" + sub_element_in_logic_controller = SubElementInLogicController.query.filter_by( + element_id=logic_controller_id, + element_type=ELEMENT_TYPE.LOGIC_CONTROLLER, + ).first() + if sub_element_in_logic_controller: + return _recursive_find_scene_controller(sub_element_in_logic_controller.logic_controller_id) + else: + return logic_controller_id + + if logic_controller.logic_controller_type == LOGIC_CONTROLLER_TYPE.SCENE_CONTROLLER: + return logic_controller.specific_controller.scene.module.project_id + else: + # 从SubElementInLogicController关系中找到最上层的SceneController + logic_controller_id = _recursive_find_scene_controller(logic_controller.id) + scene_controller = SceneController.query.filter_by(logic_controller_id=logic_controller_id).first() + if scene_controller: + return scene_controller.scene.module.project_id + class SubElementInLogicController(db.Model): """ @@ -1079,6 +1102,20 @@ class Tool(db.Model): tool.status = STATUS.NORMAL db.session.commit() + @classmethod + def get_project_id_from_current_tool(cls, tool): + """找到当前工具所属的项目id""" + # 找到工具所在的逻辑控制器 + sub_element_in_logic_controller = SubElementInLogicController.query.filter_by( + element_id=tool.id, + element_type=ELEMENT_TYPE.TOOL, + ).first() + if sub_element_in_logic_controller: + parent_logic_controller_id = sub_element_in_logic_controller.logic_controller_id + logic_controller = LogicController.query.filter_by(id=parent_logic_controller_id).first() + if logic_controller: + return LogicController.get_project_id_from_current_logic_controller(logic_controller=logic_controller) + class TimerTool(db.Model): """ diff --git a/ApiAutomationTest/app/routes/ajax/routes.py b/ApiAutomationTest/app/routes/ajax/routes.py index c672c78..8c2ce3d 100644 --- a/ApiAutomationTest/app/routes/ajax/routes.py +++ b/ApiAutomationTest/app/routes/ajax/routes.py @@ -16,7 +16,7 @@ from app.cores.case.debug.dispatcher import DebugCaseDispatcher from app.cores.tool.dispatcher import ScriptToolDispatcher from app.cores.dispatcher import async_module_run, async_project_run, apscheduler_async_project_run from app.routes.ajax import bp -from app.utils.util import get_form_from_request, exception_handle +from app.utils.util import get_form_from_request, exception_handle, get_project_id_from_current_element from app.models import Case, Project, Module, Scene, HTTPCase, SSHCase, SQLCase, Dispatcher, DispatcherDetail, Report, \ SubElementInLogicController, LogicController, IfController, WhileController, LoopController, SimpleController, \ Scheduler, DingTalkRobotSetting, DingTalkRobotSettingAssociationProject, DebugCase, Tool, TimerTool, ScriptTool, \ @@ -809,7 +809,8 @@ def run_script_tool(): 'error_no': -1, 'error_msg': '错误信息 [脚本工具状态不是正常状态 tool_id=%s, status=%s]' % (form_tool_id, tool.status), }) - result = ScriptToolDispatcher(tool=tool, dispatcher_type=DISPATCHER_TYPE.DEBUG).run() # TODO BUG 未传project_id + project_id = get_project_id_from_current_element(element=tool) + result = ScriptToolDispatcher(tool=tool, dispatcher_type=DISPATCHER_TYPE.DEBUG, project_id=project_id).run() log_text = result.get('log_text', '') else: return jsonify({ diff --git a/ApiAutomationTest/app/utils/util.py b/ApiAutomationTest/app/utils/util.py index bc4945d..9356ac7 100644 --- a/ApiAutomationTest/app/utils/util.py +++ b/ApiAutomationTest/app/utils/util.py @@ -64,3 +64,14 @@ def get_form_from_request(req, name): }) else: return 1, value + + +def get_project_id_from_current_element(element): + from app.models import Case, LogicController, Tool + + if isinstance(element, Case): + return element.scene.module.project_id + elif isinstance(element, LogicController): + return LogicController.get_project_id_from_current_logic_controller(logic_controller=element) + elif isinstance(element, Tool): + return Tool.get_project_id_from_current_tool(tool=element)