Code Duplication    Length = 143-146 lines in 2 locations

myems-api/core/combinedequipment.py 1 location

@@ 380-525 (lines=146) @@
377
        resp.status = falcon.HTTP_200
378
379
    # Clone a Combined Equipment
380
    @staticmethod
381
    @user_logger
382
    def on_post(req, resp, id_):
383
        """Handles PUT requests"""
384
        access_control(req)
385
        if not id_.isdigit() or int(id_) <= 0:
386
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
387
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
388
        try:
389
            raw_json = req.stream.read().decode('utf-8')
390
        except Exception as ex:
391
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
392
393
        new_values = json.loads(raw_json)
394
395
        cnx = mysql.connector.connect(**config.myems_system_db)
396
        cursor = cnx.cursor()
397
        cursor.execute(" SELECT name "
398
                       " FROM tbl_combined_equipments "
399
                       " WHERE id = %s ", (id_,))
400
        if cursor.fetchone() is None:
401
            cursor.close()
402
            cnx.close()
403
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
404
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
405
406
        query = (" SELECT name, is_input_counted, is_output_counted, "
407
                 "        cost_center_id, description "
408
                 " FROM tbl_combined_equipments "
409
                 " WHERE id = %s ")
410
        cursor.execute(query, (id_,))
411
        row = cursor.fetchone()
412
413
        if row is None:
414
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
415
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
416
        else:
417
418
            add_values = (" INSERT INTO tbl_combined_equipments "
419
                          "    (name, uuid, is_input_counted, is_output_counted, "
420
                          "     cost_center_id, description) "
421
                          " VALUES (%s, %s, %s, %s, %s, %s) ")
422
            cursor.execute(add_values, (row[0] + ' Copy',
423
                                        str(uuid.uuid4()),
424
                                        row[1],
425
                                        row[2],
426
                                        row[3],
427
                                        row[4]))
428
            new_id = cursor.lastrowid
429
            cnx.commit()
430
431
        # clone relation with meter
432
        cursor.execute(" SELECT meter_id, is_output "
433
                       " FROM tbl_combined_equipments_meters "
434
                       " WHERE combined_equipment_id = %s ",
435
                       (id_,))
436
        rows_meters = cursor.fetchall()
437
        if rows_meters is not None and len(rows_meters) > 0:
438
            add_values = (" INSERT INTO tbl_combined_equipments_meters (combined_equipment_id, meter_id, is_output) "
439
                          " VALUES  ")
440
            for row in rows_meters:
441
                add_values += " (" + str(new_id) + ","
442
                add_values += str(row[0]) + ","
443
                add_values += str(bool(row[1])) + "), "
444
            # trim ", " at the end of string and then execute
445
            cursor.execute(add_values[:-2])
446
            cnx.commit()
447
448
        # clone relation with offline meter
449
        cursor.execute(" SELECT offline_meter_id, is_output "
450
                       " FROM tbl_combined_equipments_offline_meters "
451
                       " WHERE combined_equipment_id = %s ",
452
                       (id_,))
453
        rows_offline_meters = cursor.fetchall()
454
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
455
            add_values = (" INSERT INTO tbl_combined_equipments_offline_meters "
456
                          " (combined_equipment_id, offline_meter_id, is_output) "
457
                          " VALUES  ")
458
            for row in rows_offline_meters:
459
                add_values += " (" + str(new_id) + ","
460
                add_values += "'" + str(row[0]) + "',"
461
                add_values += str(bool(row[1])) + "), "
462
            # trim ", " at the end of string and then execute
463
            cursor.execute(add_values[:-2])
464
            cnx.commit()
465
466
        # clone relation with virtual meter
467
        cursor.execute(" SELECT virtual_meter_id, is_output "
468
                       " FROM tbl_combined_equipments_virtual_meters "
469
                       " WHERE combined_equipment_id = %s ",
470
                       (id_,))
471
        rows_virtual_meters = cursor.fetchall()
472
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
473
            add_values = (" INSERT INTO tbl_combined_equipments_virtual_meters "
474
                          " (combined_equipment_id, virtual_meter_id, is_output) "
475
                          " VALUES  ")
476
            for row in rows_virtual_meters:
477
                add_values += " (" + str(new_id) + ","
478
                add_values += str(row[0]) + ","
479
                add_values += str(bool(row[1])) + "), "
480
            # trim ", " at the end of string and then execute
481
            cursor.execute(add_values[:-2])
482
            cnx.commit()
483
484
        # clone parameters
485
        cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid "
486
                       " FROM tbl_combined_equipments_parameters "
487
                       " WHERE combined_equipment_id = %s ",
488
                       (id_,))
489
        rows_parameters = cursor.fetchall()
490
        if rows_parameters is not None and len(rows_parameters) > 0:
491
            add_values = (" INSERT INTO tbl_combined_equipments_parameters"
492
                          "     (combined_equipment_id, name, parameter_type, constant, point_id, "
493
                          "      numerator_meter_uuid, denominator_meter_uuid) "
494
                          " VALUES  ")
495
            for row in rows_parameters:
496
                add_values += " (" + str(new_id) + ","
497
                add_values += "'" + str(row[0]) + "',"
498
                add_values += "'" + str(row[1]) + "',"
499
                if row[2] is not None:
500
                    add_values += "'" + str(row[2]) + "',"
501
                else:
502
                    add_values += "null, "
503
504
                if row[3] is not None:
505
                    add_values += str(row[2]) + ","
506
                else:
507
                    add_values += "null, "
508
509
                if row[4] is not None:
510
                    add_values += "'" + row[4] + "',"
511
                else:
512
                    add_values += "null, "
513
                if row[5] is not None:
514
                    add_values += "'" + row[5] + "'), "
515
                else:
516
                    add_values += "null), "
517
518
            # trim ", " at the end of string and then execute
519
            cursor.execute(add_values[:-2])
520
            cnx.commit()
521
522
        cursor.close()
523
        cnx.close()
524
        resp.status = falcon.HTTP_201
525
        resp.location = '/combinedequipments/' + str(new_id)
526
527
528
class CombinedEquipmentEquipmentCollection:

myems-api/core/equipment.py 1 location

@@ 394-536 (lines=143) @@
391
        resp.status = falcon.HTTP_200
392
393
    # Clone an Equipment
394
    @staticmethod
395
    @user_logger
396
    def on_post(req, resp, id_):
397
        """Handles PUT requests"""
398
        if not id_.isdigit() or int(id_) <= 0:
399
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
400
                                   description='API.INVALID_EQUIPMENT_ID')
401
        try:
402
            raw_json = req.stream.read().decode('utf-8')
403
        except Exception as ex:
404
            raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)
405
406
        new_values = json.loads(raw_json)
407
408
        cnx = mysql.connector.connect(**config.myems_system_db)
409
        cursor = cnx.cursor(dictionary=True)
410
        cursor.execute(" SELECT name "
411
                       " FROM tbl_equipments "
412
                       " WHERE id = %s ", (id_,))
413
        if cursor.fetchone() is None:
414
            cursor.close()
415
            cnx.close()
416
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
417
                                   description='API.EQUIPMENT_NOT_FOUND')
418
419
        query = (" SELECT name, is_input_counted, is_output_counted, "
420
                 "        cost_center_id, description "
421
                 " FROM tbl_equipments "
422
                 " WHERE id = %s ")
423
        cursor.execute(query, (id_,))
424
        row = cursor.fetchone()
425
426
        if row is None:
427
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
428
                                   description='API.EQUIPMENT_NOT_FOUND')
429
        else:
430
431
            add_values = (" INSERT INTO tbl_equipments "
432
                          "    (name, uuid, is_input_counted, is_output_counted, "
433
                          "     cost_center_id, description) "
434
                          " VALUES (%s, %s, %s, %s, %s, %s) ")
435
            cursor.execute(add_values, (row['name'] + ' Copy',
436
                                        str(uuid.uuid4()),
437
                                        row['is_input_counted'],
438
                                        row['is_output_counted'],
439
                                        row['cost_center_id'],
440
                                        row['description']))
441
            new_id = cursor.lastrowid
442
            cnx.commit()
443
444
        # clone relation with meter
445
        cursor.execute(" SELECT meter_id, is_output "
446
                       " FROM tbl_equipments_meters "
447
                       " WHERE equipment_id = %s ",
448
                       (id_,))
449
        rows_meters = cursor.fetchall()
450
        if rows_meters is not None and len(rows_meters) > 0:
451
            add_values = (" INSERT INTO tbl_equipments_meters (equipment_id, meter_id, is_output) "
452
                          " VALUES  ")
453
            for row in rows_meters:
454
                add_values += " (" + str(new_id) + ","
455
                add_values += str(row['meter_id']) + ","
456
                add_values += str(bool(row['is_output'])) + "), "
457
            # trim ", " at the end of string and then execute
458
            cursor.execute(add_values[:-2])
459
            cnx.commit()
460
461
        # clone relation with offline meter
462
        cursor.execute(" SELECT offline_meter_id, is_output "
463
                       " FROM tbl_equipments_offline_meters "
464
                       " WHERE equipment_id = %s ",
465
                       (id_,))
466
        rows_offline_meters = cursor.fetchall()
467
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
468
            add_values = (" INSERT INTO tbl_equipments_offline_meters (equipment_id, offline_meter_id, is_output) "
469
                          " VALUES  ")
470
            for row in rows_offline_meters:
471
                add_values += " (" + str(new_id) + ","
472
                add_values += "'" + str(row['offline_meter_id']) + "',"
473
                add_values += str(bool(row['is_output'])) + "), "
474
            # trim ", " at the end of string and then execute
475
            cursor.execute(add_values[:-2])
476
            cnx.commit()
477
478
        # clone relation with virtual meter
479
        cursor.execute(" SELECT virtual_meter_id, is_output "
480
                       " FROM tbl_equipments_virtual_meters "
481
                       " WHERE equipment_id = %s ",
482
                       (id_,))
483
        rows_virtual_meters = cursor.fetchall()
484
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
485
            add_values = (" INSERT INTO tbl_equipments_virtual_meters (equipment_id, virtual_meter_id, is_output) "
486
                          " VALUES  ")
487
            for row in rows_virtual_meters:
488
                add_values += " (" + str(new_id) + ","
489
                add_values += str(row['virtual_meter_id']) + ","
490
                add_values += str(bool(row['is_output'])) + "), "
491
            # trim ", " at the end of string and then execute
492
            cursor.execute(add_values[:-2])
493
            cnx.commit()
494
495
        # clone parameters
496
        cursor.execute(" SELECT name, parameter_type, constant, point_id, numerator_meter_uuid, denominator_meter_uuid "
497
                       " FROM tbl_equipments_parameters "
498
                       " WHERE equipment_id = %s ",
499
                       (id_,))
500
        rows_parameters = cursor.fetchall()
501
        if rows_parameters is not None and len(rows_parameters) > 0:
502
            add_values = (" INSERT INTO tbl_equipments_parameters"
503
                          "     (equipment_id, name, parameter_type, constant, point_id, "
504
                          "      numerator_meter_uuid, denominator_meter_uuid) "
505
                          " VALUES  ")
506
            for row in rows_parameters:
507
                add_values += " (" + str(new_id) + ","
508
                add_values += "'" + str(row['name']) + "',"
509
                add_values += "'" + str(row['parameter_type']) + "',"
510
                if row['constant'] is not None:
511
                    add_values += "'" + str(row['constant']) + "',"
512
                else:
513
                    add_values += "null, "
514
515
                if row['point_id'] is not None:
516
                    add_values += str(row['point_id']) + ","
517
                else:
518
                    add_values += "null, "
519
520
                if row['numerator_meter_uuid'] is not None:
521
                    add_values += "'" + row['numerator_meter_uuid'] + "',"
522
                else:
523
                    add_values += "null, "
524
                if row['denominator_meter_uuid'] is not None:
525
                    add_values += "'" + row['denominator_meter_uuid'] + "'), "
526
                else:
527
                    add_values += "null), "
528
529
            # trim ", " at the end of string and then execute
530
            cursor.execute(add_values[:-2])
531
            cnx.commit()
532
533
        cursor.close()
534
        cnx.close()
535
        resp.status = falcon.HTTP_201
536
        resp.location = '/equipments/' + str(new_id)
537
538
539
class EquipmentParameterCollection: