| @@ 439-499 (lines=61) @@ | ||
| 436 | ||
| 437 | resp.text = json.dumps(result) |
|
| 438 | ||
| 439 | @staticmethod |
|
| 440 | @user_logger |
|
| 441 | def on_post(req, resp, id_): |
|
| 442 | """Handles POST requests""" |
|
| 443 | admin_control(req) |
|
| 444 | try: |
|
| 445 | raw_json = req.stream.read().decode('utf-8') |
|
| 446 | except Exception as ex: |
|
| 447 | print(ex) |
|
| 448 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 449 | title='API.BAD_REQUEST', |
|
| 450 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 451 | ||
| 452 | if not id_.isdigit() or int(id_) <= 0: |
|
| 453 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 454 | description='API.INVALID_COST_CENTER_ID') |
|
| 455 | ||
| 456 | new_values = json.loads(raw_json) |
|
| 457 | ||
| 458 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 459 | cursor = cnx.cursor() |
|
| 460 | ||
| 461 | cursor.execute(" SELECT name " |
|
| 462 | " FROM tbl_cost_centers " |
|
| 463 | " WHERE id = %s ", (id_,)) |
|
| 464 | if cursor.fetchone() is None: |
|
| 465 | cursor.close() |
|
| 466 | cnx.close() |
|
| 467 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 468 | description='API.COST_CENTER_NOT_FOUND') |
|
| 469 | ||
| 470 | cursor.execute(" SELECT name " |
|
| 471 | " FROM tbl_tariffs " |
|
| 472 | " WHERE id = %s ", (new_values['data']['tariff_id'],)) |
|
| 473 | if cursor.fetchone() is None: |
|
| 474 | cursor.close() |
|
| 475 | cnx.close() |
|
| 476 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 477 | description='API.TARIFF_NOT_FOUND') |
|
| 478 | ||
| 479 | cursor.execute(" SELECT id " |
|
| 480 | " FROM tbl_cost_centers_tariffs " |
|
| 481 | " WHERE cost_center_id = %s AND tariff_id = %s ", (id_, new_values['data']['tariff_id'])) |
|
| 482 | rows = cursor.fetchall() |
|
| 483 | if rows is not None and len(rows) > 0: |
|
| 484 | cursor.close() |
|
| 485 | cnx.close() |
|
| 486 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 487 | description='API.TARIFF_IS_ALREADY_ASSOCIATED_WITH_COST_CENTER') |
|
| 488 | ||
| 489 | add_row = (" INSERT INTO tbl_cost_centers_tariffs " |
|
| 490 | " (cost_center_id, tariff_id) " |
|
| 491 | " VALUES (%s, %s) ") |
|
| 492 | cursor.execute(add_row, (id_, new_values['data']['tariff_id'],)) |
|
| 493 | cnx.commit() |
|
| 494 | ||
| 495 | cursor.close() |
|
| 496 | cnx.close() |
|
| 497 | ||
| 498 | resp.status = falcon.HTTP_201 |
|
| 499 | resp.location = '/costcenters/' + str(id_) + '/tariffs/' + str(new_values['data']['tariff_id']) |
|
| 500 | ||
| 501 | ||
| 502 | class CostCenterTariffItem: |
|
| @@ 474-532 (lines=59) @@ | ||
| 471 | ||
| 472 | resp.text = json.dumps(result) |
|
| 473 | ||
| 474 | @staticmethod |
|
| 475 | @user_logger |
|
| 476 | def on_post(req, resp, id_): |
|
| 477 | """Handles POST requests""" |
|
| 478 | admin_control(req) |
|
| 479 | try: |
|
| 480 | raw_json = req.stream.read().decode('utf-8') |
|
| 481 | except Exception as ex: |
|
| 482 | print(str(ex)) |
|
| 483 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 484 | title='API.BAD_REQUEST', |
|
| 485 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 486 | ||
| 487 | if not id_.isdigit() or int(id_) <= 0: |
|
| 488 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 489 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
|
| 490 | ||
| 491 | new_values = json.loads(raw_json) |
|
| 492 | ||
| 493 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 494 | cursor = cnx.cursor() |
|
| 495 | ||
| 496 | cursor.execute(" SELECT name " |
|
| 497 | " from tbl_distribution_circuits " |
|
| 498 | " WHERE id = %s ", (id_,)) |
|
| 499 | if cursor.fetchone() is None: |
|
| 500 | cursor.close() |
|
| 501 | cnx.close() |
|
| 502 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 503 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
|
| 504 | ||
| 505 | cursor.execute(" SELECT name " |
|
| 506 | " FROM tbl_points " |
|
| 507 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 508 | if cursor.fetchone() is None: |
|
| 509 | cursor.close() |
|
| 510 | cnx.close() |
|
| 511 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 512 | description='API.POINT_NOT_FOUND') |
|
| 513 | ||
| 514 | query = (" SELECT id " |
|
| 515 | " FROM tbl_distribution_circuits_points " |
|
| 516 | " WHERE distribution_circuit_id = %s AND point_id = %s") |
|
| 517 | cursor.execute(query, (id_, new_values['data']['point_id'],)) |
|
| 518 | if cursor.fetchone() is not None: |
|
| 519 | cursor.close() |
|
| 520 | cnx.close() |
|
| 521 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 522 | description='API.DISTRIBUTION_CIRCUIT_POINT_RELATION_EXISTS') |
|
| 523 | ||
| 524 | add_row = (" INSERT INTO tbl_distribution_circuits_points (distribution_circuit_id, point_id) " |
|
| 525 | " VALUES (%s, %s) ") |
|
| 526 | cursor.execute(add_row, (id_, new_values['data']['point_id'],)) |
|
| 527 | cnx.commit() |
|
| 528 | cursor.close() |
|
| 529 | cnx.close() |
|
| 530 | ||
| 531 | resp.status = falcon.HTTP_201 |
|
| 532 | resp.location = '/distributioncircuits/' + str(id_) + '/points/' + str(new_values['data']['point_id']) |
|
| 533 | ||
| 534 | ||
| 535 | class DistributionCircuitPointItem: |
|
| @@ 364-422 (lines=59) @@ | ||
| 361 | ||
| 362 | resp.text = json.dumps(result) |
|
| 363 | ||
| 364 | @staticmethod |
|
| 365 | @user_logger |
|
| 366 | def on_post(req, resp, id_): |
|
| 367 | """Handles POST requests""" |
|
| 368 | admin_control(req) |
|
| 369 | try: |
|
| 370 | raw_json = req.stream.read().decode('utf-8') |
|
| 371 | except Exception as ex: |
|
| 372 | print(str(ex)) |
|
| 373 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 374 | title='API.BAD_REQUEST', |
|
| 375 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 376 | ||
| 377 | if not id_.isdigit() or int(id_) <= 0: |
|
| 378 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 379 | description='API.INVALID_SENSOR_ID') |
|
| 380 | ||
| 381 | new_values = json.loads(raw_json) |
|
| 382 | ||
| 383 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 384 | cursor = cnx.cursor() |
|
| 385 | ||
| 386 | cursor.execute(" SELECT name " |
|
| 387 | " from tbl_sensors " |
|
| 388 | " WHERE id = %s ", (id_,)) |
|
| 389 | if cursor.fetchone() is None: |
|
| 390 | cursor.close() |
|
| 391 | cnx.close() |
|
| 392 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 393 | description='API.SENSOR_NOT_FOUND') |
|
| 394 | ||
| 395 | cursor.execute(" SELECT name " |
|
| 396 | " FROM tbl_points " |
|
| 397 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 398 | if cursor.fetchone() is None: |
|
| 399 | cursor.close() |
|
| 400 | cnx.close() |
|
| 401 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 402 | description='API.POINT_NOT_FOUND') |
|
| 403 | ||
| 404 | query = (" SELECT id " |
|
| 405 | " FROM tbl_sensors_points " |
|
| 406 | " WHERE sensor_id = %s AND point_id = %s") |
|
| 407 | cursor.execute(query, (id_, new_values['data']['point_id'],)) |
|
| 408 | if cursor.fetchone() is not None: |
|
| 409 | cursor.close() |
|
| 410 | cnx.close() |
|
| 411 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 412 | description='API.SENSOR_POINT_RELATION_EXISTS') |
|
| 413 | ||
| 414 | add_row = (" INSERT INTO tbl_sensors_points (sensor_id, point_id) " |
|
| 415 | " VALUES (%s, %s) ") |
|
| 416 | cursor.execute(add_row, (id_, new_values['data']['point_id'],)) |
|
| 417 | cnx.commit() |
|
| 418 | cursor.close() |
|
| 419 | cnx.close() |
|
| 420 | ||
| 421 | resp.status = falcon.HTTP_201 |
|
| 422 | resp.location = '/sensors/' + str(id_) + '/points/' + str(new_values['data']['point_id']) |
|
| 423 | ||
| 424 | ||
| 425 | class SensorPointItem: |
|