예제 #1
0
    def agreed(self):

        '''

        :argument 将执行的结果通过站内信,email,dingding 发送

        :param   self.from_user
                 self.to_user
                 self.title
                 self.order
                 self.addr_ip

        :return: none

        '''

        Usermessage.objects.get_or_create(
            from_user=self.from_user, time=util.date(),
            title=self.title, content='该工单已审核通过!', to_user=self.to_user,
            state='unread'
        )

        # content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        # mail = Account.objects.filter(username=self.to_user).first()
        # tag = globalpermissions.objects.filter(authorization='global').first()
        try:
            util.dingding(
                '# 【<font face=\"微软雅黑\">工单执行通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **发起人员:**  <font color=\"#000080\">%s</font><br /> \n \n  **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n **平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#38C759\">已同意</font><br />'
                % (self.order.workid, self.order.username, self.order.assigned, self.addr_ip, self.order.remark),
                url=dingding_url)
        except Exception as e:
            print(e)
            CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
예제 #2
0
 def push(self):
     content = DatabaseList.objects.filter(
         id=self._tmpData['bundle_id']).first()
     mail = Account.objects.filter(username=self.to_user).first()
     tag = globalpermissions.objects.filter(authorization='global').first()
     if tag is None or tag.dingding == 0:
         pass
     else:
         try:
             if content.url:
                 util.dingding(
                     content='工单驳回通知\n工单编号:%s\n发起人:%s\n地址:%s\n驳回说明:%s\n状态:驳回'
                     % (self._tmpData['work_id'], self.to_user,
                        self.addr_ip, self.text),
                     url=content.url)
         except Exception as e:
             CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
     if tag is None or tag.email == 0:
         pass
     else:
         try:
             if mail.email:
                 mess_info = {
                     'workid': self._tmpData['work_id'],
                     'to_user': self.to_user,
                     'addr': self.addr_ip,
                     'rejected': self.text
                 }
                 put_mess = send_email.send_email(to_addr=mail.email)
                 put_mess.send_mail(mail_data=mess_info, type=1)
         except Exception as e:
             CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #3
0
 def post(self, request, args=None):
     try:
         data = json.loads(request.data['data'])
         tmp = json.loads(request.data['sql'])
         user = request.data['user']
         type = request.data['type']
         id = request.data['id']
     except KeyError as e:
         CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
         return HttpResponse(status=500)
     else:
         try:
             x = [x.rstrip(';') for x in tmp]
             sql = ';'.join(x)
             sql = sql.strip(' ').rstrip(';')
             workId = util.workId()
             SqlOrder.objects.get_or_create(
                 username=user,
                 date=util.date(),
                 work_id=workId,
                 status=2,
                 basename=data['basename'],
                 sql=sql,
                 type=type,
                 text=data['text'],
                 backup=data['backup'],
                 bundle_id=id
                 )
             content = DatabaseList.objects.filter(id=id).first()
             if content.url:
                 util.dingding(content='工单提交通知\n' + content.before, url=content.url)
             return Response('已提交,请等待管理员审核!')
         except Exception as e:
             CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
             return HttpResponse(status=500)
예제 #4
0
 def submit(self):
     content = DatabaseList.objects.filter(id=self.id).first()
     mail = Account.objects.filter(username=self.assigned).first()
     tag = globalpermissions.objects.filter(authorization='global').first()
     if tag is None or tag.dingding == 0:
         pass
     else:
         if content.url:
             try:
                 util.dingding(
                     content=
                     '工单提交通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单说明:%s\n状态:已提交\n备注:%s'
                     % (self.workId, self.user, self.addr_ip, self.text,
                        content.before),
                     url=content.url)
             except Exception as e:
                 CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
     if tag is None or tag.email == 0:
         pass
     else:
         if mail.email:
             mess_info = {
                 'workid': self.workId,
                 'to_user': self.user,
                 'addr': self.addr_ip,
                 'text': self.text,
                 'note': content.before
             }
             try:
                 put_mess = send_email.send_email(to_addr=mail.email)
                 put_mess.send_mail(mail_data=mess_info, type=2)
             except Exception as e:
                 CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #5
0
    def con_close(self):

        content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()

        if tag.message['ding']:
            try:
                util.dingding(
                    content='工单执行通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单备注:%s\n状态:已执行\n备注:%s'
                            % (
                                self.order.work_id, self.order.username, self.addr_ip, self.order.text,
                                content.after),
                    url=ding_url())
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')

        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self.order.work_id,
                        'to_user': self.order.username,
                        'addr': self.addr_ip,
                        'text': self.order.text,
                        'note': content.after}
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=0)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #6
0
def push_message(message=None,
                 type=None,
                 user=None,
                 to_addr=None,
                 work_id=None,
                 status=None):
    try:
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['mail']:
            try:
                put_mess = send_email.send_email(to_addr=to_addr)
                put_mess.send_mail(mail_data=message, type=type)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')

        if tag.message['ding']:
            un_init = util.init_conf()
            webhook = ast.literal_eval(un_init['message'])
            util.dingding(
                content=
                '# <font face=\"微软雅黑\">工单提交通知</font> #  \n <br>  \n  **工单编号:**  %s \n  \n  **提交人员:**  <font color=\"#000080\">%s</font><br /> \n  \n **状态:**  <font color=\"#FF9900\">%s</font><br /> \n'
                % (work_id, user, status),
                url=webhook['webhook'])
    except Exception as e:
        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
예제 #7
0
    def con_close(self):

        content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()

        if tag.message['ding']:
            try:
                util.dingding(
                    content=
                    '# <font face=\"微软雅黑\">工单执行通知</font> \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **发起人员:**  <font color=\"#000080\">%s</font><br /> \n \n  **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n **平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#38C759\">已执行</font><br /> \n \n **备注:**  %s \n '
                    % (self.order.work_id, self.order.username, self.from_user,
                       self.addr_ip, self.order.text, content.after),
                    url=ding_url())
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')

        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self.order.work_id,
                        'to_user': self.order.username,
                        'addr': self.addr_ip,
                        'text': self.order.text,
                        'note': content.after
                    }
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=0)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #8
0
    def execute(self):

        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self._tmpData
                self.addr_ip
                self.text
                self.to_user

        :return: none

        '''
        # content = DatabaseList.objects.filter(id=self._tmpData['bundle_id']).first()
        # mail = Account.objects.filter(username=self.to_user).first()
        # tag = globalpermissions.objects.filter(authorization='global').first()
        # if tag is None or tag.dingding == 0:
        #     pass
        # else:
        try:
            # if content.url:
            util.dingding('# 【<font face=\"微软雅黑\">工单驳回通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **发起人员:**  <font color=\"#000080\">%s</font><br /> \n \n **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n **平台地址:**  http://%s \n  \n **驳回说明:**  %s \n \n **状态:**  <font color=\"#FF0000\">驳回</font>\n '
                                % (self._tmpData['workid'], self._tmpData['username'],self._tmpData['assigned'], self.addr_ip, self.text), url='https://oapi.dingtalk.com/robot/send?access_token=3c5909159e71b1472390a95b5100469aa5d88386962c100d63d0583401e7d90b')
        except Exception as e:
            print(e)
            CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
예제 #9
0
    def con_close(self):

        content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()

        if tag.message['ding']:
            try:
                util.dingding(
                    content=
                    '工单执行通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单备注:%s\n状态:已执行\n备注:%s' %
                    (self.order.work_id, self.order.username, self.addr_ip,
                     self.order.text, content.after),
                    url=ding_url())
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')

        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self.order.work_id,
                        'to_user': self.order.username,
                        'addr': self.addr_ip,
                        'text': self.order.text,
                        'note': content.after
                    }
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=0)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #10
0
    def agreed(self):

        '''

        :argument 将执行的结果通过站内信,email,dingding 发送

        :param   self.from_user
                 self.to_user
                 self.title
                 self.order
                 self.addr_ip

        :return: none

        '''

        Usermessage.objects.get_or_create(
            from_user=self.from_user, time=util.date(),
            title=self.title, content='该工单已审核通过!', to_user=self.to_user,
            state='unread'
        )

        content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()

        if tag is None or tag.dingding == 0:
            pass
        else:
            try:
                if content.url:
                    if self.statue == 'normal':
                        util.dingding('# 【<font face=\"微软雅黑\">工单执行通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **发起人员:**  <font color=\"#000080\">%s</font><br /> \n \n  **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n **平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#38C759\">已执行</font><br /> \n \n **执行结果:**  <font color=\"#38C759\">正常</font><br /> \n \n **业务备注:**  %s \n '
                                % (self.order.work_id, self.order.username, self.from_user,self.addr_ip, self.order.text, content.after), url=content.url)
                    else:
                        util.dingding(
                            '# 【<font face=\"微软雅黑\">工单执行通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **发起人员:**  <font color=\"#000080\">%s</font><br /> \n \n  **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n **平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#38C759\">已执行</font><br /> \n \n **执行结果:**  <font color=\"#38C759\">正常</font><br /> \n \n **业务备注:**  %s \n '
                            % (self.order.work_id, self.order.username, self.from_user, self.addr_ip, self.order.text,
                               content.after), url=content.url)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')

        if tag is None or tag.email == 0:
            pass
        else:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self.order.work_id,
                        'to_user': self.order.username,
                        'addr': self.addr_ip,
                        'text': self.order.text,
                        'note': content.after}
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=0)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #11
0
    def agreed(self):
        '''

        :argument 将执行的结果通过站内信,email,dingding 发送

        :param   self.from_user
                 self.to_user
                 self.title
                 self.order
                 self.addr_ip

        :return: none

        '''

        Usermessage.objects.get_or_create(from_user=self.from_user,
                                          time=util.date(),
                                          title=self.title,
                                          content='该工单已审核通过!',
                                          to_user=self.to_user,
                                          state='unread')

        content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()

        if tag is None or tag.dingding == 0:
            pass
        else:
            try:
                if content.url:
                    util.dingding(
                        content=
                        '工单执行通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单备注:%s\n状态:已执行\n备注:%s'
                        % (self.order.work_id, self.order.username,
                           self.addr_ip, self.order.text, content.after),
                        url=content.url)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')

        if tag is None or tag.email == 0:
            pass
        else:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self.order.work_id,
                        'to_user': self.order.username,
                        'addr': self.addr_ip,
                        'text': self.order.text,
                        'note': content.after
                    }
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=0)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #12
0
파일: serachsql.py 프로젝트: w0niu/Yearning
def push_message(message=None, type=None, user=None, to_addr=None, work_id=None, status=None):
    try:
        put_mess = send_email.send_email(to_addr=to_addr)
        put_mess.send_mail(mail_data=message, type=type)
    except Exception as e:
        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
    else:
        try:
            util.dingding(content='查询申请通知\n工单编号:%s\n发起人:%s\n状态:%s' % (work_id, user, status), url=WEBHOOK)
        except ValueError as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
예제 #13
0
    def put(self, request, args: str = None):

        try:
            if args == '1':  # ldap测试
                ldap = json.loads(request.data['ldap'])
                ldap_test = util.test_auth(
                    username=ldap['user'],
                    password=ldap['password'],
                    host=ldap['host'],
                    type=ldap['type'],
                    sc=ldap['sc'],
                    domain=ldap['domain'])
                if ldap_test:
                    return Response('ldap连接成功!')
                else:
                    return Response('ldap连接失败!')
            elif args == '2':
                ding = request.data['ding']
                util.dingding('SQL审核平台测试', ding)
                return Response('已发送测试消息,请在钉钉中查看')
            elif args == '4':
                # setting = globalpermissions.objects.filter(authorization='global').values('message').first()
                # user = {'mail':dict(setting.get('message')).get('fs_mail_test')}
                user = {'mail':request.data['mail']}
                if fs_send_msg('飞书消息 测试成功!',user):
                    return Response('已发送测试消息,请在飞书中查看')
                else:
                    return Response('飞书测试消息,发送失败!')
            else:
                mail = json.loads(request.data['mail'])
                import smtplib
                from email.utils import parseaddr, formataddr
                from email.mime.text import MIMEText
                from email.header import Header

                def _format_addr(s):
                    name, addr = parseaddr(s)
                    return formataddr((Header(name, 'utf-8').encode(), addr))

                msg = MIMEText('Yearning test Message!', 'plain', 'utf-8')
                msg['From'] = _format_addr('Yearning_Admin <%s>' % mail['user'])
                msg['Subject'] = Header('Yearning 消息推送测试', 'utf-8').encode()
                server = smtplib.SMTP(mail['smtp_host'], mail['smtp_port'])  # SMTP协议默认端口是25
                server.set_debuglevel(1)
                server.login(mail['user'], mail['password'])
                server.sendmail(mail['user'], [mail['to_user']], msg.as_string())
                server.quit()
                return Response('已发送测试邮件,请注意查收!')
        except Exception as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
            return HttpResponse(e)
예제 #14
0
    def put(self, request, args: str = None):

        try:
            if args == '1':  # ldap测试
                ldap = json.loads(request.data['ldap'])
                ldap_test = util.test_auth(url=ldap['url'],
                                           user=ldap['user'],
                                           password=ldap['password'])
                if ldap_test:
                    return Response('ldap连接成功!')
                else:
                    return Response('ldap连接失败!')
            elif args == '2':
                ding = request.data['ding']
                util.dingding('yearning webhook测试', ding)
                return Response('已发送测试消息,请在钉钉中查看')

            else:
                mail = json.loads(request.data['mail'])
                import smtplib
                from email.utils import parseaddr, formataddr
                from email.mime.text import MIMEText
                from email.header import Header

                def _format_addr(s):
                    name, addr = parseaddr(s)
                    return formataddr((Header(name, 'utf-8').encode(), addr))

                msg = MIMEText('SQLAudit Plaform test Message!', 'plain',
                               'utf-8')
                msg['From'] = _format_addr('SQLAudit_Admin <%s>' %
                                           mail['user'])
                msg['Subject'] = Header('SQLAudit 消息推送测试', 'utf-8').encode()
                msg['Cc'] = mail['user']
                if mail['ssl']:
                    server = smtplib.SMTP_SSL(
                        mail['smtp_host'], mail['smtp_port'])  # SMTP协议默认端口是25
                else:
                    server = smtplib.SMTP(mail['smtp_host'],
                                          mail['smtp_port'])  # SMTP协议默认端口是25
                server.set_debuglevel(1)
                server.login(mail['user'], mail['password'])
                server.sendmail(mail['user'], [mail['user'], mail['to_user']],
                                msg.as_string())
                server.quit()
                return Response('已发送测试邮件,请注意查收!')
        except Exception as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
            return HttpResponse(e)
예제 #15
0
    def submit(self):
        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self.workId
                self.user
                self.addr_ip
                self.text
                self.assigned
                self.id
        :return: none

        '''
        content = DatabaseList.objects.filter(id=self.id).first()
        mail = Account.objects.filter(username=self.assigned).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        try:
            util.dingding(
                '# 【<font face=\"微软雅黑\">工单提交通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **提交人员:**  <font color=\"#000080\">%s</font><br /> \n \n **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n**平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#FF9900\">已提交</font><br /> \n \n **业务备注:** 测试'
                % (self.workId, self.user, self.to_user, self.addr_ip, self.text), url='https://oapi.dingtalk.com/robot/send?access_token=3c5909159e71b1472390a95b5100469aa5d88386962c100d63d0583401e7d90b')
        except Exception as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        # if tag is None or tag.dingding == 0:
        #     pass
        # else:
        #     if content.url:
        #         try:
        #             util.dingding('# 【<font face=\"微软雅黑\">工单提交通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **提交人员:**  <font color=\"#000080\">%s</font><br /> \n \n **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n**平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#FF9900\">已提交</font><br /> \n \n **业务备注:**  %s \n '
        #                         % (self.workId, self.user, self.to_user,self.addr_ip, self.text, content.before), url=content.url)
        #         except Exception as e:
        #             CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag is None or tag.email == 0:
            pass
        else:
            if mail.email:
                mess_info = {
                    'workid': self.workId,
                    'to_user': self.user,
                    'addr': self.addr_ip,
                    'text': self.text,
                    'note': content.before}
                try:
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=2)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #16
0
파일: task.py 프로젝트: gaotuan/SQL-Manager
    def submit(self):
        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self.workId
                self.user
                self.addr_ip
                self.text
                self.assigned
                self.id
        :return: none

        '''
        content = DatabaseList.objects.filter(id=self.id).first()
        mail = Account.objects.filter(username=self.assigned).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['ding']:
            if content.url:
                try:
                    util.dingding(
                        content='工单转发通知\n工单编号:%s\n发起人:%s\n当前审批人:%s\n地址:%s\n工单说明:%s\n状态:已提交\n备注:%s'
                                % (self.workId, self.user,self.assigned, self.addr_ip, self.text, content.before), url=content.url)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['feishu']:
            try:
                user_mail = Account.objects.filter(username=self.assigned).values('email').first()
                user = {'mail': user_mail.get('email')}
                fs_send_msg(
                    msg='工单转发通知\n工单编号:%s\n发起人:%s\n当前审批人:%s\n地址:%s\n工单说明:%s\n状态:已提交\n备注:%s'
                                % (self.workId, self.user,self.assigned, self.addr_ip, self.text, content.before),user=user)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--飞书推送失败: {e}')
        if tag.message['mail']:
            if mail.email:
                mess_info = {
                    'workid': self.workId,
                    'to_user': self.user,
                    'addr': self.addr_ip,
                    'text': self.text,
                    'note': content.before}
                try:
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=99)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #17
0
    def put(self, request, args: str = None):

        try:
            if args == '1':  # ldap测试
                ldap = json.loads(request.data['ldap'])
                ldap_test = util.test_auth(
                    username=ldap['user'],
                    password=ldap['password'],
                    host=ldap['host'],
                    type=ldap['type'],
                    sc=ldap['sc'],
                    domain=ldap['domain'],
                    ou=ldap['ou'])
                if ldap_test:
                    return Response('ldap连接成功!')
                else:
                    return Response('ldap连接失败!')
            elif args == '2':
                ding = request.data['ding']
                util.dingding('yearning webhook测试', ding)
                return Response('已发送测试消息,请在钉钉中查看')

            else:
                mail = json.loads(request.data['mail'])
                import smtplib
                from email.utils import parseaddr, formataddr
                from email.mime.text import MIMEText
                from email.header import Header

                def _format_addr(s):
                    name, addr = parseaddr(s)
                    return formataddr((Header(name, 'utf-8').encode(), addr))
                msg = MIMEText('Yearning test Message!', 'plain', 'utf-8')
                msg['From'] = _format_addr('Yearning_Admin <%s>' % mail['user'])
                msg['Subject'] = Header('Yearning 消息推送测试', 'utf-8').encode()
                if mail['ssl']:
                    server = smtplib.SMTP_SSL(mail['smtp_host'], mail['smtp_port'])  # SMTP协议默认端口是25
                else:
                    server = smtplib.SMTP(mail['smtp_host'], mail['smtp_port'])  # SMTP协议默认端口是25
                server.set_debuglevel(1)
                server.login(mail['user'], mail['password'])
                server.sendmail(mail['user'], [mail['to_user']], msg.as_string())
                server.quit()
                return Response('已发送测试邮件,请注意查收!')
        except Exception as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
            return HttpResponse(e)
예제 #18
0
파일: task.py 프로젝트: gaotuan/SQL-Manager
    def execute(self):

        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self._tmpData
                self.addr_ip
                self.text
                self.to_user

        :return: none

        '''
        content = DatabaseList.objects.filter(id=self._tmpData['bundle_id']).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['ding']:
            try:
                if content.url:
                    util.dingding(
                        content='工单驳回通知\n工单编号:%s\n发起人:%s\n操作人:%s\n地址:%s\n驳回说明:%s\n状态:驳回'
                                % (self._tmpData['work_id'], self.to_user,self.from_user, self.addr_ip, self.text), url=content.url)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['feishu']:
            try:
                user_mail = Account.objects.filter(username=self.to_user).values('email').first()
                user = {'mail': user_mail.get('email')}
                fs_send_msg(
                    msg='工单驳回通知\n工单编号:%s\n发起人:%s\n操作人:%s\n地址:%s\n驳回说明:%s\n状态:驳回'
                            % (self._tmpData['work_id'], self.to_user,self.from_user, self.addr_ip, self.text),user=user)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--飞书推送失败: {e}')
        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self._tmpData['work_id'],
                        'to_user': self.to_user,
                        'addr': self.addr_ip,
                        'rejected': self.text}
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=1)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #19
0
def push_message(message=None, type=None, user=None, to_addr=None, work_id=None, status=None):
    try:
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['mail']:
            put_mess = send_email.send_email(to_addr=to_addr)
            put_mess.send_mail(mail_data=message, type=type)
    except Exception as e:
        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
    else:
        try:
            if tag.message['ding']:
                un_init = util.init_conf()
                webhook = ast.literal_eval(un_init['message'])
                util.dingding(content='查询申请通知\n工单编号:%s\n发起人:%s\n状态:%s' % (work_id, user, status),
                              url=webhook['webhook'])
        except ValueError as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
예제 #20
0
def push_message(message=None, type=None, user=None, to_addr=None, work_id=None, status=None):
    try:
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['mail']:
            put_mess = send_email.send_email(to_addr=to_addr, ssl=tag.message['ssl'])
            put_mess.send_mail(mail_data=message, type=type)
    except Exception as e:
        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
    else:
        try:
            if tag.message['ding']:
                un_init = util.init_conf()
                webhook = ast.literal_eval(un_init['message'])
                util.dingding(content='权限申请通知\n工单编号:%s\n发起人:%s\n状态:%s' % (work_id, user, status),
                              url=webhook['webhook'])
        except ValueError as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
예제 #21
0
파일: task.py 프로젝트: gaotuan/SQL-Manager
    def con_close(self):

        Usermessage.objects.get_or_create(
            from_user=self.from_user, time=util.date(),
            title=self.title, content='该工单已审核通过!', to_user=self.to_user,
            state='unread'
        )

        content = DatabaseList.objects.filter(id=self.order.bundle_id).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()

        if tag.message['ding']:
            try:
                if content.url:
                    util.dingding(
                        content='工单执行通知\n工单编号:%s\n发起人:%s\n审核人:%s\n地址:%s\n工单备注:%s\n状态:已执行\n备注:%s'
                                % (
                                self.order.work_id, self.order.username,self.order.assigned, self.addr_ip, self.order.text, content.after),
                        url=content.url)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['feishu']:
            try:
                user_mail = Account.objects.filter(username=self.order.username).values('email').first()
                user = {'mail': user_mail.get('email')}
                fs_send_msg(
                    msg='工单执行通知\n工单编号:%s\n发起人:%s\n审核人:%s\n地址:%s\n工单备注:%s\n状态:已执行\n备注:%s'
                                % (self.order.work_id, self.order.username,self.order.assigned, self.addr_ip, self.order.text, content.after),user=user)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--飞书推送失败: {e}')

        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self.order.work_id,
                        'to_user': self.order.username,
                        'addr': self.addr_ip,
                        'text': self.order.text,
                        'note': content.after}
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=0)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #22
0
    def submit(self):
        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self.workId
                self.user
                self.addr_ip
                self.text
                self.assigned
                self.id
        :return: none

        '''
        content = DatabaseList.objects.filter(id=self.id).first()
        mail = Account.objects.filter(username=self.assigned).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['ding']:
            try:
                util.dingding(
                    '# <font face=\"微软雅黑\">工单提交通知</font> #  \n <br>  \n  **工单编号:**  %s \n  \n  **提交人员:**  <font color=\"#000080\">%s</font><br /> \n \n **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n**平台地址:**  http://%s \n  \n **环境:**  %s \n \n **服务:**  %s \n \n **迭代版本:**  %s \n \n **工单说明:**  %s \n \n **状态:**  <font color=\"#FF9900\">已提交</font><br /> \n \n **备注:**  %s \n '
                    % (self.workId, self.user, self.assigned, self.addr_ip,
                       self.env, self.service, self.version, self.text,
                       content.before),
                    url=ding_url())
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['mail']:
            if mail.email:
                mess_info = {
                    'workid': self.workId,
                    'to_user': self.user,
                    'addr': self.addr_ip,
                    'text': self.text,
                    'note': content.before
                }
                try:
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=99)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #23
0
    def submit(self):
        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self.workId
                self.user
                self.addr_ip
                self.text
                self.assigned
                self.id
        :return: none

        '''
        content = DatabaseList.objects.filter(id=self.id).first()
        mail = Account.objects.filter(username=self.assigned).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag is None or tag.dingding == 0:
            pass
        else:
            if content.url:
                try:
                    util.dingding('# 【<font face=\"微软雅黑\">工单提交通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **提交人员:**  <font color=\"#000080\">%s</font><br /> \n \n **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n**平台地址:**  http://%s \n  \n **工单备注:**  %s \n \n **执行状态:**  <font color=\"#38C759\">已提交</font><br /> \n \n **执行状态:**  <font color=\"#38C759\">已提交</font><br /> \n \n **业务备注:**  %s \n '
                                % (self.workId, self.user, self.to_user,self.addr_ip, self.text, content.before), url=content.url)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag is None or tag.email == 0:
            pass
        else:
            if mail.email:
                mess_info = {
                    'workid': self.workId,
                    'to_user': self.user,
                    'addr': self.addr_ip,
                    'text': self.text,
                    'note': content.before}
                try:
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=2)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #24
0
    def execute(self):

        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self._tmpData
                self.addr_ip
                self.text
                self.to_user

        :return: none

        '''
        content = DatabaseList.objects.filter(id=self._tmpData['bundle_id']).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag is None or tag.dingding == 0:
            pass
        else:
            try:
                if content.url:
                    util.dingding('# 【<font face=\"微软雅黑\">工单驳回通知</font>】 \n #  \n <br>  \n  **工单编号:**  %s \n  \n  **发起人员:**  <font color=\"#000080\">%s</font><br /> \n \n **审核人员:**  <font color=\"#000080\">%s</font><br /> \n \n **平台地址:**  http://%s \n  \n **驳回说明:**  %s \n \n **状态:**  驳回 \n "'
                                % (self._tmpData['work_id'], self.to_user,self.from_user, self.addr_ip, self.text), url=content.url)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag is None or tag.email == 0:
            pass
        else:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self._tmpData['work_id'],
                        'to_user': self.to_user,
                        'addr': self.addr_ip,
                        'rejected': self.text}
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=1)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #25
0
    def submit(self):
        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self.workId
                self.user
                self.addr_ip
                self.text
                self.assigned
                self.id
        :return: none

        '''
        content = DatabaseList.objects.filter(id=self.id).first()
        mail = Account.objects.filter(username=self.assigned).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['ding']:
            if content.url:
                try:
                    util.dingding(
                        content='工单提交通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单说明:%s\n状态:已提交\n备注:%s'
                                % (self.workId, self.user, self.addr_ip, self.text, content.before), url=content.url)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['mail']:
            if mail.email:
                mess_info = {
                    'workid': self.workId,
                    'to_user': self.user,
                    'addr': self.addr_ip,
                    'text': self.text,
                    'note': content.before}
                try:
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=99)
                except Exception as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #26
0
    def execute(self):

        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self._tmpData
                self.addr_ip
                self.text
                self.to_user

        :return: none

        '''
        content = DatabaseList.objects.filter(id=self._tmpData['bundle_id']).first()
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['ding']:
            try:
                if content.url:
                    util.dingding(
                        content='工单驳回通知\n工单编号:%s\n发起人:%s\n地址:%s\n驳回说明:%s\n状态:驳回'
                                % (self._tmpData['work_id'], self.to_user, self.addr_ip, self.text), url=content.url)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self._tmpData['work_id'],
                        'to_user': self.to_user,
                        'addr': self.addr_ip,
                        'rejected': self.text}
                    put_mess = send_email.send_email(to_addr=mail.email)
                    put_mess.send_mail(mail_data=mess_info, type=1)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #27
0
파일: task.py 프로젝트: dbaops-com/ypp_sql
    def execute(self):

        '''

        :argument 更改该工单SqlOrder表中的status

        :param
                self._tmpData
                self.addr_ip
                self.text
                self.to_user

        :return: none

        '''
        mail = Account.objects.filter(username=self.to_user).first()
        tag = globalpermissions.objects.filter(authorization='global').first()
        if tag.message['ding']:
            try:
                util.dingding(
                    content='工单驳回通知\n工单编号:%s\n发起人:%s\n地址:%s\n驳回说明:%s\n状态:驳回'
                            % (self._tmpData['work_id'], self.to_user, self.addr_ip, self.text), url=ding_url())
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--钉钉推送失败: {e}')
        if tag.message['mail']:
            try:
                if mail.email:
                    mess_info = {
                        'workid': self._tmpData['work_id'],
                        'to_user': self.to_user,
                        'addr': self.addr_ip,
                        'rejected': self.text}
                    put_mess = send_email.send_email(to_addr=mail.email, ssl=tag.message['ssl'])
                    put_mess.send_mail(mail_data=mess_info, type=1)
            except Exception as e:
                CUSTOM_ERROR.error(f'{e.__class__.__name__}--邮箱推送失败: {e}')
예제 #28
0
    def put(self, request, args=None):
        try:
            type = request.data['type']
        except KeyError as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
        else:
            if type == 0:
                try:
                    from_user = request.data['from_user']
                    to_user = request.data['to_user']
                    text = request.data['text']
                    id = request.data['id']
                except KeyError as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                    return HttpResponse(status=500)
                else:
                    try:
                        SqlOrder.objects.filter(id=id).update(status=0)
                        _tmpData = SqlOrder.objects.filter(id=id).values(
                            'work_id', 'bundle_id').first()
                        title = '工单:' + _tmpData['work_id'] + '驳回通知'
                        Usermessage.objects.get_or_create(from_user=from_user,
                                                          time=util.date(),
                                                          title=title,
                                                          content=text,
                                                          to_user=to_user,
                                                          state='unread')
                        content = DatabaseList.objects.filter(
                            id=_tmpData['bundle_id']).first()
                        mail = Account.objects.filter(username=to_user).first()
                        tag = globalpermissions.objects.filter(
                            authorization='global').first()
                        ret_info = '操作成功,该请求已驳回!'
                        if tag is None or tag.dingding == 0:
                            pass
                        else:
                            try:
                                if content.url:
                                    util.dingding(
                                        content=
                                        '工单驳回通知\n工单编号:%s\n发起人:%s\n地址:%s\n驳回说明:%s\n状态:驳回'
                                        % (_tmpData['work_id'], to_user,
                                           addr_ip, text),
                                        url=content.url)
                            except:
                                ret_info = '工单执行成功!但是钉钉推送失败,请查看错误日志排查错误.'
                        if tag is None or tag.email == 0:
                            pass
                        else:
                            try:
                                if mail.email:
                                    mess_info = {
                                        'workid': _tmpData['work_id'],
                                        'to_user': to_user,
                                        'addr': addr_ip,
                                        'rejected': text
                                    }
                                    put_mess = send_email.send_email(
                                        to_addr=mail.email)
                                    put_mess.send_mail(mail_data=mess_info,
                                                       type=1)
                            except:
                                ret_info = '工单执行成功!但是邮箱推送失败,请查看错误日志排查错误.'
                        return Response(ret_info)
                    except Exception as e:
                        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                        return HttpResponse(status=500)

            elif type == 1:
                try:
                    from_user = request.data['from_user']
                    to_user = request.data['to_user']
                    id = request.data['id']
                except KeyError as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                    return HttpResponse(status=500)
                else:
                    try:
                        SqlOrder.objects.filter(id=id).update(status=3)
                        c = SqlOrder.objects.filter(id=id).first()
                        title = f'工单:{c.work_id}审核通过通知'
                        '''

                        根据工单编号拿出对应sql的拆解数据

                        '''

                        SQL_LIST = DatabaseList.objects.filter(
                            id=c.bundle_id).first()
                        '''

                        发送sql语句到inception中执行

                        '''
                        with call_inception.Inception(
                                LoginDic={
                                    'host': SQL_LIST.ip,
                                    'user': SQL_LIST.username,
                                    'password': SQL_LIST.password,
                                    'db': c.basename,
                                    'port': SQL_LIST.port
                                }) as f:
                            res = f.Execute(sql=c.sql, backup=c.backup)
                            '''

                            修改该工单编号的state状态

                            '''
                            SqlOrder.objects.filter(id=id).update(status=1)
                            '''

                            遍历返回结果插入到执行记录表中

                            '''
                            for i in res:
                                SqlRecord.objects.get_or_create(
                                    date=util.date(),
                                    state=i['stagestatus'],
                                    sql=i['sql'],
                                    area=SQL_LIST.computer_room,
                                    name=SQL_LIST.connection_name,
                                    error=i['errormessage'],
                                    base=c.basename,
                                    workid=c.work_id,
                                    person=c.username,
                                    reviewer=c.assigned,
                                    affectrow=i['affected_rows'],
                                    sequence=i['sequence'],
                                    backup_dbname=i['backup_dbname'])
                        '''

                        通知消息

                        '''
                        Usermessage.objects.get_or_create(from_user=from_user,
                                                          time=util.date(),
                                                          title=title,
                                                          content='该工单已审核通过!',
                                                          to_user=to_user,
                                                          state='unread')
                        '''

                        Dingding

                        '''

                        content = DatabaseList.objects.filter(
                            id=c.bundle_id).first()
                        mail = Account.objects.filter(username=to_user).first()
                        tag = globalpermissions.objects.filter(
                            authorization='global').first()
                        ret_info = '操作成功,该请求已同意!并且已在相应库执行!详细执行信息请前往执行记录页面查看!'

                        if tag is None or tag.dingding == 0:
                            pass
                        else:
                            try:
                                if content.url:
                                    util.dingding(
                                        content=
                                        '工单执行通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单备注:%s\n状态:同意\n备注:%s'
                                        % (c.work_id, c.username, addr_ip,
                                           c.text, content.after),
                                        url=content.url)
                            except:
                                ret_info = '工单执行成功!但是钉钉推送失败,请查看错误日志排查错误.'

                        if tag is None or tag.email == 0:
                            pass
                        else:
                            try:
                                if mail.email:
                                    mess_info = {
                                        'workid': c.work_id,
                                        'to_user': c.username,
                                        'addr': addr_ip,
                                        'text': c.text,
                                        'note': content.after
                                    }
                                    put_mess = send_email.send_email(
                                        to_addr=mail.email)
                                    put_mess.send_mail(mail_data=mess_info,
                                                       type=0)
                            except:
                                ret_info = '工单执行成功!但是邮箱推送失败,请查看错误日志排查错误.'
                        return Response(ret_info)
                    except Exception as e:
                        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                        return HttpResponse(status=500)

            elif type == 'test':
                try:
                    base = request.data['base']
                    id = request.data['id']
                except KeyError as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                    return HttpResponse(status=500)
                else:
                    sql = SqlOrder.objects.filter(id=id).first()
                    data = DatabaseList.objects.filter(
                        id=sql.bundle_id).first()
                    info = {
                        'host': data.ip,
                        'user': data.username,
                        'password': data.password,
                        'db': base,
                        'port': data.port
                    }
                    try:
                        with call_inception.Inception(LoginDic=info) as test:
                            res = test.Check(sql=sql.sql)
                            return Response({'result': res, 'status': 200})
                    except Exception as e:
                        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                        return Response({'status': '500'})
예제 #29
0
    def put(self, request, args=None):
        try:
            type = request.data['type']
        except KeyError as e:
            CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
        else:
            if type == 0:
                try:
                    from_user = request.data['from_user']
                    to_user = request.data['to_user']
                    text = request.data['text']
                    id = request.data['id']
                except KeyError as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                    return HttpResponse(status=500)
                else:
                    try:
                        SqlOrder.objects.filter(id=id).update(status=0)
                        _tmpData = SqlOrder.objects.filter(id=id).values(
                            'work_id', 'bundle_id').first()
                        title = '工单:' + _tmpData['work_id'] + '驳回通知'
                        Usermessage.objects.get_or_create(from_user=from_user,
                                                          time=util.date(),
                                                          title=title,
                                                          content=text,
                                                          to_user=to_user,
                                                          state='unread')
                        content = DatabaseList.objects.filter(
                            id=_tmpData['bundle_id']).first()
                        if content.url:
                            util.dingding(content='工单驳回通知\n' + text,
                                          url=content.url)
                        return Response('操作成功,该请求已驳回!')
                    except Exception as e:
                        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                        return HttpResponse(status=500)

            elif type == 1:
                try:
                    from_user = request.data['from_user']
                    to_user = request.data['to_user']
                    id = request.data['id']
                except KeyError as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                    return HttpResponse(status=500)
                else:
                    try:
                        c = SqlOrder.objects.filter(id=id).first()
                        title = f'工单:{c.work_id}审核通过通知'
                        '''
                        
                        根据工单编号拿出对应sql的拆解数据
                        
                        '''

                        SQL_LIST = DatabaseList.objects.filter(
                            id=c.bundle_id).first()
                        '''
                        
                        发送sql语句到inception中执行
                        
                        '''
                        with call_inception.Inception(
                                LoginDic={
                                    'host': SQL_LIST.ip,
                                    'user': SQL_LIST.username,
                                    'password': SQL_LIST.password,
                                    'db': c.basename,
                                    'port': SQL_LIST.port
                                }) as f:
                            res = f.Execute(sql=c.sql, backup=c.backup)
                            '''
                            
                            遍历返回结果插入到执行记录表中
                            
                            '''
                            for i in res:
                                SqlRecord.objects.get_or_create(
                                    date=util.date(),
                                    state=i['stagestatus'],
                                    sql=i['sql'],
                                    area=SQL_LIST.computer_room,
                                    name=SQL_LIST.connection_name,
                                    error=i['errormessage'],
                                    base=c.basename,
                                    workid=c.work_id,
                                    person=c.username,
                                    reviewer=from_user,
                                    affectrow=i['affected_rows'],
                                    sequence=i['sequence'],
                                    backup_dbname=i['backup_dbname'])

                                if c.type == 0 and \
                                                i['errlevel'] == 0 and \
                                                i['sql'].find('use') == -1 and \
                                                i['stagestatus'] != 'Audit completed':
                                    data = testddl.AutomaticallyDDL(
                                        sql=" ".join(i['sql'].split()))
                                    if data['mode'] == 'edit':
                                        SqlDictionary.objects.filter(
                                            BaseName=data['BaseName'],
                                            TableName=data['TableName'],
                                            Field=data['Field']).update(
                                                Type=data['Type'],
                                                Null=data['Null'],
                                                Default=data['Default'])

                                    elif data['mode'] == 'add':
                                        SqlDictionary.objects.get_or_create(
                                            Type=data['Type'],
                                            Null=data['Null'],
                                            Default=data['Default'],
                                            Extra=data['COMMENT'],
                                            BaseName=data['BaseName'],
                                            TableName=data['TableName'],
                                            Field=data['Field'],
                                            TableComment='',
                                            Name=SQL_LIST.connection_name)

                                    elif data['mode'] == 'del':
                                        SqlDictionary.objects.filter(
                                            BaseName=data['BaseName'],
                                            TableName=data['TableName'],
                                            Field=data['Field'],
                                            Name=SQL_LIST.connection_name
                                        ).delete()
                        '''

                        修改该工单编号的state状态

                        '''
                        SqlOrder.objects.filter(id=id).update(status=1)
                        '''

                        通知消息

                        '''
                        Usermessage.objects.get_or_create(from_user=from_user,
                                                          time=util.date(),
                                                          title=title,
                                                          content='该工单已审核通过!',
                                                          to_user=to_user,
                                                          state='unread')
                        '''

                        Dingding

                        '''

                        content = DatabaseList.objects.filter(
                            id=c.bundle_id).first()
                        if content.url:
                            util.dingding(content='工单执行通知\n' + content.after,
                                          url=content.url)
                        return Response(
                            '操作成功,该请求已同意!并且已在相应库执行!详细执行信息请前往执行记录页面查看!')
                    except Exception as e:
                        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                        return HttpResponse(status=500)

            elif type == 'test':
                try:
                    base = request.data['base']
                    id = request.data['id']
                except KeyError as e:
                    CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                    return HttpResponse(status=500)
                else:
                    sql = SqlOrder.objects.filter(id=id).first()
                    data = DatabaseList.objects.filter(
                        id=sql.bundle_id).first()
                    info = {
                        'host': data.ip,
                        'user': data.username,
                        'password': data.password,
                        'db': base
                    }
                    try:
                        with call_inception.Inception(LoginDic=info) as test:
                            res = test.Check(sql=sql.sql)
                            return Response({'result': res, 'status': 200})
                    except Exception as e:
                        CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
                        return Response({'status': '500'})
예제 #30
0
 def post(self, request, args=None):
     try:
         data = json.loads(request.data['data'])
         tmp = json.loads(request.data['sql'])
         user = request.data['user']
         type = request.data['type']
         id = request.data['id']
     except KeyError as e:
         CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
         return HttpResponse(status=500)
     else:
         try:
             x = [x.rstrip(';') for x in tmp]
             sql = ';'.join(x)
             sql = sql.strip(' ').rstrip(';')
             workId = util.workId()
             SqlOrder.objects.get_or_create(username=user,
                                            date=util.date(),
                                            work_id=workId,
                                            status=2,
                                            basename=data['basename'],
                                            sql=sql,
                                            type=type,
                                            text=data['text'],
                                            backup=data['backup'],
                                            bundle_id=id,
                                            assigned=data['assigned'])
             content = DatabaseList.objects.filter(id=id).first()
             mail = Account.objects.filter(
                 username=data['assigned']).first()
             tag = globalpermissions.objects.filter(
                 authorization='global').first()
             ret_info = '已提交,请等待管理员审核!'
             if tag is None or tag.dingding == 0:
                 pass
             else:
                 if content.url:
                     try:
                         util.dingding(
                             content=
                             '工单提交通知\n工单编号:%s\n发起人:%s\n地址:%s\n工单说明:%s\n状态:已提交\n备注:%s'
                             % (workId, user, addr_ip, data['text'],
                                content.before),
                             url=content.url)
                     except:
                         ret_info = '工单执行成功!但是钉钉推送失败,请查看错误日志排查错误.'
             if tag is None or tag.email == 0:
                 pass
             else:
                 if mail.email:
                     mess_info = {
                         'workid': workId,
                         'to_user': user,
                         'addr': addr_ip,
                         'text': data['text'],
                         'note': content.before
                     }
                     try:
                         put_mess = send_email.send_email(
                             to_addr=mail.email)
                         put_mess.send_mail(mail_data=mess_info, type=2)
                     except:
                         ret_info = '工单执行成功!但是邮箱推送失败,请查看错误日志排查错误.'
             return Response(ret_info)
         except Exception as e:
             CUSTOM_ERROR.error(f'{e.__class__.__name__}: {e}')
             return HttpResponse(status=500)