-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
285 lines (248 loc) · 8.51 KB
/
logic.py
File metadata and controls
285 lines (248 loc) · 8.51 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
import json
from contextlib import nullcontext
import requests
from django.contrib import messages
from journal.models import Journal
from plugins.notification_manager.models import NotificationMessage
from utils import setting_handler
from utils.logger import get_logger
logger = get_logger(__name__)
def send_message(request, body):
"""
Sends a message with the given body.
:param request: The request when sending the event.
:param body: the body of the message to send.
"""
# get the per-journal settings for the plugin
(
notification_manager_enabled,
notification_manager_email,
notification_manager_password,
notification_manager_url,
notification_manager_authorization_url,
notification_manager_authenticate_send,
) = get_plugin_settings(request.journal)
message = NotificationMessage()
# setup switchboard option
url_to_use = notification_manager_url
if not url_to_use.endswith("/"):
url_to_use += "/"
token = None
message.authorized = False
if not notification_manager_authenticate_send:
# try authorization
token, success = authorize(notification_manager_email, notification_manager_password, notification_manager_authorization_url)
if not success:
message.save()
messages.add_message(
request,
messages.ERROR,
"Failed to authorize.",
)
return
message.authorized = True
payload = build_payload(body)
# send the payload
json_output, success = send_payload(payload, token, url_to_use)
message.message = payload
message.response = json_output
if success:
message.success = True
message.save()
messages.add_message(
request,
messages.SUCCESS,
"Message sent.",
)
return
message.success = False
message.save()
messages.add_message(
request,
messages.ERROR,
f"Failed to send the message: \
{[item for item in json_output.get('errorMessage', [])]}",
)
def authorize(email, password, url_to_use):
"""
Obtain a bearer token from the authorization service.
:param email: the email to use
:param password: the password to use
:param url_to_use: the base URL to use
:return:
"""
auth_url = f"{url_to_use}authorize"
authorization_json = build_authorization_json(email, password)
r = requests.post(
f"{url_to_use}authorize",
data=json.dumps(authorization_json),
timeout=30,
)
if r.status_code != 200:
logger.error(
f"Failed to authorize with the given URL: {auth_url}: "
f"{r.status_code}"
)
return None, False
authorization_response = r.json()
if "error" in authorization_response:
logger.error(
f"Failed to authorize: {auth_url}: "
f"{authorization_response['errorMessage']}"
)
return None, False
# get the token
if "token" not in authorization_response:
logger.error(
f"Failed to authorize: {auth_url}: "
"no token returned"
)
return None, False
token = authorization_response.get("token", None)
return token, True
def build_authorization_json(email, password):
"""
Build the authorization JSON
:param email: the email to use
:param password: the password to use
"""
return {
"email": email,
"password": password,
}
def send_payload(payload, token, url_to_use):
"""
Send the payload to the Notification Service.
:param payload: the payload to send
:param token: the bearer token to use
:param url_to_use: the base URL to use
"""
if token is not None:
headers = {"Authorization": "Bearer " + token}
message_url = f"{url_to_use}message"
r = requests.post(
message_url, headers=headers, data=json.dumps(payload), timeout=30
)
try:
json_output = r.json()
except Exception:
json_output = {"message": r.content}
is_errored = json_output.get("error", False)
if is_errored:
return json_output, False
return json_output, True
def build_payload(body):
"""
Build the payload to send.
:param body: The body of the payload.
"""
return {
"header": build_header(),
"data": body,
}
def build_header():
"""
Build the header for the payload.
"""
return {
}
def get_plugin_settings(journal: Journal):
"""
Get the plugin settings for the Notification Manager.
:param journal: the journal
"""
logger.debug("Fetching journal settings for the following journal: %s", journal.id)
notification_manager_enabled = setting_handler.get_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_send",
journal=journal,
default=False,
).processed_value
notification_manager_email = setting_handler.get_setting(
"plugin:notification_manager_plugin",
setting_name="notification_manager_email",
journal=journal
).processed_value
notification_manager_password = setting_handler.get_setting(
"plugin:notification_manager_plugin",
setting_name="notification_manager_password",
journal=journal
).processed_value
notification_manager_url = setting_handler.get_setting(
"plugin:notification_manager_plugin",
setting_name="notification_manager_url",
journal=journal
).processed_value
notification_manager_authorization_url = setting_handler.get_setting(
"plugin:notification_manager_plugin",
setting_name="notification_manager_authorization_url",
journal=journal
).processed_value
notification_manager_authenticate_send = setting_handler.get_setting(
"plugin:notification_manager_plugin",
setting_name="notification_manager_authenticate_send",
journal=journal
).processed_value
return (
notification_manager_enabled,
notification_manager_email,
notification_manager_password,
notification_manager_url,
notification_manager_authorization_url,
notification_manager_authenticate_send,
)
def save_plugin_settings(
notification_manager_enabled,
notification_manager_email,
notification_manager_password,
notification_manager_url,
notification_manager_authorization_url,
notification_manager_authenticate_send,
request,
):
"""
Save the plugin settings for the notification manager plugin
:param notification_manager_email: the email
:param notification_manager_enabled: whether the plugin is enabled
:param notification_manager_password: the password
:param notification_manager_authorization_url: the authentication URL
:param notification_manager_url: the live URL
:param notification_manager_authenticate_send: True if authenticated messages should be sent, false otherwise.
:param request: the request object (to specify the journal)
"""
setting_handler.save_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_send",
journal=request.journal,
value=notification_manager_enabled,
)
setting_handler.save_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_authenticate_send",
journal=request.journal,
value=notification_manager_authenticate_send,
)
setting_handler.save_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_email",
journal=request.journal,
value=notification_manager_email,
)
setting_handler.save_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_password",
journal=request.journal,
value=notification_manager_password,
)
setting_handler.save_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_url",
journal=request.journal,
value=notification_manager_url,
)
setting_handler.save_setting(
setting_group_name="plugin:notification_manager_plugin",
setting_name="notification_manager_authorization_url",
journal=request.journal,
value=notification_manager_authorization_url,
)