Code Duplication    Length = 74-79 lines in 2 locations

myems-api/core/space.py 1 location

@@ 416-494 (lines=79) @@
413
414
        resp.text = json.dumps(meta_result)
415
416
    @staticmethod
417
    @user_logger
418
    def on_delete(req, resp, id_):
419
        admin_control(req)
420
        if not id_.isdigit() or int(id_) <= 0:
421
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
422
                                   description='API.INVALID_SPACE_ID')
423
        if int(id_) == 1:
424
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
425
                                   description='API.THIS_SPACE_CANNOT_BE_DELETED')
426
427
        cnx = mysql.connector.connect(**config.myems_system_db)
428
        cursor = cnx.cursor()
429
430
        cursor.execute(" SELECT name "
431
                       " FROM tbl_spaces "
432
                       " WHERE id = %s ", (id_,))
433
        if cursor.fetchone() is None:
434
            cursor.close()
435
            cnx.close()
436
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
437
                                   description='API.SPACE_NOT_FOUND')
438
439
        # checkout relation with children spaces
440
        cursor.execute(" SELECT id "
441
                       " FROM tbl_spaces "
442
                       " WHERE parent_space_id = %s ",
443
                       (id_,))
444
        rows_spaces = cursor.fetchall()
445
        if rows_spaces is not None and len(rows_spaces) > 0:
446
            cursor.close()
447
            cnx.close()
448
            raise falcon.HTTPError(status=falcon.HTTP_400,
449
                                   title='API.BAD_REQUEST',
450
                                   description='API.THERE_IS_RELATION_WITH_CHILDREN_SPACES')
451
452
        # delete relation with combined equipment
453
        cursor.execute(" DELETE FROM tbl_spaces_combined_equipments WHERE space_id = %s ", (id_,))
454
455
        # delete relation with commands
456
        cursor.execute(" DELETE FROM tbl_spaces_commands WHERE space_id = %s ", (id_,))
457
458
        # delete relation with equipments
459
        cursor.execute(" DELETE FROM tbl_spaces_equipments WHERE space_id = %s ", (id_,))
460
461
        # delete relation with meters
462
        cursor.execute(" DELETE FROM tbl_spaces_meters WHERE space_id = %s ", (id_,))
463
464
        # delete relation with offline meters
465
        cursor.execute(" DELETE FROM tbl_spaces_offline_meters WHERE space_id = %s ", (id_,))
466
467
        # delete relation with points
468
        cursor.execute(" DELETE FROM tbl_spaces_points WHERE space_id = %s ", (id_,))
469
470
        # delete relation with sensors
471
        cursor.execute(" DELETE FROM tbl_spaces_sensors WHERE space_id = %s ", (id_,))
472
473
        # delete relation with shopfloors
474
        cursor.execute(" DELETE FROM tbl_spaces_shopfloors WHERE space_id = %s ", (id_,))
475
476
        # delete relation with stores
477
        cursor.execute(" DELETE FROM tbl_spaces_stores WHERE space_id = %s ", (id_,))
478
479
        # delete relation with tenants
480
        cursor.execute(" DELETE FROM tbl_spaces_tenants WHERE space_id = %s ", (id_,))
481
482
        # delete relation with virtual meters
483
        cursor.execute(" DELETE FROM tbl_spaces_virtual_meters WHERE space_id = %s ", (id_,))
484
485
        # delete relation with working calendars
486
        cursor.execute(" DELETE FROM tbl_spaces_working_calendars WHERE space_id = %s ", (id_,))
487
488
        cursor.execute(" DELETE FROM tbl_spaces WHERE id = %s ", (id_,))
489
        cnx.commit()
490
491
        cursor.close()
492
        cnx.close()
493
494
        resp.status = falcon.HTTP_204
495
496
    @staticmethod
497
    @user_logger

myems-api/core/microgrid.py 1 location

@@ 425-498 (lines=74) @@
422
423
        resp.text = json.dumps(meta_result)
424
425
    @staticmethod
426
    @user_logger
427
    def on_delete(req, resp, id_):
428
        admin_control(req)
429
        if not id_.isdigit() or int(id_) <= 0:
430
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
431
                                   description='API.INVALID_MICROGRID_ID')
432
433
        cnx = mysql.connector.connect(**config.myems_system_db)
434
        cursor = cnx.cursor()
435
436
        cursor.execute(" SELECT name "
437
                       " FROM tbl_microgrids "
438
                       " WHERE id = %s ", (id_,))
439
        if cursor.fetchone() is None:
440
            cursor.close()
441
            cnx.close()
442
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
443
                                   description='API.MICROGRID_NOT_FOUND')
444
445
        # check relation with spaces
446
        cursor.execute(" SELECT id "
447
                       " FROM  tbl_spaces_microgrids "
448
                       " WHERE microgrid_id = %s ", (id_,))
449
        rows_spaces = cursor.fetchall()
450
        if rows_spaces is not None and len(rows_spaces) > 0:
451
            cursor.close()
452
            cnx.close()
453
            raise falcon.HTTPError(status=falcon.HTTP_400,
454
                                   title='API.BAD_REQUEST',
455
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
456
457
        # check relation with power plants
458
        cursor.execute(" SELECT id "
459
                       " FROM tbl_virtual_power_plants_microgrids "
460
                       " WHERE microgrid_id = %s ",
461
                       (id_,))
462
        rows_power_plants = cursor.fetchall()
463
        if rows_power_plants is not None and len(rows_power_plants) > 0:
464
            cursor.close()
465
            cnx.close()
466
            raise falcon.HTTPError(status=falcon.HTTP_400,
467
                                   title='API.BAD_REQUEST',
468
                                   description='API.THERE_IS_RELATION_WITH_VIRTUAL_POWER_PLANTS')
469
470
        cursor.execute(" DELETE FROM tbl_microgrids_batteries WHERE microgrid_id = %s ", (id_,))
471
        cnx.commit()
472
        cursor.execute(" DELETE FROM tbl_microgrids_commands WHERE microgrid_id = %s ", (id_,))
473
        cnx.commit()
474
        cursor.execute(" DELETE FROM tbl_microgrids_power_conversion_systems WHERE microgrid_id = %s ", (id_,))
475
        cnx.commit()
476
        cursor.execute(" DELETE FROM tbl_microgrids_evchargers WHERE microgrid_id = %s ", (id_,))
477
        cnx.commit()
478
        cursor.execute(" DELETE FROM tbl_microgrids_generators WHERE microgrid_id = %s ", (id_,))
479
        cnx.commit()
480
        cursor.execute(" DELETE FROM tbl_microgrids_grids WHERE microgrid_id = %s ", (id_,))
481
        cnx.commit()
482
        cursor.execute(" DELETE FROM tbl_microgrids_heatpumps WHERE microgrid_id = %s ", (id_,))
483
        cnx.commit()
484
        cursor.execute(" DELETE FROM tbl_microgrids_loads WHERE microgrid_id = %s ", (id_,))
485
        cnx.commit()
486
        cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics WHERE microgrid_id = %s ", (id_,))
487
        cnx.commit()
488
        cursor.execute(" DELETE FROM tbl_microgrids_sensors WHERE microgrid_id = %s ", (id_,))
489
        cnx.commit()
490
        cursor.execute(" DELETE FROM tbl_microgrids_users WHERE microgrid_id = %s ", (id_,))
491
        cnx.commit()
492
        cursor.execute(" DELETE FROM tbl_microgrids WHERE id = %s ", (id_,))
493
        cnx.commit()
494
495
        cursor.close()
496
        cnx.close()
497
498
        resp.status = falcon.HTTP_204
499
500
    @staticmethod
501
    @user_logger