def perform_destroy_user(instance, operator): subject = 'LDAP账号删除通知' content = '<br>{}({})的LDAP账号已由管理员({})删除!'.format(instance.username, instance.cname, operator.username) send_html_mail(settings.ADMIN_EMAIL, subject, content) if settings.LDAP_ENABLE: s = LdapTool() s.del_user(instance.username)
def workorder_task(workorder_id): instance = WorkOrder.objects.get(pk=workorder_id) cname = instance.cname creator = instance.creator exec_status = 1 script_path = instance.workflow.script.path if os.path.exists(script_path): log_file = '{}/logs/workflow/{}.log'.format(settings.BASE_DIR, workorder_id) cmd = 'python -u {} {} &>>{}'.format(script_path, workorder_id, log_file) local_cmd(cmd) with open(log_file) as file: log_content = file.read() m = re.search('task_mark_error', log_content, re.M) if m: exec_status = 2 m = re.search('task_mark_crontab', log_content, re.M) if m: exec_status = 3 instance.exec_log = log_content instance.exec_status = exec_status instance.save() tos = creator.email subject = '<{}>工单进度通知'.format(cname) content = '<br>您好!<br>{} 工单已处理,等待您确认,<a href="{}/workflow/workorder/{}/approval/" target="_blank">点击此处处理工单</a>,<a href="{}/workflow/task/{}/show/" target="_blank">点击此处查看工单详情</a>,谢谢!'.format( instance.cname, settings.PROJECT_URL, instance.id, settings.PROJECT_URL, instance.id) send_html_mail(tos, subject, content) return 'workorder task done'
def workorder_remind(): data = {'code': 0, 'message': 'Success'} rets = WorkOrder.objects.filter(cur_step__role__name='dba') for ret in rets: subject = '【工单处理提醒】如已处理请忽略此邮件' content = '<br>{}({})申请 {} 工单已发起,工单ID{}, 申请时间{}, 等待您处理,<a href="{}/#/workflow/workorderswaiting" target="_blank">点击处理工单</a>,谢谢!'.format( ret.creator.username, ret.creator.cname, ret.cname, ret.id, ret.create_time.strftime('%Y-%m-%d %H:%M'), settings.PROJECT_URL) mail_list = [ row.email for row in ret.cur_step.role.userprofile_set.all() ] send_html_mail(','.join(mail_list), subject, content) return data
def send_email_code(self, request): """ 发送邮箱验证码 """ serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.validated_data["email"] char_list = map(str, random_char_list(4)) code = ''.join(char_list) try: cache.set(email, code, 3 * 60) subject = content = '您的验证码是: %s ,该验证码有效期3分钟, 如非本人操作请忽略此邮件!' % code send_html_mail(email, subject, content) except Exception as e: return Response({"detail": e[0]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({"email": email}, status=status.HTTP_201_CREATED)
def perform_create_user(instance): password = make_password() instance.set_password(password) instance.save() subject = '运维云平台账号已开通,请登录自行修改密码' content = '<br>{},您好:<br><br> 您的账号: {} ,密码: {}<br> <a href="{}" target="_blank">运维云平台</a> 请登录自行修改密码并妥善保管,谢谢!'.format( instance.cname, instance.username, password, settings.PROJECT_URL) send_html_mail(instance.email, subject, content) if settings.LDAP_ENABLE: data = {} uid = instance.email.split('@')[0] data['uid'] = uid data['cn'] = uid data['sn'] = instance.cname data['telephonenumber'] = instance.phone data['mail'] = instance.email data['userPassword'] = '******' + make_crypt_password(password) s = LdapTool() s.add_user(data)
def email_reset_password(self, request): """ 通过邮箱重置密码 """ # todo: 此接口暂时不需要 serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.validated_data["email"] try: user_obj = User.objects.get(email=email) token_obj = Token() token = token_obj.generate_validate_token(email) reset_url = '{}/#/resetpwd?token={}'.format(os.environ.get('PROJECT_URL'), token) subject = '重置您的密码' content = '<br>{},您好:<br><br> 点击以下链接重置您的密码: {} <br><br>如果您没有请求重置密码,请忽略该邮件。'.format( user_obj.username, reset_url) send_html_mail(user_obj.email, subject, content) except Exception as e: return Response({'detail': ['邮箱不存在']}, status=status.HTTP_400_BAD_REQUEST) return Response({'status': 'ok'})
def workorder_notice(instance): '''工单通知''' workorder_detail = get_workorder_detail(instance) data = json.loads(instance.data) if instance.cur_user: tos = instance.cur_user.email cc = '' if 'cc' in data: cc = data['cc'] subject = '<{}>工单处理通知'.format(instance.cname) is_dingding = 0 if instance.status == 2: is_dingding = 1 content = '<br>{}({})申请 {} 工单已发起,等待您处理,<a href="{}/#/workflow/workorderswaiting" target="_blank">点击处理工单</a>,谢谢!'.format( instance.creator.username, instance.creator.cname, instance.cname, settings.PROJECT_URL) elif instance.status == 3: is_dingding = 1 content = '<br>{}({})申请 {} 工单上级审批人已审批,等待您处理,<a href="{}/#/workflow/workorderswaiting" target="_blank">点击处理工单</a>,谢谢!'.format( instance.creator.username, instance.creator.cname, instance.cname, settings.PROJECT_URL) elif instance.status == 6: content = '<br>{}({})申请 {} 工单申请人反馈未解决,等待您确认并处理,<a href="{}/#/workflow/workorderswaiting" target="_blank">点击处理工单</a>,谢谢!'.format( instance.creator.username, instance.creator.cname, instance.cname, settings.PROJECT_URL) elif instance.status == 0: tos = instance.creator.email content = '<br>{} 工单已驳回,<a href="{}/#/workflow/workorders" target="_blank">点击查看</a>,谢谢!'.format( instance.cname, settings.PROJECT_URL) else: return '' #丁丁通知 if settings.IS_DINGDING and is_dingding == 1: ding_cli = DingTalk() ding_cli.send_workorder_approval_message(instance, workorder_detail, instance.cur_user.email, instance.cur_user.id) #微信提醒 im = instance.cur_user.im if im: weixin_config = { 'WEIXIN_CORPID': settings.WEIXIN_CORPID, 'WEIXIN_SECRET': settings.WEIXIN_SECRET, 'WEIXIN_AGTID': settings.WEIXIN_AGTID, } weixin = Weixin(weixin_config) weixin.send_text(im, content.replace('<br>', '')) return send_html_mail(tos, subject, content, cc=cc)