def tocket_permission_check(self, request): signature = request.META.get('SIGNATURE') timestamp = request.META.get('TIMESTAMP') app_name = request.META.get('APPNAME') if not app_name: return False, '未提供appname' app_token, msg = AccountBaseService.get_token_by_app_name(app_name) if not app_token: return False, 'appname未授权,请联系管理员' return CommonService.signature_check(timestamp, signature, app_token)
def __init__(self): from service.common.common_service import CommonService flag, msg = CommonService.gen_signature('ops') if not flag: return dict(code=-1, msg=msg) self.signature = msg.get('signature', '') self.timestamp = msg.get('timestamp', '') self.headers = { 'HTTP_SIGNATURE': self.signature, 'HTTP_TIMESTAMP': self.timestamp, 'HTTP_APPNAME': 'ops' }
def token_permission_check(self, request): signature = request.META.get('HTTP_SIGNATURE') timestamp = request.META.get('HTTP_TIMESTAMP') app_name = request.META.get('HTTP_APPNAME') if not app_name: return False, '未提供appname(调用loonflow接口需要鉴权,请根据文档中"调用授权"部分说明来调用)' app_token_obj, msg = AccountBaseService.get_token_by_app_name(app_name) if not app_token_obj: return False, 'appname未授权,请联系管理员(调用loonflow接口需要鉴权,请根据文档中"调用授权"部分说明来调用)' return CommonService.signature_check(timestamp, signature, app_token_obj.token)
def flow_hook_task(ticket_id): """ hook 任务 :param ticket_id: :return: """ # 查询工单状态 ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() state_id = ticket_obj.state_id state_obj = State.objects.filter(id=state_id, is_deleted=0).first() participant_type_id = state_obj.participant_type_id if participant_type_id != CONSTANT_SERVICE.PARTICIPANT_TYPE_HOOK: return False, '' hook_config = state_obj.participant hook_config_dict = json.loads(hook_config) hook_url = hook_config_dict.get('hook_url') hook_token = hook_config_dict.get('hook_token') wait = hook_config_dict.get('wait') flag, msg = CommonService().gen_hook_signature(hook_token) if not flag: return False, msg all_ticket_data, msg = TicketBaseService().get_ticket_all_field_value( ticket_id) r = requests.post(hook_url, headers=msg, json=all_ticket_data, timeout=10) result = r.json() if result.get('code') == 0: # 调用成功 if wait: # date等格式需要转换为str for key, value in all_ticket_data.items(): if type(value) not in [int, str, bool, float]: all_ticket_data[key] = str(all_ticket_data[key]) all_ticket_data_json = json.dumps(all_ticket_data) TicketBaseService().add_ticket_flow_log( dict( ticket_id=ticket_id, transition_id=0, suggestion=result.get('msg'), participant_type_id=CONSTANT_SERVICE.PARTICIPANT_TYPE_HOOK, participant='hook', state_id=state_id, ticket_data=all_ticket_data_json, creator='loonrobot')) return True, '' else: # 不等待hook目标回调,直接流转 transition_queryset, msg = WorkflowTransitionService( ).get_state_transition_queryset(state_id) transition_id = transition_queryset[0] # hook状态只支持一个流转 new_request_dict = {} new_request_dict.update({ 'transition_id': transition_id, 'suggestion': msg, 'username': '******' }) # 执行流转 flag, msg = TicketBaseService().handle_ticket(ticket_id, new_request_dict, by_hook=True) if not flag: return False, msg else: TicketBaseService().update_ticket_field_value( {'script_run_last_result': False}) all_ticket_data, msg = TicketBaseService().get_ticket_all_field_value( ticket_id) # date等格式需要转换为str for key, value in all_ticket_data.items(): if type(value) not in [int, str, bool, float]: all_ticket_data[key] = str(all_ticket_data[key]) all_ticket_data_json = json.dumps(all_ticket_data) TicketBaseService().add_ticket_flow_log( dict(ticket_id=ticket_id, transition_id=0, suggestion=result.get('msg'), participant_type_id=CONSTANT_SERVICE.PARTICIPANT_TYPE_HOOK, participant='hook', state_id=state_id, ticket_data=all_ticket_data_json, creator='loonrobot'))