-
Notifications
You must be signed in to change notification settings - Fork 199
/
msgNotice.py
273 lines (245 loc) · 11.1 KB
/
msgNotice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding:utf-8 -*-
# edit by fuzongfei
import json
from celery import shared_task
from dingtalkchatbot.chatbot import DingtalkChatbot
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.utils import timezone
from config.config import EMAIL, DINGDING
from opsql.settings import EMAIL_FROM, DOMAIN_NAME
from orders.models import Orders, OrderReply
from users.models import UserAccounts
def get_user_email(id):
obj = Orders.objects.get(pk=id)
# 获取申请人的邮箱地址
applicant_email = list(UserAccounts.objects.filter(username=obj.applicant).values_list('email', flat=True))
# 获取审核人的邮箱地址
auditor = [i['user'] for i in json.loads(obj.auditor)]
auditor_email = list(set(UserAccounts.objects.filter(username__in=auditor).values_list('email', flat=True)))
# 获取复核人的邮箱地址
reviewer = [i['user'] for i in json.loads(obj.reviewer)]
reviewer_email = list(set(UserAccounts.objects.filter(username__in=reviewer).values_list('email', flat=True)))
# 获取抄送人的邮箱地址
email_cc_email = list(
set(UserAccounts.objects.filter(username__in=obj.email_cc.split(',')).values_list('email', flat=True)))
return applicant_email, auditor_email, reviewer_email, email_cc_email
class MsgPush(object):
def __init__(self, id=None, user=None, type=None, msg=None):
"""
:param user: 用户
:param id: 记录id
:param type: ['commit', 'approve', 'feedback', 'hook', 'review, 'close', 'reply']
"""
self.user = user
self.id = id
self.type = type
self.msg = msg
# 消息推送里面的域名提示
self.domain_name_tips = DOMAIN_NAME['value']
def mail_notice(self):
if self.type == 'commit':
send_commit_mail.delay(id=self.id, domain_name_tips=self.domain_name_tips)
else:
send_op_mail.delay(id=self.id, user=self.user, type=self.type,
msg=self.msg, domain_name_tips=self.domain_name_tips)
def dingding_notice(self, webhook):
dingding_push.delay(id=self.id, user=self.user, type=self.type, webhook=webhook,
msg=self.msg, domain_name_tips=self.domain_name_tips)
def send(self):
# 判断是否启用了邮件通知
if EMAIL['enable'] is True:
self.mail_notice()
# 判断是否启用了钉钉通知
if DINGDING['enable'] is True:
webhook = DINGDING['webhook']
self.dingding_notice(webhook)
@shared_task
def send_commit_mail(id=None, domain_name_tips=None):
"""发送工单提交邮件"""
applicant_email, auditor_email, reviewer_email, email_cc_email = get_user_email(id)
# 收件人
receiver_email = list(set(applicant_email + auditor_email + reviewer_email))
# 向_commit_mail.html渲染data数据
data = Orders.objects.get(pk=id)
# 获取工单的审核人和复核人
auditor = ', '.join([x['user'] for x in json.loads(data.auditor)])
reviewer = ', '.join([x['user'] for x in json.loads(data.reviewer)])
email_html_body = render_to_string(
'mail/orders_commit_mail.html',
{
'data': data,
'auditor': auditor,
'reviewer': reviewer,
'domain_name_tips': domain_name_tips,
}
)
# 发送邮件
msg = EmailMessage(subject=data.title,
body=email_html_body,
from_email=EMAIL_FROM,
to=receiver_email,
cc=email_cc_email
)
msg.content_subtype = "html"
msg.send()
@shared_task
def send_op_mail(id, user=None, type=None, msg=None, domain_name_tips=None):
if type == 'reply':
data = OrderReply.objects.get(pk=id)
title = Orders.objects.get(pk=data.reply_id).title
# 工单id,用于查找收件人
order_id = data.reply_id
else:
data = Orders.objects.get(pk=id)
title = data.title
order_id = id
applicant_email, auditor_email, reviewer_email, email_cc_email = get_user_email(order_id)
# 收件人
receiver_email = list(set(applicant_email + auditor_email + reviewer_email))
# 向mail_template.html渲染data数据
email_html_body = render_to_string('mail/orders_op_mail.html', {
'data': data,
'type': type,
'domain_name_tips': domain_name_tips,
'msg': msg,
'username': user
})
# 发送邮件
headers = {'Reply: ': receiver_email}
title = 'Re: ' + title
msg = EmailMessage(subject=title,
body=email_html_body,
from_email=EMAIL_FROM,
to=receiver_email,
cc=email_cc_email,
reply_to=receiver_email,
headers=headers)
msg.content_subtype = "html"
msg.send()
@shared_task
def dingding_push(id, user=None, type=None, msg=None, webhook=None, domain_name_tips=None):
xiaoding = DingtalkChatbot(webhook)
if type == 'reply':
obj = OrderReply.objects.get(pk=id)
reply_contents = obj.reply_contents
data = Orders.objects.get(pk=obj.reply_id)
else:
# 获取数据
data = Orders.objects.get(pk=id)
# 如果用户手机号存在,钉钉直接@mobile
# 如果手机号不存在,钉钉直接@all
applicant_mobile = UserAccounts.objects.get(username=data.applicant).mobile
auditor = ','.join([i['user'] for i in json.loads(data.auditor)])
auditor_mobile = list(set(UserAccounts.objects.filter(username__in=auditor).values_list('mobile', flat=True)))
reviewer = ','.join([i['user'] for i in json.loads(data.reviewer)])
# 提交
if type == 'commit':
text = f"您好、{user}提交了审核内容,◕‿◕\n" \
f"标题: {data.title}\n" \
f"环境: {data.envi}\n" \
f"类型: {data.sql_type}\n" \
f"主机: {data.host}\n" \
f"端口: {data.port}\n" \
f"库名: {data.database}\n" \
f"审核人: {auditor}\n" \
f"复核人: {reviewer}\n" \
f"上线版本号: {data.version}\n" \
f"需求: {data.description}\n" \
f"URL: {domain_name_tips}/orders/detail/{data.envi_id}/ \n" \
f"提交时间: {timezone.localtime(data.created_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
if auditor_mobile:
xiaoding.send_text(msg=text, at_mobiles=auditor_mobile)
else:
xiaoding.send_text(msg=text, is_at_all=True)
# 审核
elif type == 'approve':
if data.progress == '2':
text = f"您好、{user}审核已通过,◕‿◕\n" \
f"标题: {data.title}\n" \
f"环境: {data.envi}\n" \
f"附加信息: {msg}\n" \
f"URL: {domain_name_tips}/orders/envi/{data.envi_id}/ \n" \
f"审核时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
elif data.progress == '1':
text = f"您好、{user}审核未通过,◕﹏◕\n" \
f"标题: {data.title}\n" \
f"环境: {data.envi}\n" \
f"附加信息: {msg}\n" \
f"URL: {domain_name_tips}/orders/envi/{data.envi_id}/ \n" \
f"审核时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
if applicant_mobile:
xiaoding.send_text(msg=text, at_mobiles=[applicant_mobile])
else:
xiaoding.send_text(msg=text, is_at_all=True)
# 反馈
elif type == 'feedback':
if data.progress == '3':
text = f"您好、{user}正在处理中,请稍后,◕‿◕\n" \
f"标题: {data.title}\n" \
f"环境: {data.envi}\n" \
f"附加信息: {msg}\n" \
f"URL: {domain_name_tips}/orders/envi/{data.envi_id}/ \n" \
f"处理时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
elif data.progress == '4':
text = f"您好、{user}处理完成,◕‿◕\n" \
f"标题: {data.title}\n" \
f"环境: {data.envi}\n" \
f"附加信息: {msg}\n" \
f"URL: {domain_name_tips}/orders/envi/{data.envi_id}/ \n" \
f"完成时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
if applicant_mobile and auditor_mobile:
xiaoding.send_text(msg=text, at_mobiles=[applicant_mobile, auditor_mobile])
else:
xiaoding.send_text(msg=text, is_at_all=True)
# 关闭
elif type == 'close':
if data.progress == '5':
text = f"您好、{user}关闭了记录,请不要处理,◕‿◕\n" \
f"标题: {data.title}\n" \
f"环境: {data.envi}\n" \
f"附加信息: {msg}\n" \
f"URL: {domain_name_tips}/orders/envi/{data.envi_id}/ \n" \
f"关闭时间: {timezone.localtime(data.close_time).strftime('%Y-%m-%d %H:%M:%S')}\n"
if applicant_mobile and auditor_mobile:
xiaoding.send_text(msg=text, at_mobiles=[applicant_mobile, auditor_mobile])
else:
xiaoding.send_text(msg=text, is_at_all=True)
# 回复
elif type == 'reply':
text = f"您好、{user}回复了内容,◕‿◕\n" \
f"标题: {data.title}\n" \
f"回复内容: {reply_contents}\n" \
f"URL: {domain_name_tips}/orders/detail/{data.pk}/ \n" \
f"回复时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
if applicant_mobile and auditor_mobile:
xiaoding.send_text(msg=text, at_mobiles=[applicant_mobile, auditor_mobile])
else:
xiaoding.send_text(msg=text, is_at_all=True)
# 复核
elif type == 'review':
text = f"您好、{user}已复核,◕‿◕\n" \
f"标题: {data.title}\n" \
f"附加信息: {msg}\n" \
f"复核时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
if applicant_mobile and auditor_mobile:
xiaoding.send_text(msg=text, at_mobiles=[applicant_mobile, auditor_mobile])
else:
xiaoding.send_text(msg=text, is_at_all=True)
# 钩子
elif type == 'hook':
text = f"您好、{user}扭转了工单,◕‿◕\n" \
f"标题: {data.title}\n" \
f"新环境: {data.envi}\n" \
f"类型: {data.sql_type}\n" \
f"主机: {data.host}\n" \
f"端口: {data.port}\n" \
f"库名: {data.database}\n" \
f"上线版本号: {data.version}\n" \
f"需求: {data.description}\n" \
f"URL: {domain_name_tips}/orders/envi/{data.envi_id}/ \n" \
f"扭转时间: {timezone.localtime(data.updated_at).strftime('%Y-%m-%d %H:%M:%S')}\n"
if applicant_mobile and auditor_mobile:
xiaoding.send_text(msg=text, at_mobiles=[applicant_mobile, auditor_mobile])
else:
xiaoding.send_text(msg=text, is_at_all=True)