| @@ 529-589 (lines=61) @@ | ||
| 526 | ||
| 527 | resp.text = json.dumps(result) |
|
| 528 | ||
| 529 | @staticmethod |
|
| 530 | @user_logger |
|
| 531 | def on_post(req, resp, id_): |
|
| 532 | """Handles POST requests""" |
|
| 533 | admin_control(req) |
|
| 534 | try: |
|
| 535 | raw_json = req.stream.read().decode('utf-8') |
|
| 536 | except Exception as ex: |
|
| 537 | print(ex) |
|
| 538 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 539 | title='API.BAD_REQUEST', |
|
| 540 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 541 | ||
| 542 | if not id_.isdigit() or int(id_) <= 0: |
|
| 543 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 544 | description='API.INVALID_COST_CENTER_ID') |
|
| 545 | ||
| 546 | new_values = json.loads(raw_json) |
|
| 547 | ||
| 548 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 549 | cursor = cnx.cursor() |
|
| 550 | ||
| 551 | cursor.execute(" SELECT name " |
|
| 552 | " FROM tbl_cost_centers " |
|
| 553 | " WHERE id = %s ", (id_,)) |
|
| 554 | if cursor.fetchone() is None: |
|
| 555 | cursor.close() |
|
| 556 | cnx.close() |
|
| 557 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 558 | description='API.COST_CENTER_NOT_FOUND') |
|
| 559 | ||
| 560 | cursor.execute(" SELECT name " |
|
| 561 | " FROM tbl_tariffs " |
|
| 562 | " WHERE id = %s ", (new_values['data']['tariff_id'],)) |
|
| 563 | if cursor.fetchone() is None: |
|
| 564 | cursor.close() |
|
| 565 | cnx.close() |
|
| 566 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 567 | description='API.TARIFF_NOT_FOUND') |
|
| 568 | ||
| 569 | cursor.execute(" SELECT id " |
|
| 570 | " FROM tbl_cost_centers_tariffs " |
|
| 571 | " WHERE cost_center_id = %s AND tariff_id = %s ", (id_, new_values['data']['tariff_id'])) |
|
| 572 | rows = cursor.fetchall() |
|
| 573 | if rows is not None and len(rows) > 0: |
|
| 574 | cursor.close() |
|
| 575 | cnx.close() |
|
| 576 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 577 | description='API.TARIFF_IS_ALREADY_ASSOCIATED_WITH_COST_CENTER') |
|
| 578 | ||
| 579 | add_row = (" INSERT INTO tbl_cost_centers_tariffs " |
|
| 580 | " (cost_center_id, tariff_id) " |
|
| 581 | " VALUES (%s, %s) ") |
|
| 582 | cursor.execute(add_row, (id_, new_values['data']['tariff_id'],)) |
|
| 583 | cnx.commit() |
|
| 584 | ||
| 585 | cursor.close() |
|
| 586 | cnx.close() |
|
| 587 | ||
| 588 | resp.status = falcon.HTTP_201 |
|
| 589 | resp.location = '/costcenters/' + str(id_) + '/tariffs/' + str(new_values['data']['tariff_id']) |
|
| 590 | ||
| 591 | ||
| 592 | class CostCenterTariffItem: |
|
| @@ 480-538 (lines=59) @@ | ||
| 477 | ||
| 478 | resp.text = json.dumps(result) |
|
| 479 | ||
| 480 | @staticmethod |
|
| 481 | @user_logger |
|
| 482 | def on_post(req, resp, id_): |
|
| 483 | """Handles POST requests""" |
|
| 484 | admin_control(req) |
|
| 485 | try: |
|
| 486 | raw_json = req.stream.read().decode('utf-8') |
|
| 487 | except Exception as ex: |
|
| 488 | print(str(ex)) |
|
| 489 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 490 | title='API.BAD_REQUEST', |
|
| 491 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 492 | ||
| 493 | if not id_.isdigit() or int(id_) <= 0: |
|
| 494 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 495 | description='API.INVALID_DISTRIBUTION_CIRCUIT_ID') |
|
| 496 | ||
| 497 | new_values = json.loads(raw_json) |
|
| 498 | ||
| 499 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 500 | cursor = cnx.cursor() |
|
| 501 | ||
| 502 | cursor.execute(" SELECT name " |
|
| 503 | " from tbl_distribution_circuits " |
|
| 504 | " WHERE id = %s ", (id_,)) |
|
| 505 | if cursor.fetchone() is None: |
|
| 506 | cursor.close() |
|
| 507 | cnx.close() |
|
| 508 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 509 | description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND') |
|
| 510 | ||
| 511 | cursor.execute(" SELECT name " |
|
| 512 | " FROM tbl_points " |
|
| 513 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 514 | if cursor.fetchone() is None: |
|
| 515 | cursor.close() |
|
| 516 | cnx.close() |
|
| 517 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 518 | description='API.POINT_NOT_FOUND') |
|
| 519 | ||
| 520 | query = (" SELECT id " |
|
| 521 | " FROM tbl_distribution_circuits_points " |
|
| 522 | " WHERE distribution_circuit_id = %s AND point_id = %s") |
|
| 523 | cursor.execute(query, (id_, new_values['data']['point_id'],)) |
|
| 524 | if cursor.fetchone() is not None: |
|
| 525 | cursor.close() |
|
| 526 | cnx.close() |
|
| 527 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 528 | description='API.DISTRIBUTION_CIRCUIT_POINT_RELATION_EXISTS') |
|
| 529 | ||
| 530 | add_row = (" INSERT INTO tbl_distribution_circuits_points (distribution_circuit_id, point_id) " |
|
| 531 | " VALUES (%s, %s) ") |
|
| 532 | cursor.execute(add_row, (id_, new_values['data']['point_id'],)) |
|
| 533 | cnx.commit() |
|
| 534 | cursor.close() |
|
| 535 | cnx.close() |
|
| 536 | ||
| 537 | resp.status = falcon.HTTP_201 |
|
| 538 | resp.location = '/distributioncircuits/' + str(id_) + '/points/' + str(new_values['data']['point_id']) |
|
| 539 | ||
| 540 | ||
| 541 | class DistributionCircuitPointItem: |
|
| @@ 384-442 (lines=59) @@ | ||
| 381 | ||
| 382 | resp.text = json.dumps(result) |
|
| 383 | ||
| 384 | @staticmethod |
|
| 385 | @user_logger |
|
| 386 | def on_post(req, resp, id_): |
|
| 387 | """Handles POST requests""" |
|
| 388 | admin_control(req) |
|
| 389 | try: |
|
| 390 | raw_json = req.stream.read().decode('utf-8') |
|
| 391 | except Exception as ex: |
|
| 392 | print(str(ex)) |
|
| 393 | raise falcon.HTTPError(status=falcon.HTTP_400, |
|
| 394 | title='API.BAD_REQUEST', |
|
| 395 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
|
| 396 | ||
| 397 | if not id_.isdigit() or int(id_) <= 0: |
|
| 398 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 399 | description='API.INVALID_SENSOR_ID') |
|
| 400 | ||
| 401 | new_values = json.loads(raw_json) |
|
| 402 | ||
| 403 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
| 404 | cursor = cnx.cursor() |
|
| 405 | ||
| 406 | cursor.execute(" SELECT name " |
|
| 407 | " from tbl_sensors " |
|
| 408 | " WHERE id = %s ", (id_,)) |
|
| 409 | if cursor.fetchone() is None: |
|
| 410 | cursor.close() |
|
| 411 | cnx.close() |
|
| 412 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 413 | description='API.SENSOR_NOT_FOUND') |
|
| 414 | ||
| 415 | cursor.execute(" SELECT name " |
|
| 416 | " FROM tbl_points " |
|
| 417 | " WHERE id = %s ", (new_values['data']['point_id'],)) |
|
| 418 | if cursor.fetchone() is None: |
|
| 419 | cursor.close() |
|
| 420 | cnx.close() |
|
| 421 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
|
| 422 | description='API.POINT_NOT_FOUND') |
|
| 423 | ||
| 424 | query = (" SELECT id " |
|
| 425 | " FROM tbl_sensors_points " |
|
| 426 | " WHERE sensor_id = %s AND point_id = %s") |
|
| 427 | cursor.execute(query, (id_, new_values['data']['point_id'],)) |
|
| 428 | if cursor.fetchone() is not None: |
|
| 429 | cursor.close() |
|
| 430 | cnx.close() |
|
| 431 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
|
| 432 | description='API.SENSOR_POINT_RELATION_EXISTS') |
|
| 433 | ||
| 434 | add_row = (" INSERT INTO tbl_sensors_points (sensor_id, point_id) " |
|
| 435 | " VALUES (%s, %s) ") |
|
| 436 | cursor.execute(add_row, (id_, new_values['data']['point_id'],)) |
|
| 437 | cnx.commit() |
|
| 438 | cursor.close() |
|
| 439 | cnx.close() |
|
| 440 | ||
| 441 | resp.status = falcon.HTTP_201 |
|
| 442 | resp.location = '/sensors/' + str(id_) + '/points/' + str(new_values['data']['point_id']) |
|
| 443 | ||
| 444 | ||
| 445 | class SensorPointItem: |
|