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, access_control |
7
|
|
|
|
8
|
|
|
|
9
|
|
View Code Duplication |
class EmailMessageCollection: |
|
|
|
|
10
|
|
|
@staticmethod |
11
|
|
|
def __init__(): |
12
|
|
|
""""Initializes EmailMessageCollection""" |
13
|
|
|
pass |
14
|
|
|
|
15
|
|
|
@staticmethod |
16
|
|
|
def on_options(req, resp, startdate, enddate): |
17
|
|
|
resp.status = falcon.HTTP_200 |
18
|
|
|
|
19
|
|
|
@staticmethod |
20
|
|
|
def on_get(req, resp, startdate, enddate): |
21
|
|
|
access_control(req) |
22
|
|
|
try: |
23
|
|
|
start_datetime_local = datetime.strptime(startdate, '%Y-%m-%d') |
24
|
|
|
except Exception: |
25
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
26
|
|
|
title='API.BAD_REQUEST', |
27
|
|
|
description='API.INVALID_START_DATE_FORMAT') |
28
|
|
|
try: |
29
|
|
|
end_datetime_local = datetime.strptime(enddate, '%Y-%m-%d') |
30
|
|
|
except Exception: |
31
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
32
|
|
|
title='API.BAD_REQUEST', |
33
|
|
|
description='API.INVALID_END_DATE_FORMAT') |
34
|
|
|
|
35
|
|
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
36
|
|
|
if config.utc_offset[0] == '-': |
37
|
|
|
timezone_offset = -timezone_offset |
38
|
|
|
|
39
|
|
|
start_datetime_utc = start_datetime_local.replace(tzinfo=timezone.utc) |
40
|
|
|
start_datetime_utc -= timedelta(minutes=timezone_offset) |
41
|
|
|
|
42
|
|
|
end_datetime_utc = end_datetime_local.replace(tzinfo=timezone.utc) |
43
|
|
|
end_datetime_utc -= timedelta(minutes=timezone_offset) |
44
|
|
|
end_datetime_utc += timedelta(days=1) |
45
|
|
|
|
46
|
|
|
if start_datetime_utc >= end_datetime_utc: |
47
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
48
|
|
|
title='API.BAD_REQUEST', |
49
|
|
|
description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME') |
50
|
|
|
|
51
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
52
|
|
|
cursor = cnx.cursor() |
53
|
|
|
|
54
|
|
|
query = (" SELECT id, recipient_name, recipient_email, " |
55
|
|
|
" subject, message, attachment_file_name, " |
56
|
|
|
" created_datetime_utc, scheduled_datetime_utc, status " |
57
|
|
|
" FROM tbl_email_messages " |
58
|
|
|
" WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
59
|
|
|
" ORDER BY created_datetime_utc ") |
60
|
|
|
cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
61
|
|
|
rows = cursor.fetchall() |
62
|
|
|
|
63
|
|
|
if cursor: |
64
|
|
|
cursor.close() |
65
|
|
|
if cnx: |
66
|
|
|
cnx.disconnect() |
67
|
|
|
|
68
|
|
|
result = list() |
69
|
|
|
if rows is not None and len(rows) > 0: |
70
|
|
|
for row in rows: |
71
|
|
|
meta_result = {"id": row[0], |
72
|
|
|
"recipient_name": row[1], |
73
|
|
|
"recipient_email": row[2], |
74
|
|
|
"subject": row[3], |
75
|
|
|
"message": row[4].replace("<br>", ""), |
76
|
|
|
"attachment_file_name": row[5], |
77
|
|
|
"created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None, |
78
|
|
|
"scheduled_datetime": |
79
|
|
|
row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None, |
80
|
|
|
"status": row[8]} |
81
|
|
|
result.append(meta_result) |
82
|
|
|
|
83
|
|
|
resp.text = json.dumps(result) |
84
|
|
|
|
85
|
|
|
|
86
|
|
View Code Duplication |
class EmailMessageItem: |
|
|
|
|
87
|
|
|
@staticmethod |
88
|
|
|
def __init__(): |
89
|
|
|
""""Initializes EmailMessageItem""" |
90
|
|
|
pass |
91
|
|
|
|
92
|
|
|
@staticmethod |
93
|
|
|
def on_options(req, resp, id_): |
94
|
|
|
resp.status = falcon.HTTP_200 |
95
|
|
|
|
96
|
|
|
@staticmethod |
97
|
|
|
def on_get(req, resp, id_): |
98
|
|
|
access_control(req) |
99
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
100
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
101
|
|
|
description='API.INVALID_EMAIL_MESSAGE_ID') |
102
|
|
|
|
103
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
104
|
|
|
cursor = cnx.cursor() |
105
|
|
|
|
106
|
|
|
query = (" SELECT id, recipient_name, recipient_email, " |
107
|
|
|
" subject, message, attachment_file_name, " |
108
|
|
|
" created_datetime_utc, scheduled_datetime_utc, status " |
109
|
|
|
" FROM tbl_email_messages " |
110
|
|
|
" WHERE id = %s ") |
111
|
|
|
cursor.execute(query, (id_,)) |
112
|
|
|
row = cursor.fetchone() |
113
|
|
|
|
114
|
|
|
if cursor: |
115
|
|
|
cursor.close() |
116
|
|
|
if cnx: |
117
|
|
|
cnx.disconnect() |
118
|
|
|
|
119
|
|
|
if row is None: |
120
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
121
|
|
|
description='API.EMAIL_MESSAGE_NOT_FOUND') |
122
|
|
|
|
123
|
|
|
result = {"id": row[0], |
124
|
|
|
"recipient_name": row[1], |
125
|
|
|
"recipient_email": row[2], |
126
|
|
|
"subject": row[3], |
127
|
|
|
"message": row[4].replace("<br>", ""), |
128
|
|
|
"attachment_file_name": row[5], |
129
|
|
|
"created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None, |
130
|
|
|
"scheduled_datetime": row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None, |
131
|
|
|
"status": row[8]} |
132
|
|
|
|
133
|
|
|
resp.text = json.dumps(result) |
134
|
|
|
|
135
|
|
|
@staticmethod |
136
|
|
|
@user_logger |
137
|
|
|
def on_delete(req, resp, id_): |
138
|
|
|
access_control(req) |
139
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
140
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
141
|
|
|
description='API.INVALID_EMAIL_MESSAGE_ID') |
142
|
|
|
|
143
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
144
|
|
|
cursor = cnx.cursor() |
145
|
|
|
|
146
|
|
|
cursor.execute(" SELECT id " |
147
|
|
|
" FROM tbl_email_messages " |
148
|
|
|
" WHERE id = %s ", (id_,)) |
149
|
|
|
row = cursor.fetchone() |
150
|
|
|
|
151
|
|
|
if row is None: |
152
|
|
|
if cursor: |
153
|
|
|
cursor.close() |
154
|
|
|
if cnx: |
155
|
|
|
cnx.disconnect() |
156
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
157
|
|
|
description='API.EMAIL_MESSAGE_NOT_FOUND') |
158
|
|
|
|
159
|
|
|
cursor.execute(" DELETE FROM tbl_email_messages WHERE id = %s ", (id_,)) |
160
|
|
|
cnx.commit() |
161
|
|
|
|
162
|
|
|
if cursor: |
163
|
|
|
cursor.close() |
164
|
|
|
if cnx: |
165
|
|
|
cnx.disconnect() |
166
|
|
|
|
167
|
|
|
resp.status = falcon.HTTP_204 |
168
|
|
|
|
169
|
|
|
|