|
1
|
|
|
import falcon |
|
2
|
|
|
import json |
|
3
|
|
|
import mysql.connector |
|
4
|
|
|
import config |
|
5
|
|
|
import uuid |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class RuleCollection: |
|
9
|
|
|
@staticmethod |
|
10
|
|
|
def __init__(): |
|
11
|
|
|
pass |
|
12
|
|
|
|
|
13
|
|
|
@staticmethod |
|
14
|
|
|
def on_options(req, resp): |
|
15
|
|
|
resp.status = falcon.HTTP_200 |
|
16
|
|
|
|
|
17
|
|
|
@staticmethod |
|
18
|
|
|
def on_get(req, resp): |
|
19
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
20
|
|
|
cursor = cnx.cursor() |
|
21
|
|
|
|
|
22
|
|
|
query = (" SELECT id, name, uuid, channel, expression, message, is_enabled " |
|
23
|
|
|
" FROM tbl_rules " |
|
24
|
|
|
" ORDER BY id ") |
|
25
|
|
|
cursor.execute(query) |
|
26
|
|
|
rows = cursor.fetchall() |
|
27
|
|
|
cursor.close() |
|
28
|
|
|
cnx.disconnect() |
|
29
|
|
|
|
|
30
|
|
|
result = list() |
|
31
|
|
|
if rows is not None and len(rows) > 0: |
|
32
|
|
|
for row in rows: |
|
33
|
|
|
meta_result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
34
|
|
|
"channel": row[3], "expression": row[4], "message": row[5].replace("<br>", ""), |
|
35
|
|
|
"is_enabled": bool(row[6])} |
|
36
|
|
|
result.append(meta_result) |
|
37
|
|
|
|
|
38
|
|
|
resp.body = json.dumps(result) |
|
39
|
|
|
|
|
40
|
|
|
@staticmethod |
|
41
|
|
|
def on_post(req, resp): |
|
42
|
|
|
"""Handles POST requests""" |
|
43
|
|
|
try: |
|
44
|
|
|
raw_json = req.stream.read().decode('utf-8') |
|
45
|
|
|
except Exception as ex: |
|
46
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
47
|
|
|
|
|
48
|
|
|
new_values = json.loads(raw_json, encoding='utf-8') |
|
49
|
|
|
if 'name' not in new_values['data'].keys() or \ |
|
50
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
|
51
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
|
52
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
53
|
|
|
description='API.INVALID_RULE_NAME') |
|
54
|
|
|
name = str.strip(new_values['data']['name']) |
|
55
|
|
|
|
|
56
|
|
|
if 'channel' not in new_values['data'].keys() or \ |
|
57
|
|
|
not isinstance(new_values['data']['channel'], str) or \ |
|
58
|
|
|
len(str.strip(new_values['data']['channel'])) == 0 or \ |
|
59
|
|
|
str.strip(new_values['data']['channel']) not in ('call', 'sms', 'email', 'wechat', 'web'): |
|
60
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
|
61
|
|
|
title='API.BAD_REQUEST', |
|
62
|
|
|
description='API.INVALID_CHANNEL') |
|
63
|
|
|
channel = str.strip(new_values['data']['channel']) |
|
64
|
|
|
|
|
65
|
|
|
if 'expression' not in new_values['data'].keys() or \ |
|
66
|
|
|
not isinstance(new_values['data']['expression'], str) or \ |
|
67
|
|
|
len(str.strip(new_values['data']['expression'])) == 0: |
|
68
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
|
69
|
|
|
title='API.BAD_REQUEST', |
|
70
|
|
|
description='API.INVALID_EXPRESSION') |
|
71
|
|
|
expression = str.strip(new_values['data']['expression']) |
|
72
|
|
|
|
|
73
|
|
|
if 'message' not in new_values['data'].keys() or \ |
|
74
|
|
|
not isinstance(new_values['data']['message'], str) or \ |
|
75
|
|
|
len(str.strip(new_values['data']['message'])) == 0: |
|
76
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
|
77
|
|
|
title='API.BAD_REQUEST', |
|
78
|
|
|
description='API.INVALID_MESSAGE') |
|
79
|
|
|
message = str.strip(new_values['data']['message']) |
|
80
|
|
|
|
|
81
|
|
|
if 'is_enabled' not in new_values['data'].keys() or \ |
|
82
|
|
|
not isinstance(new_values['data']['is_enabled'], bool): |
|
83
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
84
|
|
|
description='API.INVALID_IS_ENABLED') |
|
85
|
|
|
is_enabled = new_values['data']['is_enabled'] |
|
86
|
|
|
|
|
87
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
88
|
|
|
cursor = cnx.cursor() |
|
89
|
|
|
|
|
90
|
|
|
cursor.execute(" SELECT name " |
|
91
|
|
|
" FROM tbl_rules " |
|
92
|
|
|
" WHERE name = %s ", (name,)) |
|
93
|
|
|
if cursor.fetchone() is not None: |
|
94
|
|
|
cursor.close() |
|
95
|
|
|
cnx.disconnect() |
|
96
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
|
97
|
|
|
description='API.RULE_NAME_IS_ALREADY_IN_USE') |
|
98
|
|
|
|
|
99
|
|
|
add_row = (" INSERT INTO tbl_rules " |
|
100
|
|
|
" (name, uuid, channel, expression, message, is_enabled) " |
|
101
|
|
|
" VALUES (%s, %s, %s, %s, %s, %s) ") |
|
102
|
|
|
cursor.execute(add_row, (name, |
|
103
|
|
|
str(uuid.uuid4()), |
|
104
|
|
|
channel, |
|
105
|
|
|
expression, |
|
106
|
|
|
message, |
|
107
|
|
|
is_enabled)) |
|
108
|
|
|
new_id = cursor.lastrowid |
|
109
|
|
|
cnx.commit() |
|
110
|
|
|
cursor.close() |
|
111
|
|
|
cnx.disconnect() |
|
112
|
|
|
|
|
113
|
|
|
resp.status = falcon.HTTP_201 |
|
114
|
|
|
resp.location = '/rules/' + str(new_id) |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
class RuleItem: |
|
118
|
|
|
@staticmethod |
|
119
|
|
|
def __init__(): |
|
120
|
|
|
pass |
|
121
|
|
|
|
|
122
|
|
|
@staticmethod |
|
123
|
|
|
def on_options(req, resp, id_): |
|
124
|
|
|
resp.status = falcon.HTTP_200 |
|
125
|
|
|
|
|
126
|
|
|
@staticmethod |
|
127
|
|
|
def on_get(req, resp, id_): |
|
128
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
|
129
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
130
|
|
|
description='API.INVALID_RULE_ID') |
|
131
|
|
|
|
|
132
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
133
|
|
|
cursor = cnx.cursor() |
|
134
|
|
|
|
|
135
|
|
|
query = (" SELECT id, name, uuid, " |
|
136
|
|
|
" channel, expression, message, is_enabled " |
|
137
|
|
|
" FROM tbl_rules " |
|
138
|
|
|
" WHERE id = %s ") |
|
139
|
|
|
cursor.execute(query, (id_,)) |
|
140
|
|
|
row = cursor.fetchone() |
|
141
|
|
|
cursor.close() |
|
142
|
|
|
cnx.disconnect() |
|
143
|
|
|
if row is None: |
|
144
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
145
|
|
|
description='API.RULE_NOT_FOUND') |
|
146
|
|
|
|
|
147
|
|
|
result = {"id": row[0], "name": row[1], "uuid": row[2], |
|
148
|
|
|
"channel": row[3], "expression": row[4], "message": row[5].replace("<br>", ""), |
|
149
|
|
|
"is_enabled": bool(row[6])} |
|
150
|
|
|
resp.body = json.dumps(result) |
|
151
|
|
|
|
|
152
|
|
View Code Duplication |
@staticmethod |
|
|
|
|
|
|
153
|
|
|
def on_delete(req, resp, id_): |
|
154
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
|
155
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
156
|
|
|
description='API.INVALID_RULE_ID') |
|
157
|
|
|
|
|
158
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
159
|
|
|
cursor = cnx.cursor() |
|
160
|
|
|
|
|
161
|
|
|
cursor.execute(" SELECT id " |
|
162
|
|
|
" FROM tbl_rules " |
|
163
|
|
|
" WHERE id = %s ", |
|
164
|
|
|
(id_,)) |
|
165
|
|
|
if cursor.fetchone() is None: |
|
166
|
|
|
cursor.close() |
|
167
|
|
|
cnx.disconnect() |
|
168
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
169
|
|
|
description='API.RULE_NOT_FOUND') |
|
170
|
|
|
|
|
171
|
|
|
cursor.execute(" DELETE FROM tbl_rules WHERE id = %s ", (id_,)) |
|
172
|
|
|
cnx.commit() |
|
173
|
|
|
|
|
174
|
|
|
cursor.close() |
|
175
|
|
|
cnx.disconnect() |
|
176
|
|
|
|
|
177
|
|
|
resp.status = falcon.HTTP_204 |
|
178
|
|
|
|
|
179
|
|
|
@staticmethod |
|
180
|
|
|
def on_put(req, resp, id_): |
|
181
|
|
|
"""Handles PUT requests""" |
|
182
|
|
|
try: |
|
183
|
|
|
raw_json = req.stream.read().decode('utf-8') |
|
184
|
|
|
except Exception as ex: |
|
185
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
186
|
|
|
|
|
187
|
|
|
if not id_.isdigit() or int(id_) <= 0: |
|
188
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
189
|
|
|
description='API.INVALID_RULE_ID') |
|
190
|
|
|
|
|
191
|
|
|
new_values = json.loads(raw_json, encoding='utf-8') |
|
192
|
|
|
if 'name' not in new_values['data'].keys() or \ |
|
193
|
|
|
not isinstance(new_values['data']['name'], str) or \ |
|
194
|
|
|
len(str.strip(new_values['data']['name'])) == 0: |
|
195
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
196
|
|
|
description='API.INVALID_RULE_NAME') |
|
197
|
|
|
name = str.strip(new_values['data']['name']) |
|
198
|
|
|
|
|
199
|
|
|
if 'channel' not in new_values['data'].keys() or \ |
|
200
|
|
|
not isinstance(new_values['data']['channel'], str) or \ |
|
201
|
|
|
len(str.strip(new_values['data']['channel'])) == 0 or \ |
|
202
|
|
|
str.strip(new_values['data']['channel']) not in ('call', 'sms', 'email', 'wechat', 'web'): |
|
203
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
|
204
|
|
|
title='API.BAD_REQUEST', |
|
205
|
|
|
description='API.INVALID_CHANNEL') |
|
206
|
|
|
channel = str.strip(new_values['data']['channel']) |
|
207
|
|
|
|
|
208
|
|
|
if 'expression' not in new_values['data'].keys() or \ |
|
209
|
|
|
not isinstance(new_values['data']['expression'], str) or \ |
|
210
|
|
|
len(str.strip(new_values['data']['expression'])) == 0: |
|
211
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
|
212
|
|
|
title='API.BAD_REQUEST', |
|
213
|
|
|
description='API.INVALID_EXPRESSION') |
|
214
|
|
|
expression = str.strip(new_values['data']['expression']) |
|
215
|
|
|
|
|
216
|
|
|
if 'message' not in new_values['data'].keys() or \ |
|
217
|
|
|
not isinstance(new_values['data']['message'], str) or \ |
|
218
|
|
|
len(str.strip(new_values['data']['message'])) == 0: |
|
219
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, |
|
220
|
|
|
title='API.BAD_REQUEST', |
|
221
|
|
|
description='API.INVALID_MESSAGE') |
|
222
|
|
|
message = str.strip(new_values['data']['message']) |
|
223
|
|
|
|
|
224
|
|
|
if 'is_enabled' not in new_values['data'].keys() or \ |
|
225
|
|
|
not isinstance(new_values['data']['is_enabled'], bool): |
|
226
|
|
|
raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
227
|
|
|
description='API.INVALID_IS_ENABLED') |
|
228
|
|
|
is_enabled = new_values['data']['is_enabled'] |
|
229
|
|
|
|
|
230
|
|
|
cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
231
|
|
|
cursor = cnx.cursor() |
|
232
|
|
|
|
|
233
|
|
|
cursor.execute(" SELECT id " |
|
234
|
|
|
" FROM tbl_rules " |
|
235
|
|
|
" WHERE id = %s ", (id_,)) |
|
236
|
|
|
if cursor.fetchone() is None: |
|
237
|
|
|
cursor.close() |
|
238
|
|
|
cnx.disconnect() |
|
239
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
240
|
|
|
description='API.RULE_NOT_FOUND') |
|
241
|
|
|
|
|
242
|
|
|
cursor.execute(" SELECT name " |
|
243
|
|
|
" FROM tbl_rules " |
|
244
|
|
|
" WHERE name = %s AND id != %s ", (name, id_)) |
|
245
|
|
|
if cursor.fetchone() is not None: |
|
246
|
|
|
cursor.close() |
|
247
|
|
|
cnx.disconnect() |
|
248
|
|
|
raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
|
249
|
|
|
description='API.RULE_NAME_IS_ALREADY_IN_USE') |
|
250
|
|
|
|
|
251
|
|
|
update_row = (" UPDATE tbl_rules " |
|
252
|
|
|
" SET name = %s, channel = %s, expression = %s, message = %s, is_enabled = %s " |
|
253
|
|
|
" WHERE id = %s ") |
|
254
|
|
|
cursor.execute(update_row, (name, |
|
255
|
|
|
channel, |
|
256
|
|
|
expression, |
|
257
|
|
|
message, |
|
258
|
|
|
is_enabled, |
|
259
|
|
|
id_,)) |
|
260
|
|
|
cnx.commit() |
|
261
|
|
|
|
|
262
|
|
|
cursor.close() |
|
263
|
|
|
cnx.disconnect() |
|
264
|
|
|
|
|
265
|
|
|
resp.status = falcon.HTTP_200 |
|
266
|
|
|
|