Code Duplication    Length = 139-144 lines in 2 locations

myems-api/core/equipment.py 1 location

@@ 476-614 (lines=139) @@
473
        resp.status = falcon.HTTP_200
474
475
    # Clone an Equipment
476
    @staticmethod
477
    @user_logger
478
    def on_post(req, resp, id_):
479
        admin_control(req)
480
        """Handles POST requests"""
481
        if not id_.isdigit() or int(id_) <= 0:
482
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
483
                                   description='API.INVALID_EQUIPMENT_ID')
484
485
        cnx = mysql.connector.connect(**config.myems_system_db)
486
        cursor = cnx.cursor()
487
        cursor.execute(" SELECT name "
488
                       " FROM tbl_equipments "
489
                       " WHERE id = %s ", (id_,))
490
        if cursor.fetchone() is None:
491
            cursor.close()
492
            cnx.close()
493
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
494
                                   description='API.EQUIPMENT_NOT_FOUND')
495
496
        query = (" SELECT name, is_input_counted, is_output_counted, "
497
                 "        cost_center_id, svg_id, camera_url, description "
498
                 " FROM tbl_equipments "
499
                 " WHERE id = %s ")
500
        cursor.execute(query, (id_,))
501
        row = cursor.fetchone()
502
503
        if row is None:
504
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
505
                                   description='API.EQUIPMENT_NOT_FOUND')
506
        else:
507
            add_values = (" INSERT INTO tbl_equipments "
508
                          "    (name, uuid, is_input_counted, is_output_counted, "
509
                          "     cost_center_id, svg_id, camera_url, description) "
510
                          " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ")
511
            cursor.execute(add_values, (row[0] + ' Copy',
512
                                        str(uuid.uuid4()),
513
                                        row[1],
514
                                        row[2],
515
                                        row[3],
516
                                        row[4],
517
                                        row[5],
518
                                        row[6]))
519
            new_id = cursor.lastrowid
520
            cnx.commit()
521
522
        # clone relation with meter
523
        cursor.execute(" SELECT meter_id, is_output "
524
                       " FROM tbl_equipments_meters "
525
                       " WHERE equipment_id = %s ",
526
                       (id_,))
527
        rows_meters = cursor.fetchall()
528
        if rows_meters is not None and len(rows_meters) > 0:
529
            add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) "
530
                          " VALUES  ")
531
            for row in rows_meters:
532
                add_values += " (" + str(new_id) + ","
533
                add_values += str(row[0]) + ","
534
                add_values += str(bool(row[1])) + "), "
535
            # trim ", " at the end of string and then execute
536
            cursor.execute(add_values[:-2])
537
            cnx.commit()
538
539
        # clone relation with offline meter
540
        cursor.execute(" SELECT offline_meter_id, is_output "
541
                       " FROM tbl_equipments_offline_meters "
542
                       " WHERE equipment_id = %s ",
543
                       (id_,))
544
        rows_offline_meters = cursor.fetchall()
545
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
546
            add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) "
547
                          " VALUES  ")
548
            for row in rows_offline_meters:
549
                add_values += " (" + str(new_id) + ","
550
                add_values += "'" + str(row[0]) + "',"
551
                add_values += str(bool(row[1])) + "), "
552
            # trim ", " at the end of string and then execute
553
            cursor.execute(add_values[:-2])
554
            cnx.commit()
555
556
        # clone relation with virtual meter
557
        cursor.execute(" SELECT virtual_meter_id, is_output "
558
                       " FROM tbl_equipments_virtual_meters "
559
                       " WHERE equipment_id = %s ",
560
                       (id_,))
561
        rows_virtual_meters = cursor.fetchall()
562
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
563
            add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) "
564
                          " VALUES  ")
565
            for row in rows_virtual_meters:
566
                add_values += " (" + str(new_id) + ","
567
                add_values += str(row[0]) + ","
568
                add_values += str(bool(row[1])) + "), "
569
            # trim ", " at the end of string and then execute
570
            cursor.execute(add_values[:-2])
571
            cnx.commit()
572
573
        # clone parameters
574
        cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid "
575
                       " FROM tbl_equipments_parameters "
576
                       " WHERE equipment_id = %s ",
577
                       (id_,))
578
        rows_parameters = cursor.fetchall()
579
        if rows_parameters is not None and len(rows_parameters) > 0:
580
            add_values = (" INSERT INTO tbl_equipments_parameters"
581
                          "     (equipment_id, name, parameter_type, constant, point_id, "
582
                          "      numerator_meter_uuid, denominator_meter_uuid) "
583
                          " VALUES  ")
584
            for row in rows_parameters:
585
                add_values += " (" + str(new_id) + ","
586
                add_values += "'" + str(row[0]) + "',"
587
                add_values += "'" + str(row[1]) + "',"
588
                if row[2] is not None:
589
                    add_values += "'" + str(row[2]) + "',"
590
                else:
591
                    add_values += "null, "
592
593
                if row[3] is not None:
594
                    add_values += str(row[3]) + ","
595
                else:
596
                    add_values += "null, "
597
598
                if row[4] is not None:
599
                    add_values += "'" + row[4] + "',"
600
                else:
601
                    add_values += "null, "
602
                if row[5] is not None:
603
                    add_values += "'" + row[5] + "'), "
604
                else:
605
                    add_values += "null), "
606
607
            # trim ", " at the end of string and then execute
608
            cursor.execute(add_values[:-2])
609
            cnx.commit()
610
611
        cursor.close()
612
        cnx.close()
613
        resp.status = falcon.HTTP_201
614
        resp.location = '/equipments/' + str(new_id)
615
616
617
class EquipmentParameterCollection:

myems-api/core/combinedequipment.py 1 location

@@ 455-598 (lines=144) @@
452
        resp.status = falcon.HTTP_200
453
454
    # Clone a Combined Equipment
455
    @staticmethod
456
    @user_logger
457
    def on_post(req, resp, id_):
458
        """Handles POST requests"""
459
        admin_control(req)
460
        if not id_.isdigit() or int(id_) <= 0:
461
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
462
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
463
464
        cnx = mysql.connector.connect(**config.myems_system_db)
465
        cursor = cnx.cursor()
466
        cursor.execute(" SELECT name "
467
                       " FROM tbl_combined_equipments "
468
                       " WHERE id = %s ", (id_,))
469
        if cursor.fetchone() is None:
470
            cursor.close()
471
            cnx.close()
472
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
473
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
474
475
        query = (" SELECT name, is_input_counted, is_output_counted, "
476
                 "        cost_center_id, svg_id, camera_url, description "
477
                 " FROM tbl_combined_equipments "
478
                 " WHERE id = %s ")
479
        cursor.execute(query, (id_,))
480
        row = cursor.fetchone()
481
482
        if row is None:
483
            cursor.close()
484
            cnx.close()
485
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
486
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
487
        else:
488
            add_values = (" INSERT INTO tbl_combined_equipments "
489
                          "    (name, uuid, is_input_counted, is_output_counted, "
490
                          "     cost_center_id, svg_id, camera_url, description) "
491
                          " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ")
492
            cursor.execute(add_values, (row[0] + ' Copy',
493
                                        str(uuid.uuid4()),
494
                                        row[1],
495
                                        row[2],
496
                                        row[3],
497
                                        row[4],
498
                                        row[5],
499
                                        row[6]))
500
            new_id = cursor.lastrowid
501
            cnx.commit()
502
503
        # clone relation with meter
504
        cursor.execute(" SELECT meter_id, is_output "
505
                       " FROM tbl_combined_equipments_meters "
506
                       " WHERE combined_equipment_id = %s ",
507
                       (id_,))
508
        rows_meters = cursor.fetchall()
509
        if rows_meters is not None and len(rows_meters) > 0:
510
            add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) "
511
                          " VALUES  ")
512
            for row in rows_meters:
513
                add_values += " (" + str(new_id) + ","
514
                add_values += str(row[0]) + ","
515
                add_values += str(bool(row[1])) + "), "
516
            # trim ", " at the end of string and then execute
517
            cursor.execute(add_values[:-2])
518
            cnx.commit()
519
520
        # clone relation with offline meter
521
        cursor.execute(" SELECT offline_meter_id, is_output "
522
                       " FROM tbl_combined_equipments_offline_meters "
523
                       " WHERE combined_equipment_id = %s ",
524
                       (id_,))
525
        rows_offline_meters = cursor.fetchall()
526
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
527
            add_values = (" INSERT INTO tbl_combined_equipments_offline_meters "
528
                          " (combined_equipment_id, offline_meter_id, is_output) "
529
                          " VALUES  ")
530
            for row in rows_offline_meters:
531
                add_values += " (" + str(new_id) + ","
532
                add_values += "'" + str(row[0]) + "',"
533
                add_values += str(bool(row[1])) + "), "
534
            # trim ", " at the end of string and then execute
535
            cursor.execute(add_values[:-2])
536
            cnx.commit()
537
538
        # clone relation with virtual meter
539
        cursor.execute(" SELECT virtual_meter_id, is_output "
540
                       " FROM tbl_combined_equipments_virtual_meters "
541
                       " WHERE combined_equipment_id = %s ",
542
                       (id_,))
543
        rows_virtual_meters = cursor.fetchall()
544
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
545
            add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters "
546
                          " (combined_equipment_id, virtual_meter_id, is_output) "
547
                          " VALUES  ")
548
            for row in rows_virtual_meters:
549
                add_values += " (" + str(new_id) + ","
550
                add_values += str(row[0]) + ","
551
                add_values += str(bool(row[1])) + "), "
552
            # trim ", " at the end of string and then execute
553
            cursor.execute(add_values[:-2])
554
            cnx.commit()
555
556
        # clone parameters
557
        cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid "
558
                       " FROM tbl_combined_equipments_parameters "
559
                       " WHERE combined_equipment_id = %s ",
560
                       (id_,))
561
        rows_parameters = cursor.fetchall()
562
        if rows_parameters is not None and len(rows_parameters) > 0:
563
            add_values = (" INSERT INTO tbl_combined_equipments_parameters"
564
                          "     (combined_equipment_id, name, parameter_type, constant, point_id, "
565
                          "      numerator_meter_uuid, denominator_meter_uuid) "
566
                          " VALUES  ")
567
            for row in rows_parameters:
568
                add_values += " (" + str(new_id) + ","
569
                add_values += "'" + str(row[0]) + "',"
570
                add_values += "'" + str(row[1]) + "',"
571
                if row[2] is not None:
572
                    add_values += "'" + str(row[2]) + "',"
573
                else:
574
                    add_values += "null, "
575
576
                if row[3] is not None:
577
                    add_values += str(row[2]) + ","
578
                else:
579
                    add_values += "null, "
580
581
                if row[4] is not None:
582
                    add_values += "'" + row[4] + "',"
583
                else:
584
                    add_values += "null, "
585
                if row[5] is not None:
586
                    add_values += "'" + row[5] + "'), "
587
                else:
588
                    add_values += "null), "
589
590
            # trim ", " at the end of string and then execute
591
            cursor.execute(add_values[:-2])
592
            cnx.commit()
593
594
        cursor.close()
595
        cnx.close()
596
        resp.status = falcon.HTTP_201
597
        resp.location = '/combinedequipments/' + str(new_id)
598
599
600
class CombinedEquipmentEquipmentCollection:
601
    def __init__(self):