-
Notifications
You must be signed in to change notification settings - Fork 53
/
mail_utils.py
248 lines (210 loc) · 11.2 KB
/
mail_utils.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
import base64
import logging
from os import linesep
from django.conf import settings
from django.contrib.auth.models import User
from django.template.loader import get_template
from service_catalog import tasks
from service_catalog.models.request import RequestState
DEFAULT_FROM_EMAIL = f"{settings.SQUEST_EMAIL_HOST}"
EMAIL_TITLE_PREFIX = "[Squest]"
logger = logging.getLogger(__name__)
def _get_subject(target_object):
return f"{EMAIL_TITLE_PREFIX} {target_object.__class__.__name__} #{target_object.id} - {target_object}"
def _get_headers(subject):
email_id = str(base64.b64encode(subject.encode()))
headers = dict()
headers["Message-ID"] = email_id
headers["In-Reply-To"] = email_id
headers["References"] = email_id
return headers
def _exclude_user_without_email(user_qs):
if user_qs.filter(email='').exists():
logger.warning(f'The following users have no email:\n'
f'{linesep.join(f" - {user.username}" for user in user_qs.filter(email=""))}')
return user_qs.exclude(email='')
def _apply_when_filter_instance(user_qs, squest_object):
user_emails = list()
user_qs = _exclude_user_without_email(user_qs)
if user_qs.filter(profile__instance_notification_enabled=False).exists():
logger.info(f"The following users have disabled their instance notification. They won't be notified:"
f'{linesep.join(f" - {user.username}" for user in user_qs.filter(profile__instance_notification_enabled=False))}')
for user in user_qs.exclude(profile__instance_notification_enabled=False):
if user.profile.is_notification_authorized_for_instance(squest_object):
user_emails.append(user.email)
return user_emails
def _apply_when_filter_request(user_qs, squest_object):
user_emails = list()
user_qs = _exclude_user_without_email(user_qs)
if user_qs.filter(profile__request_notification_enabled=False).exists():
logger.info(f"The following users have disabled their request notification. They won't be notified:"
f'{linesep.join(user.username for user in user_qs.filter(profile__request_notification_enabled=False))}')
for user in user_qs.exclude(profile__request_notification_enabled=False):
if user.profile.is_notification_authorized_for_request(squest_object):
user_emails.append(user.email)
return user_emails
def _get_receivers_for_support_message(support_message):
# Apply when filter on all users
receivers_raw = support_message.who_has_perm("service_catalog.view_supportmessage").exclude(
id=support_message.sender.id)
return _apply_when_filter_instance(receivers_raw, support_message.support.instance)
def _get_receivers_for_request_message(request_message):
# Apply when filter on all users
receivers_raw = request_message.who_has_perm("service_catalog.view_requestmessage").exclude(
id=request_message.sender.id)
return _apply_when_filter_request(receivers_raw, request_message.request)
def _get_receivers_for_support(support):
# Apply when filter on all users
receivers_raw = support.who_has_perm("service_catalog.view_support")
return _apply_when_filter_instance(receivers_raw, support.instance)
def _get_receivers_for_request(squest_request):
# Apply when filter on all users
customer_raw = squest_request.who_has_perm("service_catalog.view_request")
admin_raw = squest_request.who_can_accept()
return _apply_when_filter_request(customer_raw | admin_raw, squest_request)
def send_mail_request_update(target_request, user_applied_state=None, message=None, receiver_email_list=None,
plain_text=None):
"""
Notify users that a request has been updated
:param message: A message to add to the email
:param user_applied_state: string email of the user who send the notification
:type user_applied_state: User
:param target_request:
:type target_request: service_catalog.models.request.Request
:param receiver_email_list: email diffusion list
:type receiver_email_list: List
:param plain_text: text displayed in the mail
:type plain_text: String
:return:
"""
if not settings.SQUEST_EMAIL_NOTIFICATION_ENABLED:
return
subject = _get_subject(target_request)
template_name = "service_catalog/mails/request_state_update.html"
context = {'request': target_request,
'user_applied_state': user_applied_state,
'current_site': settings.SQUEST_HOST,
'message': message}
if plain_text is None:
plain_text = f"Request state update: {target_request.state}"
if target_request.state == RequestState.SUBMITTED:
plain_text = f"Request update for service: {target_request.instance.name}"
html_template = get_template(template_name)
html_content = html_template.render(context)
if receiver_email_list is None:
receiver_email_list = _get_receivers_for_request(target_request)
headers = _get_headers(subject)
logger.debug(
f'Celery task: "send_email" will be send with subject: {subject}, plain_text: {plain_text}, html_template: '
f'{html_content}, from_email: {DEFAULT_FROM_EMAIL}, receivers: None, bcc: {receiver_email_list}, '
f'reply_to: None, headers: {headers}')
tasks.send_email.delay(subject, plain_text, html_content, DEFAULT_FROM_EMAIL,
bcc=receiver_email_list,
headers=headers)
def send_mail_new_support_message(message):
if not settings.SQUEST_EMAIL_NOTIFICATION_ENABLED:
return
subject = _get_subject(message.support)
template_name = "service_catalog/mails/support.html"
plain_text = f"New support message received on Instance #{message.support.instance.id} (#{message.support.id})"
context = {'message': message, 'current_site': settings.SQUEST_HOST}
html_template = get_template(template_name)
html_content = html_template.render(context)
receiver_email_list = _get_receivers_for_support_message(message)
headers = _get_headers(subject)
logger.debug(
f'Celery task: "send_email" will be send with subject: {subject}, plain_text: {plain_text}, html_template: '
f'{html_content}, from_email: {DEFAULT_FROM_EMAIL}, receivers: None, bcc: {receiver_email_list}, '
f'reply_to: None, headers: {headers}')
tasks.send_email.delay(subject, plain_text, html_content, DEFAULT_FROM_EMAIL,
bcc=receiver_email_list,
headers=_get_headers(subject))
def send_mail_support_is_closed(support):
if not settings.SQUEST_EMAIL_NOTIFICATION_ENABLED:
return
subject = _get_subject(support)
template_name = "service_catalog/mails/closed_support.html"
plain_text = f"Support closed on Instance #{support.instance.id} (#{support.id})"
context = {'support': support, 'current_site': settings.SQUEST_HOST}
html_template = get_template(template_name)
html_content = html_template.render(context)
receiver_email_list = _get_receivers_for_support(support)
headers = _get_headers(subject)
logger.debug(
f'Celery task: "send_email" will be send with subject: {subject}, plain_text: {plain_text}, html_template: '
f'{html_content}, from_email: {DEFAULT_FROM_EMAIL}, receivers: None, bcc: {receiver_email_list}, '
f'reply_to: None, headers: {headers}')
tasks.send_email.delay(subject, plain_text, html_content, DEFAULT_FROM_EMAIL,
bcc=receiver_email_list,
headers=headers)
def send_mail_new_comment_on_request(message):
if not settings.SQUEST_EMAIL_NOTIFICATION_ENABLED:
return
subject = _get_subject(message.request)
template_name = "service_catalog/mails/comment.html"
plain_text = f"New comment received on Request #{message.request.id}"
context = {'message': message, 'current_site': settings.SQUEST_HOST}
html_template = get_template(template_name)
html_content = html_template.render(context)
receiver_email_list = _get_receivers_for_request_message(message)
headers = _get_headers(subject)
logger.debug(
f'Celery task: "send_email" will be send with subject: {subject}, plain_text: {plain_text}, html_template: '
f'{html_content}, from_email: {DEFAULT_FROM_EMAIL}, receivers: None, bcc: {receiver_email_list}, '
f'reply_to: None, headers: {headers}')
tasks.send_email.delay(subject, plain_text, html_content, DEFAULT_FROM_EMAIL,
bcc=receiver_email_list,
headers=headers)
def send_email_request_canceled(target_request, user_applied_state=None):
"""
:param target_request: Request model
:type target_request: service_catalog.models.request.Request
:param user_applied_state: user who called this method
:type user_applied_state: User
:param request_owner_user: user owner of the Request
:type request_owner_user: User
:return:
"""
if not settings.SQUEST_EMAIL_NOTIFICATION_ENABLED:
return
subject = _get_subject(target_request)
plain_text = f"Request #{target_request.id} - CANCELLED"
template_name = "service_catalog/mails/request_cancelled.html"
context = {'request_id': target_request.id,
'user_applied_state': user_applied_state,
'current_site': settings.SQUEST_HOST}
html_template = get_template(template_name)
html_content = html_template.render(context)
receiver_email_list = _get_receivers_for_request(target_request)
headers = _get_headers(subject)
logger.debug(
f'Celery task: "send_email" will be send with subject: {subject}, plain_text: {plain_text}, html_template: '
f'{html_content}, from_email: {DEFAULT_FROM_EMAIL}, receivers: None, bcc: {receiver_email_list}, '
f'reply_to: None, headers: {headers}')
tasks.send_email.delay(subject, plain_text, html_content, DEFAULT_FROM_EMAIL,
bcc=receiver_email_list,
headers=headers)
def send_template_email(email_template, user_id_list):
if not settings.SQUEST_EMAIL_NOTIFICATION_ENABLED:
logger.debug("SQUEST_EMAIL_NOTIFICATION_ENABLED false. No email will be sent")
return
user_emails = list()
user_qs = User.objects.filter(id__in=user_id_list)
user_qs = _exclude_user_without_email(user_qs)
for user in user_qs.exclude(profile__request_notification_enabled=False):
user_emails.append(user.email)
subject = f"{EMAIL_TITLE_PREFIX} {email_template.email_title}"
plain_text = f"Squest notification"
template_name = "service_catalog/mails/email_notification_template.html"
context = {'email_template': email_template,
'current_site': settings.SQUEST_HOST}
html_template = get_template(template_name)
html_content = html_template.render(context)
headers = _get_headers(subject)
logger.debug(
f'Celery task: "send_email" will be send with subject: {subject}, plain_text: {plain_text}, html_template: '
f'{html_content}, from_email: {DEFAULT_FROM_EMAIL}, receivers: None, bcc: {user_emails}, reply_to: None, '
f'headers: {headers}')
tasks.send_email.delay(subject, plain_text, html_content, DEFAULT_FROM_EMAIL,
bcc=user_emails,
headers=headers)