1 | import falcon |
||
2 | import simplejson as json |
||
3 | import mysql.connector |
||
4 | import config |
||
5 | import uuid |
||
6 | |||
7 | |||
8 | class DistributionCircuitCollection: |
||
9 | @staticmethod |
||
10 | def __init__(): |
||
11 | pass |
||
12 | |||
13 | @staticmethod |
||
14 | def on_options(req, resp): |
||
15 | resp.status = falcon.HTTP_200 |
||
16 | |||
17 | View Code Duplication | @staticmethod |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
18 | def on_get(req, resp): |
||
19 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
20 | cursor = cnx.cursor(dictionary=True) |
||
21 | |||
22 | query = (" SELECT id, name, uuid " |
||
23 | " FROM tbl_distribution_systems ") |
||
24 | cursor.execute(query) |
||
25 | rows_distribution_systems = cursor.fetchall() |
||
26 | |||
27 | distribution_system_dict = dict() |
||
28 | if rows_distribution_systems is not None and len(rows_distribution_systems) > 0: |
||
29 | for row in rows_distribution_systems: |
||
30 | distribution_system_dict[row['id']] = {"id": row['id'], |
||
31 | "name": row['name'], |
||
32 | "uuid": row['uuid']} |
||
33 | query = (" SELECT id, name, uuid, distribution_system_id, " |
||
34 | " distribution_room, switchgear, peak_load, peak_current, customers, meters " |
||
35 | " FROM tbl_distribution_circuits " |
||
36 | " ORDER BY id ") |
||
37 | cursor.execute(query) |
||
38 | rows_distribution_circuits = cursor.fetchall() |
||
39 | |||
40 | result = list() |
||
41 | if rows_distribution_circuits is not None and len(rows_distribution_circuits) > 0: |
||
42 | for row in rows_distribution_circuits: |
||
43 | distribution_system = distribution_system_dict.get(row['distribution_system_id']) |
||
44 | meta_result = {"id": row['id'], |
||
45 | "name": row['name'], |
||
46 | "uuid": row['uuid'], |
||
47 | "distribution_system": distribution_system, |
||
48 | "distribution_room": row['distribution_room'], |
||
49 | "switchgear": row['switchgear'], |
||
50 | "peak_load": row['peak_load'], |
||
51 | "peak_current": row['peak_current'], |
||
52 | "customers": row['customers'], |
||
53 | "meters": row['meters']} |
||
54 | result.append(meta_result) |
||
55 | |||
56 | cursor.close() |
||
57 | cnx.disconnect() |
||
58 | resp.body = json.dumps(result) |
||
59 | |||
60 | @staticmethod |
||
61 | def on_post(req, resp): |
||
62 | """Handles POST requests""" |
||
63 | try: |
||
64 | raw_json = req.stream.read().decode('utf-8') |
||
65 | except Exception as ex: |
||
66 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
67 | |||
68 | new_values = json.loads(raw_json, encoding='utf-8') |
||
69 | |||
70 | if 'name' not in new_values['data'].keys() or \ |
||
71 | not isinstance(new_values['data']['name'], str) or \ |
||
72 | len(str.strip(new_values['data']['name'])) == 0: |
||
73 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
74 | description='API.INVALID_DISTRIBUTION_CIRCUIT_NAME') |
||
75 | name = str.strip(new_values['data']['name']) |
||
76 | |||
77 | if 'distribution_system_id' not in new_values['data'].keys() or \ |
||
78 | not isinstance(new_values['data']['distribution_system_id'], int) or \ |
||
79 | new_values['data']['distribution_system_id'] <= 0: |
||
80 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
81 | description='API.INVALID_DISTRIBUTION_SYSTEM_ID') |
||
82 | distribution_system_id = new_values['data']['distribution_system_id'] |
||
83 | |||
84 | if 'distribution_room' not in new_values['data'].keys() or \ |
||
85 | not isinstance(new_values['data']['distribution_room'], str) or \ |
||
86 | len(str.strip(new_values['data']['distribution_room'])) == 0: |
||
87 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
88 | description='API.INVALID_DISTRIBUTION_ROOM') |
||
89 | distribution_room = str.strip(new_values['data']['distribution_room']) |
||
90 | |||
91 | if 'switchgear' not in new_values['data'].keys() or \ |
||
92 | not isinstance(new_values['data']['switchgear'], str) or \ |
||
93 | len(str.strip(new_values['data']['switchgear'])) == 0: |
||
94 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
95 | description='API.INVALID_SWITCHGEAR') |
||
96 | switchgear = str.strip(new_values['data']['switchgear']) |
||
97 | |||
98 | if 'peak_load' not in new_values['data'].keys() or \ |
||
99 | not (isinstance(new_values['data']['peak_load'], float) or |
||
100 | isinstance(new_values['data']['peak_load'], int)): |
||
101 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
102 | description='API.INVALID_PEAK_LOAD') |
||
103 | peak_load = float(new_values['data']['peak_load']) |
||
104 | |||
105 | if 'peak_current' not in new_values['data'].keys() or \ |
||
106 | not (isinstance(new_values['data']['peak_current'], float) or |
||
107 | isinstance(new_values['data']['peak_current'], int)): |
||
108 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
109 | description='API.INVALID_PEAK_CURRENT') |
||
110 | peak_current = float(new_values['data']['peak_current']) |
||
111 | |||
112 | if 'customers' in new_values['data'].keys() and \ |
||
113 | new_values['data']['customers'] is not None and \ |
||
114 | len(str(new_values['data']['customers'])) > 0: |
||
115 | customers = str.strip(new_values['data']['customers']) |
||
116 | else: |
||
117 | customers = None |
||
118 | |||
119 | if 'meters' in new_values['data'].keys() and \ |
||
120 | new_values['data']['meters'] is not None and \ |
||
121 | len(str(new_values['data']['meters'])) > 0: |
||
122 | meters = str.strip(new_values['data']['meters']) |
||
123 | else: |
||
124 | meters = None |
||
125 | |||
126 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
127 | cursor = cnx.cursor() |
||
128 | |||
129 | cursor.execute(" SELECT name " |
||
130 | " FROM tbl_distribution_systems " |
||
131 | " WHERE id = %s ", |
||
132 | (distribution_system_id,)) |
||
133 | if cursor.fetchone() is None: |
||
134 | cursor.close() |
||
135 | cnx.disconnect() |
||
136 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
137 | description='API.DISTRIBUTION_SYSTEM_NOT_FOUND') |
||
138 | |||
139 | cursor.execute(" SELECT name " |
||
140 | " FROM tbl_distribution_circuits " |
||
141 | " WHERE distribution_system_id = %s AND name = %s ", |
||
142 | (distribution_system_id, name,)) |
||
143 | if cursor.fetchone() is not None: |
||
144 | cursor.close() |
||
145 | cnx.disconnect() |
||
146 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
147 | description='API.DISTRIBUTION_CIRCUIT_NAME_IS_ALREADY_IN_USE') |
||
148 | |||
149 | add_values = (" INSERT INTO tbl_distribution_circuits " |
||
150 | " (name, uuid, distribution_system_id," |
||
151 | " distribution_room, switchgear, peak_load, peak_current, customers, meters) " |
||
152 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
153 | cursor.execute(add_values, (name, |
||
154 | str(uuid.uuid4()), |
||
155 | distribution_system_id, |
||
156 | distribution_room, |
||
157 | switchgear, |
||
158 | peak_load, |
||
159 | peak_current, |
||
160 | customers, |
||
161 | meters)) |
||
162 | new_id = cursor.lastrowid |
||
163 | cnx.commit() |
||
164 | cursor.close() |
||
165 | cnx.disconnect() |
||
166 | |||
167 | resp.status = falcon.HTTP_201 |
||
168 | resp.location = '/distributioncircuits/' + str(new_id) |
||
169 | |||
170 | |||
171 | class DistributionCircuitItem: |
||
172 | @staticmethod |
||
173 | def __init__(): |
||
174 | pass |
||
175 | |||
176 | @staticmethod |
||
177 | def on_options(req, resp, id_): |
||
178 | resp.status = falcon.HTTP_200 |
||
179 | |||
180 | @staticmethod |
||
181 | def on_get(req, resp, id_): |
||
182 | if not id_.isdigit() or int(id_) <= 0: |
||
183 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
184 | description='API.INVALID_METER_ID') |
||
185 | |||
186 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
187 | cursor = cnx.cursor(dictionary=True) |
||
188 | |||
189 | query = (" SELECT id, name, uuid " |
||
190 | " FROM tbl_distribution_systems ") |
||
191 | cursor.execute(query) |
||
192 | rows_distribution_systems = cursor.fetchall() |
||
193 | |||
194 | distribution_system_dict = dict() |
||
195 | if rows_distribution_systems is not None and len(rows_distribution_systems) > 0: |
||
196 | for row in rows_distribution_systems: |
||
197 | distribution_system_dict[row['id']] = {"id": row['id'], |
||
198 | "name": row['name'], |
||
199 | "uuid": row['uuid']} |
||
200 | |||
201 | query = (" SELECT id, name, uuid, distribution_system_id, " |
||
202 | " distribution_room, switchgear, peak_load, peak_current, customers, meters " |
||
203 | " FROM tbl_distribution_circuits " |
||
204 | " WHERE id = %s ") |
||
205 | cursor.execute(query, (id_,)) |
||
206 | row = cursor.fetchone() |
||
207 | cursor.close() |
||
208 | cnx.disconnect() |
||
209 | |||
210 | if row is None: |
||
211 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
212 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
||
213 | else: |
||
214 | distribution_system = distribution_system_dict.get(row['distribution_system_id']) |
||
215 | meta_result = {"id": row['id'], |
||
216 | "name": row['name'], |
||
217 | "uuid": row['uuid'], |
||
218 | "distribution_system": distribution_system, |
||
219 | "distribution_room": row['distribution_room'], |
||
220 | "switchgear": row['switchgear'], |
||
221 | "peak_load": row['peak_load'], |
||
222 | "peak_current": row['peak_current'], |
||
223 | "customers": row['customers'], |
||
224 | "meters": row['meters']} |
||
225 | |||
226 | resp.body = json.dumps(meta_result) |
||
227 | |||
228 | View Code Duplication | @staticmethod |
|
0 ignored issues
–
show
|
|||
229 | def on_delete(req, resp, id_): |
||
230 | if not id_.isdigit() or int(id_) <= 0: |
||
231 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
232 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
||
233 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
234 | cursor = cnx.cursor() |
||
235 | |||
236 | cursor.execute(" SELECT name " |
||
237 | " FROM tbl_distribution_circuits " |
||
238 | " WHERE id = %s ", (id_,)) |
||
239 | if cursor.fetchone() is None: |
||
240 | cursor.close() |
||
241 | cnx.disconnect() |
||
242 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
243 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
||
244 | |||
245 | # delete relation with points |
||
246 | cursor.execute(" DELETE FROM tbl_distribution_circuits_points " |
||
247 | " WHERE distribution_circuit_id = %s ", (id_,)) |
||
248 | # delete distribution circuit itself |
||
249 | cursor.execute(" DELETE FROM tbl_distribution_circuits " |
||
250 | " WHERE id = %s ", (id_,)) |
||
251 | cnx.commit() |
||
252 | |||
253 | cursor.close() |
||
254 | cnx.disconnect() |
||
255 | |||
256 | resp.status = falcon.HTTP_204 |
||
257 | |||
258 | @staticmethod |
||
259 | def on_put(req, resp, id_): |
||
260 | """Handles PUT requests""" |
||
261 | try: |
||
262 | raw_json = req.stream.read().decode('utf-8') |
||
263 | except Exception as ex: |
||
264 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
265 | |||
266 | if not id_.isdigit() or int(id_) <= 0: |
||
267 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
268 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
||
269 | |||
270 | new_values = json.loads(raw_json, encoding='utf-8') |
||
271 | |||
272 | if 'name' not in new_values['data'].keys() or \ |
||
273 | not isinstance(new_values['data']['name'], str) or \ |
||
274 | len(str.strip(new_values['data']['name'])) == 0: |
||
275 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
276 | description='API.INVALID_DISTRIBUTION_CIRCUIT_NAME') |
||
277 | name = str.strip(new_values['data']['name']) |
||
278 | |||
279 | if 'distribution_system_id' not in new_values['data'].keys() or \ |
||
280 | not isinstance(new_values['data']['distribution_system_id'], int) or \ |
||
281 | new_values['data']['distribution_system_id'] <= 0: |
||
282 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
283 | description='API.INVALID_DISTRIBUTION_SYSTEM_ID') |
||
284 | distribution_system_id = new_values['data']['distribution_system_id'] |
||
285 | |||
286 | if 'distribution_room' not in new_values['data'].keys() or \ |
||
287 | not isinstance(new_values['data']['distribution_room'], str) or \ |
||
288 | len(str.strip(new_values['data']['distribution_room'])) == 0: |
||
289 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
290 | description='API.INVALID_DISTRIBUTION_ROOM') |
||
291 | distribution_room = str.strip(new_values['data']['distribution_room']) |
||
292 | |||
293 | if 'switchgear' not in new_values['data'].keys() or \ |
||
294 | not isinstance(new_values['data']['switchgear'], str) or \ |
||
295 | len(str.strip(new_values['data']['switchgear'])) == 0: |
||
296 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
297 | description='API.INVALID_SWITCHGEAR') |
||
298 | switchgear = str.strip(new_values['data']['switchgear']) |
||
299 | |||
300 | if 'peak_load' not in new_values['data'].keys() or \ |
||
301 | not (isinstance(new_values['data']['peak_load'], float) or |
||
302 | isinstance(new_values['data']['peak_load'], int)): |
||
303 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
304 | description='API.INVALID_PEAK_LOAD') |
||
305 | peak_load = float(new_values['data']['peak_load']) |
||
306 | |||
307 | if 'peak_current' not in new_values['data'].keys() or \ |
||
308 | not (isinstance(new_values['data']['peak_current'], float) or |
||
309 | isinstance(new_values['data']['peak_current'], int)): |
||
310 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
311 | description='API.INVALID_PEAK_CURRENT') |
||
312 | peak_current = float(new_values['data']['peak_current']) |
||
313 | |||
314 | if 'customers' in new_values['data'].keys() and \ |
||
315 | new_values['data']['customers'] is not None and \ |
||
316 | len(str(new_values['data']['customers'])) > 0: |
||
317 | customers = str.strip(new_values['data']['customers']) |
||
318 | else: |
||
319 | customers = None |
||
320 | |||
321 | if 'meters' in new_values['data'].keys() and \ |
||
322 | new_values['data']['meters'] is not None and \ |
||
323 | len(str(new_values['data']['meters'])) > 0: |
||
324 | meters = str.strip(new_values['data']['meters']) |
||
325 | else: |
||
326 | meters = None |
||
327 | |||
328 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
329 | cursor = cnx.cursor() |
||
330 | |||
331 | cursor.execute(" SELECT name " |
||
332 | " FROM tbl_distribution_systems " |
||
333 | " WHERE id = %s ", |
||
334 | (distribution_system_id,)) |
||
335 | if cursor.fetchone() is None: |
||
336 | cursor.close() |
||
337 | cnx.disconnect() |
||
338 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
339 | description='API.DISTRIBUTION_SYSTEM_NOT_FOUND') |
||
340 | |||
341 | cursor.execute(" SELECT name " |
||
342 | " FROM tbl_distribution_circuits " |
||
343 | " WHERE distribution_system_id = %s AND name = %s AND id != %s ", |
||
344 | (distribution_system_id, name, id_)) |
||
345 | if cursor.fetchone() is not None: |
||
346 | cursor.close() |
||
347 | cnx.disconnect() |
||
348 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
349 | description='API.DISTRIBUTION_CIRCUIT_NAME_IS_ALREADY_IN_USE') |
||
350 | |||
351 | update_row = (" UPDATE tbl_distribution_circuits " |
||
352 | " SET name = %s, distribution_system_id = %s, distribution_room = %s, switchgear = %s, " |
||
353 | " peak_load = %s, peak_current = %s, customers = %s, meters = %s " |
||
354 | " WHERE id = %s ") |
||
355 | cursor.execute(update_row, (name, |
||
356 | distribution_system_id, |
||
357 | distribution_room, |
||
358 | switchgear, |
||
359 | peak_load, |
||
360 | peak_current, |
||
361 | customers, |
||
362 | meters, |
||
363 | id_)) |
||
364 | cnx.commit() |
||
365 | |||
366 | cursor.close() |
||
367 | cnx.disconnect() |
||
368 | |||
369 | resp.status = falcon.HTTP_200 |
||
370 | |||
371 | |||
372 | class DistributionCircuitPointCollection: |
||
373 | @staticmethod |
||
374 | def __init__(): |
||
375 | pass |
||
376 | |||
377 | @staticmethod |
||
378 | def on_options(req, resp, id_): |
||
379 | resp.status = falcon.HTTP_200 |
||
380 | |||
381 | @staticmethod |
||
382 | def on_get(req, resp, id_): |
||
383 | if not id_.isdigit() or int(id_) <= 0: |
||
384 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
385 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
||
386 | |||
387 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
388 | cursor = cnx.cursor(dictionary=True) |
||
389 | |||
390 | query = (" SELECT id, name, uuid " |
||
391 | " FROM tbl_distribution_systems ") |
||
392 | cursor.execute(query) |
||
393 | rows_distribution_systems = cursor.fetchall() |
||
394 | |||
395 | distribution_system_dict = dict() |
||
396 | if rows_distribution_systems is not None and len(rows_distribution_systems) > 0: |
||
397 | for row in rows_distribution_systems: |
||
398 | distribution_system_dict[row['uuid']] = {"id": row['id'], |
||
399 | "name": row['name'], |
||
400 | "uuid": row['uuid']} |
||
401 | |||
402 | cursor.execute(" SELECT name " |
||
403 | " FROM tbl_distribution_circuits " |
||
404 | " WHERE id = %s ", (id_,)) |
||
405 | if cursor.fetchone() is None: |
||
406 | cursor.close() |
||
407 | cnx.disconnect() |
||
408 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
409 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
||
410 | |||
411 | query = (" SELECT p.id AS point_id, p.name AS point_name, p.address AS point_address, " |
||
412 | " dc.id AS distribution_circuit_id, dc.name AS distribution_circuit_name, " |
||
413 | " dc.uuid AS distribution_circuit_uuid " |
||
414 | " FROM tbl_points p, tbl_distribution_circuits_points dcp, tbl_distribution_circuits dc " |
||
415 | " WHERE dcp.distribution_circuit_id = %s AND p.id = dcp.point_id " |
||
416 | " AND dcp.distribution_circuit_id = dc.id " |
||
417 | " ORDER BY p.name ") |
||
418 | cursor.execute(query, (id_,)) |
||
419 | rows = cursor.fetchall() |
||
420 | |||
421 | result = list() |
||
422 | if rows is not None and len(rows) > 0: |
||
423 | for row in rows: |
||
424 | meta_result = {"id": row['point_id'], "name": row['point_name'], "address": row['point_address'], |
||
425 | "distribution_circuit": {"id": row['distribution_circuit_id'], |
||
426 | "name": row['distribution_circuit_name'], |
||
427 | "uuid": row['distribution_circuit_uuid']}} |
||
428 | result.append(meta_result) |
||
429 | |||
430 | resp.body = json.dumps(result) |
||
431 | |||
432 | @staticmethod |
||
433 | def on_post(req, resp, id_): |
||
434 | """Handles POST requests""" |
||
435 | try: |
||
436 | raw_json = req.stream.read().decode('utf-8') |
||
437 | except Exception as ex: |
||
438 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
439 | |||
440 | if not id_.isdigit() or int(id_) <= 0: |
||
441 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
442 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
||
443 | |||
444 | new_values = json.loads(raw_json, encoding='utf-8') |
||
445 | |||
446 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
447 | cursor = cnx.cursor() |
||
448 | |||
449 | cursor.execute(" SELECT name " |
||
450 | " from tbl_distribution_circuits " |
||
451 | " WHERE id = %s ", (id_,)) |
||
452 | if cursor.fetchone() is None: |
||
453 | cursor.close() |
||
454 | cnx.disconnect() |
||
455 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
456 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
||
457 | |||
458 | cursor.execute(" SELECT name " |
||
459 | " FROM tbl_points " |
||
460 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
||
461 | if cursor.fetchone() is None: |
||
462 | cursor.close() |
||
463 | cnx.disconnect() |
||
464 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
465 | description='API.POINT_NOT_FOUND') |
||
466 | |||
467 | query = (" SELECT id " |
||
468 | " FROM tbl_distribution_circuits_points " |
||
469 | " WHERE distribution_circuit_id = %s AND point_id = %s") |
||
470 | cursor.execute(query, (id_, new_values['data']['point_id'],)) |
||
471 | if cursor.fetchone() is not None: |
||
472 | cursor.close() |
||
473 | cnx.disconnect() |
||
474 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
475 | description='API.DISTRIBUTION_CIRCUIT_POINT_RELATION_EXISTED') |
||
476 | |||
477 | add_row = (" INSERT INTO tbl_distribution_circuits_points (distribution_circuit_id, point_id) " |
||
478 | " VALUES (%s, %s) ") |
||
479 | cursor.execute(add_row, (id_, new_values['data']['point_id'],)) |
||
480 | new_id = cursor.lastrowid |
||
481 | cnx.commit() |
||
482 | cursor.close() |
||
483 | cnx.disconnect() |
||
484 | |||
485 | resp.status = falcon.HTTP_201 |
||
486 | resp.location = '/distributioncircuits/' + str(id_) + '/points/' + str(new_values['data']['point_id']) |
||
487 | |||
488 | |||
489 | View Code Duplication | class DistributionCircuitPointItem: |
|
0 ignored issues
–
show
|
|||
490 | @staticmethod |
||
491 | def __init__(): |
||
492 | pass |
||
493 | |||
494 | @staticmethod |
||
495 | def on_options(req, resp, id_, pid): |
||
496 | resp.status = falcon.HTTP_200 |
||
497 | |||
498 | @staticmethod |
||
499 | def on_delete(req, resp, id_, pid): |
||
500 | if not id_.isdigit() or int(id_) <= 0: |
||
501 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
502 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
||
503 | |||
504 | if not pid.isdigit() or int(pid) <= 0: |
||
505 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
506 | description='API.INVALID_POINT_ID') |
||
507 | |||
508 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
509 | cursor = cnx.cursor() |
||
510 | |||
511 | cursor.execute(" SELECT name " |
||
512 | " FROM tbl_distribution_circuits " |
||
513 | " WHERE id = %s ", (id_,)) |
||
514 | if cursor.fetchone() is None: |
||
515 | cursor.close() |
||
516 | cnx.disconnect() |
||
517 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
518 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
||
519 | |||
520 | cursor.execute(" SELECT name " |
||
521 | " FROM tbl_points " |
||
522 | " WHERE id = %s ", (pid,)) |
||
523 | if cursor.fetchone() is None: |
||
524 | cursor.close() |
||
525 | cnx.disconnect() |
||
526 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
527 | description='API.POINT_NOT_FOUND') |
||
528 | |||
529 | cursor.execute(" SELECT id " |
||
530 | " FROM tbl_distribution_circuits_points " |
||
531 | " WHERE distribution_circuit_id = %s AND point_id = %s ", (id_, pid)) |
||
532 | if cursor.fetchone() is None: |
||
533 | cursor.close() |
||
534 | cnx.disconnect() |
||
535 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
536 | description='API.DISTRIBUTION_CIRCUIT_POINT_RELATION_NOT_FOUND') |
||
537 | |||
538 | cursor.execute(" DELETE FROM tbl_distribution_circuits_points " |
||
539 | " WHERE distribution_circuit_id = %s AND point_id = %s ", (id_, pid)) |
||
540 | cnx.commit() |
||
541 | |||
542 | cursor.close() |
||
543 | cnx.disconnect() |
||
544 | |||
545 | resp.status = falcon.HTTP_204 |
||
546 | |||
547 |