From 5cc57fbb0ab328cd90aaeb6dd9daf16bf39c49be Mon Sep 17 00:00:00 2001 From: blackholll Date: Mon, 6 May 2019 23:52:21 +0800 Subject: [PATCH] bugfix: condition_expression after task state is not available --- service/ticket/ticket_base_service.py | 11 +++-- tasks.py | 65 +++------------------------ 2 files changed, 15 insertions(+), 61 deletions(-) diff --git a/service/ticket/ticket_base_service.py b/service/ticket/ticket_base_service.py index 69bde9b7..b010a119 100644 --- a/service/ticket/ticket_base_service.py +++ b/service/ticket/ticket_base_service.py @@ -809,12 +809,13 @@ def get_ticket_format_participant_info(cls, ticket_id): @classmethod @auto_log - def ticket_handle_permission_check(cls, ticket_id, username, by_timer=False): + def ticket_handle_permission_check(cls, ticket_id, username, by_timer=False, by_task=False): """ 处理权限校验: 获取当前状态是否需要处理, 该用户是否有权限处理 :param ticket_id: :param username: :param by_timer:是否为定时器流转 + :param by_task:是否为通过脚本流转 :return: """ ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() @@ -830,6 +831,9 @@ def ticket_handle_permission_check(cls, ticket_id, username, by_timer=False): if by_timer and username == 'loonrobot': # 定时器流转,有权限 return True, '定时器流转,放开处理权限' + if by_task and username == 'loonrobot': + # 脚本流转,有权限 + return True, '脚本流转,放开处理权限' participant_type_id = ticket_obj.participant_type_id participant = ticket_obj.participant @@ -930,13 +934,14 @@ def get_ticket_transition(cls, ticket_id, username): @classmethod @auto_log - def handle_ticket(cls, ticket_id, request_data_dict, by_timer=False): + def handle_ticket(cls, ticket_id, request_data_dict, by_timer=False, by_task=False): """ 处理工单:校验必填参数,获取当前状态必填字段,更新工单基础字段,更新工单自定义字段, 更新工单流转记录,执行必要的脚本,通知消息 此处逻辑和新建工单有较多重复,下个版本会拆出来 :param ticket_id: :param request_data_dict: :param by_timer: 是否通过定时器触发的流转 + :param by_task: 是否通过脚本执行完成后触发的流转 :return: """ transition_id = request_data_dict.get('transition_id', '') @@ -951,7 +956,7 @@ def handle_ticket(cls, ticket_id, request_data_dict, by_timer=False): return False, '工单不存在或已被删除' # 判断用户是否有权限处理该工单 - has_permission, msg = cls.ticket_handle_permission_check(ticket_id, username, by_timer) + has_permission, msg = cls.ticket_handle_permission_check(ticket_id, username, by_timer, by_task) if not has_permission: return False, msg if msg['need_accept']: diff --git a/tasks.py b/tasks.py index e110114b..a48cca5f 100644 --- a/tasks.py +++ b/tasks.py @@ -105,6 +105,7 @@ def run_flow_task(ticket_id, script_id_str, state_id, action_from='loonrobot'): ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=False).first() # 新增处理记录,脚本后只允许只有一个后续直连状态 transition_obj = Transition.objects.filter(source_state_id=state_id, is_deleted=False).first() + new_ticket_flow_dict = dict(ticket_id=ticket_id, transition_id=transition_obj.id, suggestion=script_result_msg, participant_type_id=CONSTANT_SERVICE.PARTICIPANT_TYPE_ROBOT, participant='脚本:(id:{}, name:{})'.format(script_obj.id, script_obj.name), state_id=state_id, creator='loonrobot') @@ -116,64 +117,12 @@ def run_flow_task(ticket_id, script_id_str, state_id, action_from='loonrobot'): ticket_obj.save() return False, script_result_msg # 自动执行流转 - tar_state_obj = State.objects.filter(id=transition_obj.destination_state_id, is_deleted=False).first() - if tar_state_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_VARIABLE: - if tar_state_obj.participant == 'creator': - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_PERSONAL - destination_participant = ticket_obj.creator - elif tar_state_obj.participant == 'creator_tl': - approver, msg = AccountBaseService.get_user_dept_approver(ticket_obj.creator) - if len(approver.split(',')) > 1: - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_MULTI - else: - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_PERSONAL - destination_participant = approver - elif tar_state_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_FIELD: - destination_participant, msg = TicketBaseService.get_ticket_field_value(ticket_id, tar_state_obj.participant) - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_PERSONAL - if len(destination_participant.split(',')) > 1: - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_MULTI - elif tar_state_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_PARENT_FIELD: - parent_ticket_id = ticket_obj.parent_ticket_id - destination_participant, msg = TicketBaseService.get_ticket_field_value(parent_ticket_id, tar_state_obj.participant) - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_PERSONAL - if len(destination_participant.split(',')) > 1: - destination_participant_type_id = CONSTANT_SERVICE.PARTICIPANT_TYPE_MULTI - else: - # 其他类型不换算成实际的处理人 - destination_participant_type_id = tar_state_obj.participant_type_id - destination_participant = tar_state_obj.participant - - ticket_obj.participant = destination_participant - ticket_obj.participant_type_id = destination_participant_type_id - ticket_obj.state_id = tar_state_obj.id - ticket_obj.save() - - add_relation, msg = TicketBaseService.get_ticket_dest_relation(destination_participant_type_id, destination_participant) - if add_relation: - new_relation, msg = TicketBaseService.add_ticket_relation(ticket_id, add_relation) # 更新关系人信息 - - logger.info('******脚本执行成功,工单基础信息更新完成, ticket_id:{}******'.format(ticket_id)) - - # 子工单处理 - if tar_state_obj.type_id == CONSTANT_SERVICE.STATE_TYPE_END: - if ticket_obj.parent_ticket_id: - sub_ticket_queryset = TicketRecord.objects.filter(parent_ticket_id=ticket_obj.parent_ticket_id, is_deleted=False) - sub_ticket_state_id_list = [] - for sub_ticket_query0 in sub_ticket_queryset: - sub_ticket_state_id_list.append(sub_ticket_query0.state_id) - if set(sub_ticket_state_id_list) == set([ticket_obj.state_id]): - # 父工单的所有子工单都已处理结束,自动流转父工单 - parent_ticket_obj = TicketRecord.objects.filter(id=ticket_obj.parent_ticket_id, is_deleted=False).first() - parent_transition_obj = Transition.object.filter(source_state_id=parent_ticket_obj.state_id, is_deleted=False).first() - flag, msg = TicketBaseService.handle_ticket(parent_ticket_obj.id, dict(username='loonrobot', suggestion='所有子工单都已结束,自动流转', - transition_id=parent_transition_obj.id)) - if not flag: - return True, msg - # 下个状态也是脚本处理 - if tar_state_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_ROBOT: - run_flow_task.apply_async(args=[ticket_id, tar_state_obj.participant, tar_state_obj.id], queue='loonflow') - return True, '' + flag, msg = TicketBaseService.handle_ticket(ticket_id, dict(username='loonrobot', + suggestion='脚本执行完成后自行流转', + transition_id=transition_obj.id), False, True) + if flag: + logger.info('******脚本执行成功,工单基础信息更新完成, ticket_id:{}******'.format(ticket_id)) + return flag, msg else: return False, '工单当前处理人为非脚本,不执行脚本'