diff --git a/ApiAutomationTest/app/cores/ws.py b/ApiAutomationTest/app/cores/ws.py index 6c6e5c6..1b164fd 100644 --- a/ApiAutomationTest/app/cores/ws.py +++ b/ApiAutomationTest/app/cores/ws.py @@ -20,6 +20,7 @@ def emit_dispatcher_result(id, type, result): :param result: 结果 :return: """ + user_id = current_user.id if current_user.is_authenticated else 0 socketio.emit( SocketIOEvent.DISPATCHER_RESULT, dict( @@ -27,7 +28,7 @@ def emit_dispatcher_result(id, type, result): type=str(type), result=str(result), ), - namespace='/user/' + str(current_user.id), + namespace='/user/' + str(user_id), ) @@ -38,6 +39,7 @@ def emit_dispatcher_start(id, type, report_id): :param type:调度类型 project/module :param report_id: 报告id """ + user_id = current_user.id if current_user.is_authenticated else 0 socketio.emit( SocketIOEvent.DISPATCHER_BEGIN, dict( @@ -45,7 +47,7 @@ def emit_dispatcher_start(id, type, report_id): type=str(type).lower(), report_id=str(report_id), ), - namespace='/user/' + str(current_user.id), + namespace='/user/' + str(user_id), ) @@ -56,6 +58,7 @@ def emit_dispatcher_end(id, type, end_type): :param type:调度类型 project/module :param end_type: 结束类型 DISPATCHER_END_TYPE """ + user_id = current_user.id if current_user.is_authenticated else 0 socketio.emit( SocketIOEvent.DISPATCHER_END, dict( @@ -63,7 +66,7 @@ def emit_dispatcher_end(id, type, end_type): type=str(type).lower(), end_type=end_type, ), - namespace='/user/' + str(current_user.id), + namespace='/user/' + str(user_id), ) # def test_message(message): diff --git a/ApiAutomationTest/app/models.py b/ApiAutomationTest/app/models.py index dbd22e0..533cf2e 100644 --- a/ApiAutomationTest/app/models.py +++ b/ApiAutomationTest/app/models.py @@ -2668,7 +2668,13 @@ class Dispatcher(db.Model): @classmethod def add(cls, element_type, element_id): - dispatcher = cls(element_type=element_type, element_id=element_id, user_id=current_user.id) + # 定时构建会在服务器启动后第一个请求前app.before_first_request注册 + # 而此时如果用户未登录则请求上下文中无法获取正确的用户id即current_user.id + # 因此这里需要做下判断 + if current_user.is_authenticated: + dispatcher = cls(element_type=element_type, element_id=element_id, user_id=current_user.id) + else: + dispatcher = cls(element_type=element_type, element_id=element_id) db.session.add(dispatcher) db.session.commit() return dispatcher diff --git a/ApiAutomationTest/app/utils/routes.py b/ApiAutomationTest/app/utils/routes.py index 88624f8..a2dbe3d 100644 --- a/ApiAutomationTest/app/utils/routes.py +++ b/ApiAutomationTest/app/utils/routes.py @@ -1,7 +1,7 @@ # coding=utf-8 from flask import send_file -from flask_login import current_user +from flask_login import current_user, login_required import os from app.utils import bp @@ -9,6 +9,7 @@ from app.config import attachment_dir @bp.route("/download/", methods=['GET']) +@login_required def download_file(filename): user_id = current_user.id file_path = os.path.join(attachment_dir, str(user_id), filename)