Skip to content

Commit

Permalink
bugfix: condition_expression after task state is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
blackholll committed May 6, 2019
1 parent 752bf46 commit 5cc57fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 61 deletions.
11 changes: 8 additions & 3 deletions service/ticket/ticket_base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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', '')
Expand All @@ -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']:
Expand Down
65 changes: 7 additions & 58 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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, '工单当前处理人为非脚本,不执行脚本'

Expand Down

0 comments on commit 5cc57fb

Please sign in to comment.