1
|
|
|
import falcon |
2
|
|
|
import simplejson as json |
3
|
|
|
import mysql.connector |
4
|
|
|
import config |
5
|
|
|
from datetime import datetime, timedelta, timezone |
6
|
|
|
from core.useractivity import user_logger |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class WebMessageCollection: |
10
|
|
|
@staticmethod |
11
|
|
|
def __init__(): |
12
|
|
|
""""Initializes WebMessageCollection""" |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp): |
17
|
|
|
resp.status = falcon.HTTP_200 |
18
|
|
|
|
19
|
|
|
@staticmethod |
20
|
|
|
def on_get(req, resp): |
21
|
|
|
|
22
|
|
|
start_datetime_local = req.params.get('startdatetime') |
23
|
|
|
end_datetime_local = req.params.get('enddatetime') |
24
|
|
|
|
25
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
26
|
|
|
if config.utc_offset[0] == '-': |
27
|
|
|
timezone_offset = -timezone_offset |
28
|
|
|
|
29
|
|
|
if start_datetime_local is None: |
30
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
31
|
|
|
description="API.INVALID_START_DATETIME_FORMAT") |
32
|
|
|
else: |
33
|
|
|
start_datetime_local = str.strip(start_datetime_local) |
34
|
|
|
try: |
35
|
|
|
start_datetime_utc = datetime.strptime(start_datetime_local, |
36
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
37
|
|
|
timedelta(minutes=timezone_offset) |
38
|
|
|
except ValueError: |
39
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
40
|
|
|
description="API.INVALID_START_DATETIME_FORMAT") |
41
|
|
|
|
42
|
|
|
if end_datetime_local is None: |
43
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
44
|
|
|
description="API.INVALID_END_DATETIME_FORMAT") |
45
|
|
|
else: |
46
|
|
|
end_datetime_local = str.strip(end_datetime_local) |
47
|
|
|
try: |
48
|
|
|
end_datetime_utc = datetime.strptime(end_datetime_local, |
49
|
|
|
'%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
50
|
|
|
timedelta(minutes=timezone_offset) |
51
|
|
|
except ValueError: |
52
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
53
|
|
|
description="API.INVALID_END_DATETIME_FORMAT") |
54
|
|
|
|
55
|
|
|
if start_datetime_utc >= end_datetime_utc: |
56
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
57
|
|
|
title='API.BAD_REQUEST', |
58
|
|
|
description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME') |
59
|
|
|
|
60
|
|
|
# Verify User Session |
61
|
|
|
token = req.headers.get('TOKEN') |
62
|
|
|
user_uuid = req.headers.get('USER-UUID') |
63
|
|
|
if token is None: |
64
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
65
|
|
|
description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
66
|
|
|
if user_uuid is None: |
67
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
68
|
|
|
description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
69
|
|
|
|
70
|
|
|
cnx = mysql.connector.connect(**config.myems_user_db) |
71
|
|
|
cursor = cnx.cursor(dictionary=True) |
72
|
|
|
|
73
|
|
|
query = (" SELECT utc_expires " |
74
|
|
|
" FROM tbl_sessions " |
75
|
|
|
" WHERE user_uuid = %s AND token = %s") |
76
|
|
|
cursor.execute(query, (user_uuid, token,)) |
77
|
|
|
row = cursor.fetchone() |
78
|
|
|
|
79
|
|
|
if row is None: |
80
|
|
|
if cursor: |
81
|
|
|
cursor.close() |
82
|
|
|
if cnx: |
83
|
|
|
cnx.disconnect() |
84
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
85
|
|
|
description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
86
|
|
|
else: |
87
|
|
|
utc_expires = row['utc_expires'] |
88
|
|
|
if datetime.utcnow() > utc_expires: |
89
|
|
|
if cursor: |
90
|
|
|
cursor.close() |
91
|
|
|
if cnx: |
92
|
|
|
cnx.disconnect() |
93
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
94
|
|
|
description='API.USER_SESSION_TIMEOUT') |
95
|
|
|
|
96
|
|
|
cursor.execute(" SELECT id " |
97
|
|
|
" FROM tbl_users " |
98
|
|
|
" WHERE uuid = %s ", |
99
|
|
|
(user_uuid,)) |
100
|
|
|
row = cursor.fetchone() |
101
|
|
|
if row is None: |
102
|
|
|
if cursor: |
103
|
|
|
cursor.close() |
104
|
|
|
if cnx: |
105
|
|
|
cnx.disconnect() |
106
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
107
|
|
|
description='API.INVALID_USER_PLEASE_RE_LOGIN') |
108
|
|
|
else: |
109
|
|
|
user_id = row['id'] |
110
|
|
|
|
111
|
|
|
if cursor: |
112
|
|
|
cursor.close() |
113
|
|
|
if cnx: |
114
|
|
|
cnx.disconnect() |
115
|
|
|
|
116
|
|
|
# get web messages |
117
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
118
|
|
|
cursor = cnx.cursor() |
119
|
|
|
|
120
|
|
|
query = (" SELECT id, subject, message, " |
121
|
|
|
" created_datetime_utc, status, reply " |
122
|
|
|
" FROM tbl_web_messages " |
123
|
|
|
" WHERE user_id = %s AND " |
124
|
|
|
" created_datetime_utc >= %s AND created_datetime_utc < %s " |
125
|
|
|
" ORDER BY created_datetime_utc DESC ") |
126
|
|
|
cursor.execute(query, (user_id, start_datetime_utc, end_datetime_utc)) |
127
|
|
|
rows = cursor.fetchall() |
128
|
|
|
|
129
|
|
|
if cursor: |
130
|
|
|
cursor.close() |
131
|
|
|
if cnx: |
132
|
|
|
cnx.disconnect() |
133
|
|
|
|
134
|
|
|
result = list() |
135
|
|
|
if rows is not None and len(rows) > 0: |
136
|
|
|
for row in rows: |
137
|
|
|
meta_result = {"id": row[0], |
138
|
|
|
"subject": row[1], |
139
|
|
|
"message": row[2].replace("<br>", ""), |
140
|
|
|
"created_datetime": row[3].timestamp() * 1000 if isinstance(row[3], datetime) else None, |
141
|
|
|
"status": row[4], |
142
|
|
|
"reply": row[5]} |
143
|
|
|
result.append(meta_result) |
144
|
|
|
|
145
|
|
|
resp.text = json.dumps(result) |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
class WebMessageStatusNewCollection: |
149
|
|
|
@staticmethod |
150
|
|
|
def __init__(): |
151
|
|
|
""""Initializes WebMessageStatusNewCollection""" |
152
|
|
|
pass |
153
|
|
|
|
154
|
|
|
@staticmethod |
155
|
|
|
def on_options(req, resp): |
156
|
|
|
resp.status = falcon.HTTP_200 |
157
|
|
|
|
158
|
|
|
@staticmethod |
159
|
|
|
def on_get(req, resp): |
160
|
|
|
"""Handles GET requests""" |
161
|
|
|
# Verify User Session |
162
|
|
|
token = req.headers.get('TOKEN') |
163
|
|
|
user_uuid = req.headers.get('USER-UUID') |
164
|
|
|
if token is None: |
165
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
166
|
|
|
description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
167
|
|
|
if user_uuid is None: |
168
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
169
|
|
|
description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
170
|
|
|
|
171
|
|
|
cnx = mysql.connector.connect(**config.myems_user_db) |
172
|
|
|
cursor = cnx.cursor(dictionary=True) |
173
|
|
|
|
174
|
|
|
query = (" SELECT utc_expires " |
175
|
|
|
" FROM tbl_sessions " |
176
|
|
|
" WHERE user_uuid = %s AND token = %s") |
177
|
|
|
cursor.execute(query, (user_uuid, token,)) |
178
|
|
|
row = cursor.fetchone() |
179
|
|
|
|
180
|
|
|
if row is None: |
181
|
|
|
if cursor: |
182
|
|
|
cursor.close() |
183
|
|
|
if cnx: |
184
|
|
|
cnx.disconnect() |
185
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
186
|
|
|
description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
187
|
|
|
else: |
188
|
|
|
utc_expires = row['utc_expires'] |
189
|
|
|
if datetime.utcnow() > utc_expires: |
190
|
|
|
if cursor: |
191
|
|
|
cursor.close() |
192
|
|
|
if cnx: |
193
|
|
|
cnx.disconnect() |
194
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
195
|
|
|
description='API.USER_SESSION_TIMEOUT') |
196
|
|
|
|
197
|
|
|
cursor.execute(" SELECT id " |
198
|
|
|
" FROM tbl_users " |
199
|
|
|
" WHERE uuid = %s ", |
200
|
|
|
(user_uuid,)) |
201
|
|
|
row = cursor.fetchone() |
202
|
|
|
if row is None: |
203
|
|
|
if cursor: |
204
|
|
|
cursor.close() |
205
|
|
|
if cnx: |
206
|
|
|
cnx.disconnect() |
207
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
208
|
|
|
description='API.INVALID_USER_PLEASE_RE_LOGIN') |
209
|
|
|
else: |
210
|
|
|
user_id = row['id'] |
211
|
|
|
|
212
|
|
|
if cursor: |
213
|
|
|
cursor.close() |
214
|
|
|
if cnx: |
215
|
|
|
cnx.disconnect() |
216
|
|
|
|
217
|
|
|
# get 'new' web messages |
218
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
219
|
|
|
cursor = cnx.cursor() |
220
|
|
|
|
221
|
|
|
query = (" SELECT id, subject, message, " |
222
|
|
|
" created_datetime_utc, status, reply " |
223
|
|
|
" FROM tbl_web_messages " |
224
|
|
|
" WHERE user_id = %s AND " |
225
|
|
|
" status = %s " |
226
|
|
|
" ORDER BY created_datetime_utc DESC ") |
227
|
|
|
cursor.execute(query, (user_id, 'new')) |
228
|
|
|
rows = cursor.fetchall() |
229
|
|
|
|
230
|
|
|
if cursor: |
231
|
|
|
cursor.close() |
232
|
|
|
if cnx: |
233
|
|
|
cnx.disconnect() |
234
|
|
|
|
235
|
|
|
result = list() |
236
|
|
|
if rows is not None and len(rows) > 0: |
237
|
|
|
for row in rows: |
238
|
|
|
meta_result = {"id": row[0], |
239
|
|
|
"subject": row[1], |
240
|
|
|
"message": row[2].replace("<br>", ""), |
241
|
|
|
"created_datetime": row[3].timestamp() * 1000 if isinstance(row[3], datetime) else None, |
242
|
|
|
"status": row[4], |
243
|
|
|
"reply": row[5]} |
244
|
|
|
result.append(meta_result) |
245
|
|
|
|
246
|
|
|
resp.text = json.dumps(result) |
247
|
|
|
|
248
|
|
|
@staticmethod |
249
|
|
|
@user_logger |
250
|
|
|
def on_put(req, resp): |
251
|
|
|
"""Handles PUT requests""" |
252
|
|
|
try: |
253
|
|
|
raw_json = req.stream.read().decode('utf-8') |
254
|
|
|
except Exception as ex: |
255
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
256
|
|
|
|
257
|
|
|
new_values = json.loads(raw_json) |
258
|
|
|
|
259
|
|
|
if 'status' not in new_values['data'].keys() or \ |
260
|
|
|
not isinstance(new_values['data']['status'], str) or \ |
261
|
|
|
len(str.strip(new_values['data']['status'])) == 0 or \ |
262
|
|
|
str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'read'): |
263
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
264
|
|
|
description='API.INVALID_STATUS') |
265
|
|
|
status = str.strip(new_values['data']['status']) |
266
|
|
|
|
267
|
|
|
# reply is required for 'acknowledged' status |
268
|
|
|
if status == 'acknowledged': |
269
|
|
|
if 'reply' not in new_values['data'].keys() or \ |
270
|
|
|
not isinstance(new_values['data']['reply'], str) or \ |
271
|
|
|
len(str.strip(new_values['data']['reply'])) == 0: |
272
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPLY') |
273
|
|
|
else: |
274
|
|
|
reply = str.strip(new_values['data']['reply']) |
275
|
|
|
else: |
276
|
|
|
reply = None |
277
|
|
|
|
278
|
|
|
# Verify User Session |
279
|
|
|
token = req.headers.get('TOKEN') |
280
|
|
|
user_uuid = req.headers.get('USER-UUID') |
281
|
|
|
if token is None: |
282
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
283
|
|
|
description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
284
|
|
|
if user_uuid is None: |
285
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
286
|
|
|
description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
287
|
|
|
|
288
|
|
|
cnx = mysql.connector.connect(**config.myems_user_db) |
289
|
|
|
cursor = cnx.cursor(dictionary=True) |
290
|
|
|
|
291
|
|
|
query = (" SELECT utc_expires " |
292
|
|
|
" FROM tbl_sessions " |
293
|
|
|
" WHERE user_uuid = %s AND token = %s") |
294
|
|
|
cursor.execute(query, (user_uuid, token,)) |
295
|
|
|
row = cursor.fetchone() |
296
|
|
|
|
297
|
|
|
if row is None: |
298
|
|
|
if cursor: |
299
|
|
|
cursor.close() |
300
|
|
|
if cnx: |
301
|
|
|
cnx.disconnect() |
302
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
303
|
|
|
description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
304
|
|
|
else: |
305
|
|
|
utc_expires = row['utc_expires'] |
306
|
|
|
if datetime.utcnow() > utc_expires: |
307
|
|
|
if cursor: |
308
|
|
|
cursor.close() |
309
|
|
|
if cnx: |
310
|
|
|
cnx.disconnect() |
311
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
312
|
|
|
description='API.USER_SESSION_TIMEOUT') |
313
|
|
|
|
314
|
|
|
cursor.execute(" SELECT id " |
315
|
|
|
" FROM tbl_users " |
316
|
|
|
" WHERE uuid = %s ", |
317
|
|
|
(user_uuid,)) |
318
|
|
|
row = cursor.fetchone() |
319
|
|
|
if row is None: |
320
|
|
|
if cursor: |
321
|
|
|
cursor.close() |
322
|
|
|
if cnx: |
323
|
|
|
cnx.disconnect() |
324
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
325
|
|
|
description='API.INVALID_USER_PLEASE_RE_LOGIN') |
326
|
|
|
else: |
327
|
|
|
user_id = row['id'] |
328
|
|
|
|
329
|
|
|
if cursor: |
330
|
|
|
cursor.close() |
331
|
|
|
if cnx: |
332
|
|
|
cnx.disconnect() |
333
|
|
|
|
334
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
335
|
|
|
cursor = cnx.cursor() |
336
|
|
|
|
337
|
|
|
cursor.execute(" SELECT user_id " |
338
|
|
|
" FROM tbl_web_messages " |
339
|
|
|
" WHERE status = %s AND user_id = %s ", ('new', user_id)) |
340
|
|
|
if cursor.fetchall() is None: |
341
|
|
|
cursor.close() |
342
|
|
|
cnx.disconnect() |
343
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
344
|
|
|
description='API.WEB_MESSAGE_NOT_FOUND') |
345
|
|
|
|
346
|
|
|
update_row = (" UPDATE tbl_web_messages " |
347
|
|
|
" SET status = %s, reply = %s " |
348
|
|
|
" WHERE status = %s AND user_id = %s ") |
349
|
|
|
cursor.execute(update_row, (status, |
350
|
|
|
reply, |
351
|
|
|
'new', |
352
|
|
|
user_id,)) |
353
|
|
|
cnx.commit() |
354
|
|
|
|
355
|
|
|
cursor.close() |
356
|
|
|
cnx.disconnect() |
357
|
|
|
|
358
|
|
|
resp.status = falcon.HTTP_200 |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
class WebMessageItem: |
362
|
|
|
@staticmethod |
363
|
|
|
def __init__(): |
364
|
|
|
""""Initializes WebMessageItem""" |
365
|
|
|
pass |
366
|
|
|
|
367
|
|
|
@staticmethod |
368
|
|
|
def on_options(req, resp, id_): |
369
|
|
|
resp.status = falcon.HTTP_200 |
370
|
|
|
|
371
|
|
|
@staticmethod |
372
|
|
|
def on_get(req, resp, id_): |
373
|
|
|
"""Handles GET requests""" |
374
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
375
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
376
|
|
|
description='API.INVALID_WEB_MESSAGE_ID') |
377
|
|
|
|
378
|
|
|
# Verify User Session |
379
|
|
|
token = req.headers.get('TOKEN') |
380
|
|
|
user_uuid = req.headers.get('USER-UUID') |
381
|
|
|
if token is None: |
382
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
383
|
|
|
description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
384
|
|
|
if user_uuid is None: |
385
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
386
|
|
|
description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
387
|
|
|
|
388
|
|
|
cnx = mysql.connector.connect(**config.myems_user_db) |
389
|
|
|
cursor = cnx.cursor(dictionary=True) |
390
|
|
|
|
391
|
|
|
query = (" SELECT utc_expires " |
392
|
|
|
" FROM tbl_sessions " |
393
|
|
|
" WHERE user_uuid = %s AND token = %s") |
394
|
|
|
cursor.execute(query, (user_uuid, token,)) |
395
|
|
|
row = cursor.fetchone() |
396
|
|
|
|
397
|
|
|
if row is None: |
398
|
|
|
if cursor: |
399
|
|
|
cursor.close() |
400
|
|
|
if cnx: |
401
|
|
|
cnx.disconnect() |
402
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
403
|
|
|
description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
404
|
|
|
else: |
405
|
|
|
utc_expires = row['utc_expires'] |
406
|
|
|
if datetime.utcnow() > utc_expires: |
407
|
|
|
if cursor: |
408
|
|
|
cursor.close() |
409
|
|
|
if cnx: |
410
|
|
|
cnx.disconnect() |
411
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
412
|
|
|
description='API.USER_SESSION_TIMEOUT') |
413
|
|
|
|
414
|
|
|
cursor.execute(" SELECT id " |
415
|
|
|
" FROM tbl_users " |
416
|
|
|
" WHERE uuid = %s ", |
417
|
|
|
(user_uuid,)) |
418
|
|
|
row = cursor.fetchone() |
419
|
|
|
if row is None: |
420
|
|
|
if cursor: |
421
|
|
|
cursor.close() |
422
|
|
|
if cnx: |
423
|
|
|
cnx.disconnect() |
424
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
425
|
|
|
description='API.INVALID_USER_PLEASE_RE_LOGIN') |
426
|
|
|
else: |
427
|
|
|
user_id = row['id'] |
428
|
|
|
|
429
|
|
|
if cursor: |
430
|
|
|
cursor.close() |
431
|
|
|
if cnx: |
432
|
|
|
cnx.disconnect() |
433
|
|
|
|
434
|
|
|
# get web message by id |
435
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
436
|
|
|
cursor = cnx.cursor() |
437
|
|
|
|
438
|
|
|
query = (" SELECT id, subject, message, " |
439
|
|
|
" created_datetime_utc, status, reply " |
440
|
|
|
" FROM tbl_web_messages " |
441
|
|
|
" WHERE id = %s AND user_id = %s " |
442
|
|
|
" ORDER BY created_datetime_utc DESC ") |
443
|
|
|
cursor.execute(query, (id_, user_id)) |
444
|
|
|
row = cursor.fetchone() |
445
|
|
|
|
446
|
|
|
if cursor: |
447
|
|
|
cursor.close() |
448
|
|
|
if cnx: |
449
|
|
|
cnx.disconnect() |
450
|
|
|
|
451
|
|
|
if row is None: |
452
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
453
|
|
|
description='API.WEB_MESSAGE_NOT_FOUND') |
454
|
|
|
|
455
|
|
|
meta_result = {"id": row[0], |
456
|
|
|
"subject": row[1], |
457
|
|
|
"message": row[2].replace("<br>", ""), |
458
|
|
|
"created_datetime": row[3].timestamp() * 1000 if isinstance(row[3], datetime) else None, |
459
|
|
|
"status": row[4], |
460
|
|
|
"reply": row[5]} |
461
|
|
|
|
462
|
|
|
resp.text = json.dumps(meta_result) |
463
|
|
|
|
464
|
|
|
@staticmethod |
465
|
|
|
@user_logger |
466
|
|
|
def on_put(req, resp, id_): |
467
|
|
|
"""Handles PUT requests""" |
468
|
|
|
try: |
469
|
|
|
raw_json = req.stream.read().decode('utf-8') |
470
|
|
|
except Exception as ex: |
471
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
472
|
|
|
|
473
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
474
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
475
|
|
|
description='API.INVALID_WEB_MESSAGE_ID') |
476
|
|
|
|
477
|
|
|
new_values = json.loads(raw_json) |
478
|
|
|
|
479
|
|
|
if 'status' not in new_values['data'].keys() or \ |
480
|
|
|
not isinstance(new_values['data']['status'], str) or \ |
481
|
|
|
len(str.strip(new_values['data']['status'])) == 0 or \ |
482
|
|
|
str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'read'): |
483
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
484
|
|
|
description='API.INVALID_STATUS') |
485
|
|
|
status = str.strip(new_values['data']['status']) |
486
|
|
|
|
487
|
|
|
# reply is required for 'acknowledged' status |
488
|
|
|
if status == 'acknowledged': |
489
|
|
|
if 'reply' not in new_values['data'].keys() or \ |
490
|
|
|
not isinstance(new_values['data']['reply'], str) or \ |
491
|
|
|
len(str.strip(new_values['data']['reply'])) == 0: |
492
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', description='API.INVALID_REPLY') |
493
|
|
|
else: |
494
|
|
|
reply = str.strip(new_values['data']['reply']) |
495
|
|
|
else: |
496
|
|
|
reply = None |
497
|
|
|
|
498
|
|
|
# Verify User Session |
499
|
|
|
token = req.headers.get('TOKEN') |
500
|
|
|
user_uuid = req.headers.get('USER-UUID') |
501
|
|
|
if token is None: |
502
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
503
|
|
|
description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
504
|
|
|
if user_uuid is None: |
505
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
506
|
|
|
description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
507
|
|
|
|
508
|
|
|
cnx = mysql.connector.connect(**config.myems_user_db) |
509
|
|
|
cursor = cnx.cursor(dictionary=True) |
510
|
|
|
|
511
|
|
|
query = (" SELECT utc_expires " |
512
|
|
|
" FROM tbl_sessions " |
513
|
|
|
" WHERE user_uuid = %s AND token = %s") |
514
|
|
|
cursor.execute(query, (user_uuid, token,)) |
515
|
|
|
row = cursor.fetchone() |
516
|
|
|
|
517
|
|
|
if row is None: |
518
|
|
|
if cursor: |
519
|
|
|
cursor.close() |
520
|
|
|
if cnx: |
521
|
|
|
cnx.disconnect() |
522
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
523
|
|
|
description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
524
|
|
|
else: |
525
|
|
|
utc_expires = row['utc_expires'] |
526
|
|
|
if datetime.utcnow() > utc_expires: |
527
|
|
|
if cursor: |
528
|
|
|
cursor.close() |
529
|
|
|
if cnx: |
530
|
|
|
cnx.disconnect() |
531
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
532
|
|
|
description='API.USER_SESSION_TIMEOUT') |
533
|
|
|
|
534
|
|
|
cursor.execute(" SELECT id " |
535
|
|
|
" FROM tbl_users " |
536
|
|
|
" WHERE uuid = %s ", |
537
|
|
|
(user_uuid,)) |
538
|
|
|
row = cursor.fetchone() |
539
|
|
|
if row is None: |
540
|
|
|
if cursor: |
541
|
|
|
cursor.close() |
542
|
|
|
if cnx: |
543
|
|
|
cnx.disconnect() |
544
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
545
|
|
|
description='API.INVALID_USER_PLEASE_RE_LOGIN') |
546
|
|
|
else: |
547
|
|
|
user_id = row['id'] |
548
|
|
|
|
549
|
|
|
if cursor: |
550
|
|
|
cursor.close() |
551
|
|
|
if cnx: |
552
|
|
|
cnx.disconnect() |
553
|
|
|
|
554
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
555
|
|
|
cursor = cnx.cursor() |
556
|
|
|
|
557
|
|
|
cursor.execute(" SELECT user_id " |
558
|
|
|
" FROM tbl_web_messages " |
559
|
|
|
" WHERE id = %s AND user_id = %s ", (id_, user_id)) |
560
|
|
|
if cursor.fetchone() is None: |
561
|
|
|
cursor.close() |
562
|
|
|
cnx.disconnect() |
563
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
564
|
|
|
description='API.WEB_MESSAGE_NOT_FOUND') |
565
|
|
|
|
566
|
|
|
update_row = (" UPDATE tbl_web_messages " |
567
|
|
|
" SET status = %s, reply = %s " |
568
|
|
|
" WHERE id = %s ") |
569
|
|
|
cursor.execute(update_row, (status, |
570
|
|
|
reply, |
571
|
|
|
id_,)) |
572
|
|
|
cnx.commit() |
573
|
|
|
|
574
|
|
|
cursor.close() |
575
|
|
|
cnx.disconnect() |
576
|
|
|
|
577
|
|
|
resp.status = falcon.HTTP_200 |
578
|
|
|
|
579
|
|
|
@staticmethod |
580
|
|
|
@user_logger |
581
|
|
|
def on_delete(req, resp, id_): |
582
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
583
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
584
|
|
|
description='API.INVALID_WEB_MESSAGE_ID') |
585
|
|
|
|
586
|
|
|
# Verify User Session |
587
|
|
|
token = req.headers.get('TOKEN') |
588
|
|
|
user_uuid = req.headers.get('USER-UUID') |
589
|
|
|
if token is None: |
590
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
591
|
|
|
description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
592
|
|
|
if user_uuid is None: |
593
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
594
|
|
|
description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
595
|
|
|
|
596
|
|
|
cnx = mysql.connector.connect(**config.myems_user_db) |
597
|
|
|
cursor = cnx.cursor(dictionary=True) |
598
|
|
|
|
599
|
|
|
query = (" SELECT utc_expires " |
600
|
|
|
" FROM tbl_sessions " |
601
|
|
|
" WHERE user_uuid = %s AND token = %s") |
602
|
|
|
cursor.execute(query, (user_uuid, token,)) |
603
|
|
|
row = cursor.fetchone() |
604
|
|
|
|
605
|
|
|
if row is None: |
606
|
|
|
if cursor: |
607
|
|
|
cursor.close() |
608
|
|
|
if cnx: |
609
|
|
|
cnx.disconnect() |
610
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
611
|
|
|
description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
612
|
|
|
else: |
613
|
|
|
utc_expires = row['utc_expires'] |
614
|
|
|
if datetime.utcnow() > utc_expires: |
615
|
|
|
if cursor: |
616
|
|
|
cursor.close() |
617
|
|
|
if cnx: |
618
|
|
|
cnx.disconnect() |
619
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
620
|
|
|
description='API.USER_SESSION_TIMEOUT') |
621
|
|
|
|
622
|
|
|
cursor.execute(" SELECT id " |
623
|
|
|
" FROM tbl_users " |
624
|
|
|
" WHERE uuid = %s ", |
625
|
|
|
(user_uuid,)) |
626
|
|
|
row = cursor.fetchone() |
627
|
|
|
if row is None: |
628
|
|
|
if cursor: |
629
|
|
|
cursor.close() |
630
|
|
|
if cnx: |
631
|
|
|
cnx.disconnect() |
632
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
633
|
|
|
description='API.INVALID_USER_PLEASE_RE_LOGIN') |
634
|
|
|
else: |
635
|
|
|
user_id = row['id'] |
636
|
|
|
|
637
|
|
|
if cursor: |
638
|
|
|
cursor.close() |
639
|
|
|
if cnx: |
640
|
|
|
cnx.disconnect() |
641
|
|
|
|
642
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
643
|
|
|
cursor = cnx.cursor() |
644
|
|
|
|
645
|
|
|
cursor.execute(" SELECT id " |
646
|
|
|
" FROM tbl_web_messages " |
647
|
|
|
" WHERE id = %s AND user_id = %s ", (id_, user_id)) |
648
|
|
|
row = cursor.fetchone() |
649
|
|
|
|
650
|
|
|
if row is None: |
651
|
|
|
if cursor: |
652
|
|
|
cursor.close() |
653
|
|
|
if cnx: |
654
|
|
|
cnx.disconnect() |
655
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
656
|
|
|
description='API.WEB_MESSAGE_NOT_FOUND') |
657
|
|
|
|
658
|
|
|
cursor.execute(" DELETE FROM tbl_web_messages WHERE id = %s ", (id_,)) |
659
|
|
|
cnx.commit() |
660
|
|
|
if cursor: |
661
|
|
|
cursor.close() |
662
|
|
|
if cnx: |
663
|
|
|
cnx.disconnect() |
664
|
|
|
|
665
|
|
|
resp.status = falcon.HTTP_204 |
666
|
|
|
|