Exemple #1
0
    def post(self, request, *args, **kwargs):
        """
        转单操作, 直接修改工单处理人, 且工单状态不变,
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        # json_str = request.body.decode('utf-8')
        # request_data_dict = request.POST.dict()
        request_data_dict = json.loads(request.body.decode('utf-8'))
        if not request_data_dict:
            return api_response(0, "post 参数为空", {})
        # request_data_dict = json.loads(json_str)
        ticket_id = kwargs.get('pk', '')
        # username = request_data_dict.get('username', '')
        username = request.user.username
        target_username = request_data_dict.get('target_username', '')
        suggestion = request_data_dict.get('suggestion', '')

        result, msg = TicketBaseService.deliver_ticket(ticket_id, username, target_username, suggestion)
        if result:
            code, msg, data = 1, msg, result
        else:
            code, msg, data = 0, msg, ''
        return api_response(code, msg, data)
Exemple #2
0
 def post(self, request, *args, **kwargs):
     """
     处理工单
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     # json_str = request.body.decode('utf-8')
     # request_data_dict = request.POST.dict()
     request_data_dict = json.loads(request.body.decode('utf-8'))
     request_data_dict['username'] = request.user.username
     request_data_dict['modifier'] = request.user.username
     if not request_data_dict:
         return api_response(0, "patch参数为空", {})
     # request_data_dict = json.loads(json_str)
     ticket_id = kwargs.get('pk', '')
     if not ticket_id:
         return api_response(0, "请提供工单id", {})
     result, msg = TicketBaseService.handle_ticket(ticket_id, request_data_dict)
     if result is False:
         code, data = 0, ''
     else:
         code, data = 1, dict(value=result)
     return api_response(code, msg, data)
Exemple #3
0
 def post(self, request, *args, **kwargs):
     """
     修改工单状态
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     # json_str = request.body.decode('utf-8')
     # request_data_dict = request.POST.dict()
     request_data_dict = json.loads(request.body.decode('utf-8'))
     if not request_data_dict:
         return api_response(0, "post 参数为空", {})
     # request_data_dict = json.loads(json_str)
     ticket_id = kwargs.get('pk', '')
     username = request.user.username
     state_id = request_data_dict.get('state_id', '')
     if not username:
         return api_response(0, "参数不全, 请提供username", '')
     if not state_id:
         code, msg = 0, "请提供新的状态id"
         data = ''
     else:
         result, msg = TicketBaseService.update_ticket_state(ticket_id, state_id, username)
         if result:
             code, msg, data = 1, msg, ''
         else:
             code, msg, data = 0, msg, ''
     return api_response(code, msg, data)
Exemple #4
0
 def get(self, request, *args, **kwargs):
     request_data = request.GET
     ticket_id = kwargs.get('pk', '')
     username = request.user.username
     if not username:
         return api_response(0, "参数补全, 请提供 username", '')
     result, msg = TicketBaseService.get_ticket_transition(ticket_id, username)
     if result is False:
         code, data = 0, ''
     else:
         code, data = 1, dict(value=result)
     return api_response(code, msg, data)
Exemple #5
0
    def get(self, request, *args, **kwargs):
        request_data = request.GET
        ticket_id = kwargs.get('pk', '')
        username = request.user.username
        if not username:
            return api_response(0, "参数不全, 请提供username", '')

        result, msg = TicketBaseService.get_ticket_flow_step(ticket_id, username)
        if result:
            data = dict(value=result)
            code = 1
        else:
            code, data = 0, ''
        return api_response(code, msg, data)
Exemple #6
0
    def get(self, request, *args, **kwargs):
        request_data = request.GET
        ticket_id = kwargs.get('pk', '')
        username = request.user.username
        per_page = int(request_data.get('per_page', 10))
        page = int(request_data.get('page', 1))
        if not username:
            return api_response(0, "参数补全, 请提供 username", '')

        result, msg = TicketBaseService.get_ticket_flow_log(ticket_id, per_page, page)

        if result:
            data = dict(value=result, per_page=msg['per_page'], page=msg['page'], total=msg['total'])
            code, msg = 1, ''
        else:
            code, data = 0, ''

        return api_response(1, msg, data)
Exemple #7
0
 def post(self, request, *args, **kwargs):
     """
     删除工单,因为不想彻底清除工单记录,只是将删除字段置为0
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     request_data_dict = request.POST.dict()
     if not request_data_dict:
         return api_response(0, "post 参数为空", "")
     username = request.user.username
     delete_str = request_data_dict.get('delete_str', '')
     result, msg = TicketBaseService.ticket_delete(username, delete_str)
     if result:
         code, msg, data = 1, msg, result
     else:
         code, msg, data = 0, msg, ''
     return api_response(code, msg, data)
Exemple #8
0
    def get(self, request, *args, **kwargs):
        """
        获取工单详情, 根据用户返回不同的内容
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        request_data = request.GET
        ticket_id = kwargs.get('pk')
        username = request.user.username
        if not username:
            return api_response(-1, "参数补全, 请提供username", '')
        result, msg = TicketBaseService.get_ticket_detail(ticket_id, username)

        if result is False:
            code, data = 0, ''
        else:
            code, data = 1, result
        return api_response(code, msg, data)
Exemple #9
0
    def get(self, request, *args, **kwargs):
        """
        获取工单列表
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        request_data = request.GET
        username = request.user.username
        sn = request_data.get('sn', '')
        title = request_data.get('title', '')
        create_start = request_data.get('create_start', '')
        create_end = request_data.get('create_end', '')
        reverse = int(request_data.get('reverse', 1))
        per_page = int(request_data.get('per_page', 10))
        page = int(request_data.get('page', 1))
        workflow_id = request_data.get('workflow_id', '')
        category = request_data.get('category', 'duty')

        ticket_result_restful_list, msg = TicketBaseService.get_ticket_list(
            sn=sn,
            title=title,
            username=username,
            create_start=create_start,
            create_end=create_end,
            workflow_id=workflow_id,
            reverse=reverse,
            per_page=per_page,
            page=page,
            category=category)
        data = {}
        if ticket_result_restful_list is False:
            code, msg = 0, msg
        else:
            data = dict(value=ticket_result_restful_list,
                        per_page=msg['per_page'],
                        page=msg['page'],
                        total=msg['total'])
            code, msg = 1, ''
        return api_response(code, msg, data)
Exemple #10
0
 def post(self, request, *args, **kwargs):
     """
     接单操作
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     # json_str = request.body.decode('utf-8')
     request_data_dict = request.POST.dict()
     if not request_data_dict:
         return api_response(0, "post参数为空", {})
     # request_data_dict = json.loads(json_str)
     ticket_id = kwargs.get('pk', '')
     username = request.user.username
     result, msg = TicketBaseService.accept_ticket(ticket_id, username)
     if result:
         code, msg, data = 1, msg, result
     else:
         code, msg, data = 0, msg, ''
     return api_response(code, msg, data)
Exemple #11
0
 def post(self, request, *args, **kwargs):
     """
     加签处理完成,加签完成操作后工单处理人回回到之前加签发起人
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     # json_str = request.body.decode('utf-8')
     # request_data_dict = request.POST.dict()
     request_data_dict = json.loads(request.body.decode('utf-8'))
     if not request_data_dict:
         return api_response(0, "post参数为空", {})
     # request_data_dict = json.loads(json_str)
     ticket_id = kwargs.get('pk')
     username = request.user.username
     suggestion = request_data_dict.get('suggestion', '')
     result, msg = TicketBaseService.add_node_ticket_end(ticket_id, username, suggestion)
     if result:
         code, msg, data = 1, msg, result
     else:
         code, msg, data = 0, msg, ''
     return api_response(code, msg, data)
Exemple #12
0
    def post(self, request, *args, **kwargs):
        """
        新建工单
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        # json_str = request.body.decode('utf-8')
        request_data_dict = request.POST.dict()

        if not request_data_dict:
            return api_response(0, "post参数为空", {})
        request_data_dict['username'] = request.user.username

        new_ticket_result, msg = TicketBaseService.new_ticket(
            request_data_dict)

        if new_ticket_result:
            code, data = 1, {'ticket_id': new_ticket_result}
        else:
            code, data = 0, {}
        return api_response(code, msg, data)
Exemple #13
0
def run_flow_task(ticket_id, script_name, state_id, action_from='robot'):
    """
    执行工作流脚本
    :param script_name:
    :param ticket_id:
    :param state_id:
    :param action_from:
    :return:
    """
    ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=False).first()
    if ticket_obj.participant == script_name and ticket_obj.participant_type_id == \
            CONSTANT_SERVICE.PARTICIPANT_TYPE_ROBOT:

        # 校验脚本是否合法
        script_obj = WorkflowScript.objects.filter(saved_name='workflow_script/{}'.format(script_name),
                                                   is_deleted=False, is_active=True).first()
        if not script_obj:
            return False, '脚本未注册或非激活状态'

        # script_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "media/workflow_script"))
        script_dir = os.path.abspath(os.path.dirname(__file__))
        script_dir = os.path.abspath(os.path.join(os.path.split(os.path.split(script_dir)[0])[0], 'media/workflow_script'))
        script_file = os.path.join(script_dir, script_name)
        globals = {'ticket_id': ticket_id, 'action_from': action_from}
        # 如果需要脚本执行完成后,工单不往下流转(也就脚本执行失败或调用其他接口失败的情况),需要在脚本中抛出异常
        try:
            with stdout_io() as s:
                # execfile(script_file, globals)  # for python 2
                exec(open(script_file, encoding='utf-8').read(), globals)
            script_result = True
            # script_result_msg = ''.join(s.buflist)
            script_result_msg = ''.join(s.getvalue())
        except Exception as e:
            logger.error(traceback.format_exc())
            script_result = False
            script_result_msg = e.__str__()
            logger.info('*' * 20 + '工作流脚本回调,ticket_id:[%s]' % ticket_id + '*' * 20)
            logger.info('*******工作流脚本回调,ticket_id:{}*****'.format(ticket_id))

        # 因为上面的脚本执行时间可能会比较长,为了避免db session失效,重新获取ticket对象
        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=script_name, state_id=state_id, creator='robot')

        TicketBaseService.add_ticket_flow_log(new_ticket_flow_dict)
        if not script_result:
            # 脚本执行失败,状态不更新
            return False, script_result_msg
        # 自动执行流转
        destination_participant = None
        destination_participant_type_id = None
        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()
        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) == {ticket_obj.state_id}:
                    # 父工单的所有子工单都已处理结束,自动流转父工单
                    parent_ticket_obj = TicketRecord.objects.filter(id=ticket_obj.parent_ticket_id,
                                                                    is_deleted=False).first()
                    parent_transition_obj = Transition.objects.filter(source_state_id=parent_ticket_obj.state_id,
                                                                      is_deleted=False).first()
                    flag, msg = TicketBaseService.handle_ticket(parent_ticket_obj.id,
                                                                dict(username='******',
                                                                     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='yqpass')
        return True, ''
    else:
        return False, '工单当前处理人为非脚本,不执行脚本'