Code Duplication    Length = 74-79 lines in 2 locations

myems-api/core/space.py 1 location

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

myems-api/core/microgrid.py 1 location

@@ 379-452 (lines=74) @@
376
377
        resp.text = json.dumps(meta_result)
378
379
    @staticmethod
380
    @user_logger
381
    def on_delete(req, resp, id_):
382
        admin_control(req)
383
        if not id_.isdigit() or int(id_) <= 0:
384
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
385
                                   description='API.INVALID_MICROGRID_ID')
386
387
        cnx = mysql.connector.connect(**config.myems_system_db)
388
        cursor = cnx.cursor()
389
390
        cursor.execute(" SELECT name "
391
                       " FROM tbl_microgrids "
392
                       " WHERE id = %s ", (id_,))
393
        if cursor.fetchone() is None:
394
            cursor.close()
395
            cnx.close()
396
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
397
                                   description='API.MICROGRID_NOT_FOUND')
398
399
        # check relation with spaces
400
        cursor.execute(" SELECT id "
401
                       " FROM  tbl_spaces_microgrids "
402
                       " WHERE microgrid_id = %s ", (id_,))
403
        rows_spaces = cursor.fetchall()
404
        if rows_spaces is not None and len(rows_spaces) > 0:
405
            cursor.close()
406
            cnx.close()
407
            raise falcon.HTTPError(status=falcon.HTTP_400,
408
                                   title='API.BAD_REQUEST',
409
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
410
411
        # check relation with power plants
412
        cursor.execute(" SELECT id "
413
                       " FROM tbl_virtual_power_plants_microgrids "
414
                       " WHERE microgrid_id = %s ",
415
                       (id_,))
416
        rows_power_plants = cursor.fetchall()
417
        if rows_power_plants is not None and len(rows_power_plants) > 0:
418
            cursor.close()
419
            cnx.close()
420
            raise falcon.HTTPError(status=falcon.HTTP_400,
421
                                   title='API.BAD_REQUEST',
422
                                   description='API.THERE_IS_RELATION_WITH_VIRTUAL_POWER_PLANTS')
423
424
        cursor.execute(" DELETE FROM tbl_microgrids_batteries WHERE microgrid_id = %s ", (id_,))
425
        cnx.commit()
426
        cursor.execute(" DELETE FROM tbl_microgrids_commands WHERE microgrid_id = %s ", (id_,))
427
        cnx.commit()
428
        cursor.execute(" DELETE FROM tbl_microgrids_power_conversion_systems WHERE microgrid_id = %s ", (id_,))
429
        cnx.commit()
430
        cursor.execute(" DELETE FROM tbl_microgrids_evchargers WHERE microgrid_id = %s ", (id_,))
431
        cnx.commit()
432
        cursor.execute(" DELETE FROM tbl_microgrids_generators WHERE microgrid_id = %s ", (id_,))
433
        cnx.commit()
434
        cursor.execute(" DELETE FROM tbl_microgrids_grids WHERE microgrid_id = %s ", (id_,))
435
        cnx.commit()
436
        cursor.execute(" DELETE FROM tbl_microgrids_heatpumps WHERE microgrid_id = %s ", (id_,))
437
        cnx.commit()
438
        cursor.execute(" DELETE FROM tbl_microgrids_loads WHERE microgrid_id = %s ", (id_,))
439
        cnx.commit()
440
        cursor.execute(" DELETE FROM tbl_microgrids_photovoltaics WHERE microgrid_id = %s ", (id_,))
441
        cnx.commit()
442
        cursor.execute(" DELETE FROM tbl_microgrids_sensors WHERE microgrid_id = %s ", (id_,))
443
        cnx.commit()
444
        cursor.execute(" DELETE FROM tbl_microgrids_users WHERE microgrid_id = %s ", (id_,))
445
        cnx.commit()
446
        cursor.execute(" DELETE FROM tbl_microgrids WHERE id = %s ", (id_,))
447
        cnx.commit()
448
449
        cursor.close()
450
        cnx.close()
451
452
        resp.status = falcon.HTTP_204
453
454
    @staticmethod
455
    @user_logger