def get(self, request, *args, **kwargs): request_data = request.GET ticket_id = kwargs.get('ticket_id') # username = request_data.get('username', '') # 可用于权限控制 username = request.META.get('HTTP_USERNAME') per_page = int(request_data.get('per_page', 10)) page = int(request_data.get('page', 1)) from service.account.account_base_service import AccountBaseService app_name = request.META.get('HTTP_APPNAME') app_permission_check, msg = AccountBaseService.app_ticket_permission_check( app_name, ticket_id) if not app_permission_check: return api_response(-1, msg, '') if not username: return api_response(-1, '参数不全,请提供username', '') result, msg = TicketBaseService.get_ticket_flow_log( ticket_id, username, per_page, page) if result is not False: data = dict(value=result, per_page=msg['per_page'], page=msg['page'], total=msg['total']) code, msg, = 0, '' else: code, data = -1, '' return api_response(code, msg, data)
def get(self, request, *args, **kwargs): request_data = request.GET ticket_id = kwargs.get('ticket_id') username = request_data.get('username', '') # 可用于权限控制 per_page = int(request_data.get('per_page', 10)) page = int(request_data.get('page', 1)) result, msg = TicketBaseService.get_ticket_flow_log( ticket_id, username, per_page, page) if result is not False: data = dict(value=result, per_page=msg['per_page'], page=msg['page'], total=msg['total']) code, msg, = 0, '' else: code, data = -1, '' return api_response(code, msg, data)
def send_ticket_notice(ticket_id): """ 发送工单通知 :param ticket_id: :return: """ # 获取工单信息 # 获取工作流信息,获取工作流的通知信息 # 获取通知信息的标题和内容模板 # 将通知内容,通知标题,通知人,作为变量传给通知脚本 ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() if not ticket_obj: return False, 'ticket is not exist or has been deleted' workflow_id = ticket_obj.workflow_id workflow_obj = Workflow.objects.filter(id=workflow_id, is_deleted=0).first() notices = workflow_obj.notices if not notices: return True, 'no notice defined' notice_str_list = notices.split(',') notice_id_list = [int(notice_str) for notice_str in notice_str_list] send_notice_result_list = [] for notice_id in notice_id_list: notice_obj = CustomNotice.objects.filter(id=notice_id, is_deleted=0).first() if not notice_obj: continue title_template = notice_obj.title_template content_template = notice_obj.content_template # 获取工单所有字段的变量 ticket_value_info, msg = TicketBaseService.get_ticket_all_field_value( ticket_id) if not ticket_value_info: return False, msg title_result = title_template.format(**ticket_value_info) content_result = content_template.format(**ticket_value_info) notice_script_file_name = notice_obj.script.name notice_script_file = os.path.join(settings.MEDIA_ROOT, notice_script_file_name) # 获取工单最后一条操作记录 flow_log_list, msg = TicketBaseService.get_ticket_flow_log( ticket_id, 'loonrobot') last_flow_log = flow_log_list[0] participant_info_list = [] participant_username_list = [] from apps.account.models import LoonUser if ticket_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_PERSONAL: participant_username_list = [ticket_obj.participant] elif ticket_obj.participant_type_id in ( CONSTANT_SERVICE.PARTICIPANT_TYPE_MULTI, CONSTANT_SERVICE.PARTICIPANT_TYPE_MULTI_ALL): participant_username_list = ticket_obj.participant.split(',') elif ticket_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_ROLE: participant_username_list, msg = AccountBaseService.get_role_username_list( ticket_obj.participant) elif ticket_obj.participant_type_id == CONSTANT_SERVICE.PARTICIPANT_TYPE_DEPT: participant_username_list, msg = AccountBaseService.get_dept_username_list( ticket_obj.participant) if participant_username_list: participant_queryset = LoonUser.objects.filter( username__in=participant_username_list, is_deleted=0) for participant_0 in participant_queryset: participant_info_list.append( dict(username=participant_0.username, alias=participant_0.alias, phone=participant_0.phone, email=participant_0.email)) globals = { 'title_result': title_result, 'content_result': content_result, 'participant': ticket_obj.participant, 'participant_type_id': ticket_obj.participant_type_id, 'multi_all_person': ticket_obj.multi_all_person, 'ticket_value_info': ticket_value_info, 'last_flow_log': last_flow_log, 'participant_info_list': participant_info_list } try: with stdoutIO() as s: # execfile(script_file, globals) # for python 2 exec( open(notice_script_file, encoding='utf-8').read(), globals) script_result = True # script_result_msg = ''.join(s.buflist) script_result_msg = ''.join(s.getvalue()) logger.info( 'send notice successful for ticket_id: {}, notice_id:{}'. format(ticket_id, notice_id)) except Exception as e: logger.error(traceback.format_exc()) script_result = False script_result_msg = e.__str__() send_notice_result_list.append( dict(notice_id=notice_id, result=script_result, msg=script_result_msg)) return send_notice_result_list
def send_ticket_notice(ticket_id): """ 发送工单通知 :param ticket_id: :return: """ # 获取工单信息 # 获取工作流信息,获取工作流的通知信息 # 获取通知信息的标题和内容模板 # 将通知内容,通知标题,通知人,作为变量传给通知脚本 ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() if not ticket_obj: return False, 'ticket is not exist or has been deleted' if ticket_obj.participant_type_id not in ( CONSTANT_SERVICE.PARTICIPANT_TYPE_PERSONAL, CONSTANT_SERVICE.PARTICIPANT_TYPE_MULTI): # 个人及多人的情况才需要发送通知 return True, 'participant is not people, do not need notice' workflow_id = ticket_obj.workflow_id workflow_obj = Workflow.objects.filter(id=workflow_id, is_deleted=0).first() notices = workflow_obj.notices if not notices: return True, 'no notice defined' notice_str_list = notices.split(',') notice_id_list = [int(notice_str) for notice_str in notice_str_list] for notice_id in notice_id_list: notice_obj = CustomNotice.objects.filter(id=notice_id, is_deleted=0).first() if not notice_obj: continue title_template = notice_obj.title_template content_template = notice_obj.content_template # 获取工单所有字段的变量 ticket_value_info, msg = TicketBaseService.get_ticket_all_field_value( ticket_id) if not ticket_value_info: return False, msg title_result = title_template.format(**ticket_value_info) content_result = content_template.format(**ticket_value_info) notice_script_file_name = notice_obj.script.name notice_script_file = os.path.join(settings.MEDIA_ROOT, notice_script_file_name) # 获取工单最后一条操作记录 flow_log_list, msg = TicketBaseService.get_ticket_flow_log( ticket_id, 'loonrobot') last_flow_log = flow_log_list[0] globals = { 'title_result': title_result, 'content_result': content_result, 'participant': ticket_obj.participant, 'participant_type_id': ticket_obj.participant_type_id, 'multi_all_person': ticket_obj.multi_all_person, 'ticket_value_info': ticket_value_info, 'last_flow_log': last_flow_log } try: with stdoutIO() as s: # execfile(script_file, globals) # for python 2 exec(open(notice_script_file).read(), globals) script_result = True # script_result_msg = ''.join(s.buflist) script_result_msg = ''.join(s.getvalue()) logger.info( 'send notice successful for ticket_id: {}, notice_id:{}'. format(ticket_id, notice_id)) except Exception as e: logger.error(traceback.format_exc()) script_result = False script_result_msg = e.__str__() return script_result, script_result_msg