Code Duplication    Length = 119-121 lines in 4 locations

myems-api/core/energystoragecontainer.py 4 locations

@@ 3035-3155 (lines=121) @@
3032
        resp.status = falcon.HTTP_204
3033
3034
3035
class EnergyStorageContainerHVACCollection:
3036
    def __init__(self):
3037
        """Initializes Class"""
3038
        pass
3039
3040
    @staticmethod
3041
    def on_options(req, resp, id_):
3042
        _=req
3043
        resp.status = falcon.HTTP_200
3044
        _=id_
3045
    @staticmethod
3046
    def on_get(req, resp, id_):
3047
        access_control(req)
3048
        if not id_.isdigit() or int(id_) <= 0:
3049
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3050
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3051
3052
        cnx = mysql.connector.connect(**config.myems_system_db)
3053
        cursor = cnx.cursor()
3054
3055
        cursor.execute(" SELECT name "
3056
                       " FROM tbl_energy_storage_containers "
3057
                       " WHERE id = %s ", (id_,))
3058
        if cursor.fetchone() is None:
3059
            cursor.close()
3060
            cnx.close()
3061
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3062
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3063
3064
        query = (" SELECT id, name, uuid "
3065
                 " FROM tbl_energy_storage_containers_hvacs "
3066
                 " WHERE energy_storage_container_id = %s "
3067
                 " ORDER BY name ")
3068
        cursor.execute(query, (id_,))
3069
        rows = cursor.fetchall()
3070
3071
        result = list()
3072
        if rows is not None and len(rows) > 0:
3073
            for row in rows:
3074
                meta_result = {"id": row[0],
3075
                               "name": row[1],
3076
                               "uuid": row[2]
3077
                               }
3078
                result.append(meta_result)
3079
3080
        resp.text = json.dumps(result)
3081
3082
    @staticmethod
3083
    @user_logger
3084
    def on_post(req, resp, id_):
3085
        """Handles POST requests"""
3086
        admin_control(req)
3087
        try:
3088
            raw_json = req.stream.read().decode('utf-8')
3089
        except Exception as ex:
3090
            print(str(ex))
3091
            raise falcon.HTTPError(status=falcon.HTTP_400,
3092
                                   title='API.BAD_REQUEST',
3093
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3094
        if not id_.isdigit() or int(id_) <= 0:
3095
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3096
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3097
3098
        cnx = mysql.connector.connect(**config.myems_system_db)
3099
        cursor = cnx.cursor()
3100
3101
        cursor.execute(" SELECT name "
3102
                       " FROM tbl_energy_storage_containers "
3103
                       " WHERE id = %s ", (id_,))
3104
        if cursor.fetchone() is None:
3105
            cursor.close()
3106
            cnx.close()
3107
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3108
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3109
3110
        new_values = json.loads(raw_json)
3111
3112
        if 'name' not in new_values['data'].keys() or \
3113
                not isinstance(new_values['data']['name'], str) or \
3114
                len(str.strip(new_values['data']['name'])) == 0:
3115
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3116
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
3117
        name = str.strip(new_values['data']['name'])
3118
3119
        cnx = mysql.connector.connect(**config.myems_system_db)
3120
        cursor = cnx.cursor()
3121
3122
        cursor.execute(" SELECT name "
3123
                       " FROM tbl_energy_storage_containers "
3124
                       " WHERE id = %s ",
3125
                       (id_,))
3126
        if cursor.fetchone() is None:
3127
            cursor.close()
3128
            cnx.close()
3129
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3130
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3131
3132
        cursor.execute(" SELECT name "
3133
                       " FROM tbl_energy_storage_containers_hvacs "
3134
                       " WHERE energy_storage_container_id = %s AND name = %s ",
3135
                       (id_, name,))
3136
        if cursor.fetchone() is not None:
3137
            cursor.close()
3138
            cnx.close()
3139
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3140
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
3141
3142
        add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs "
3143
                      "    (name, uuid, energy_storage_container_id) "
3144
                      " VALUES (%s, %s, %s) ")
3145
        cursor.execute(add_values, (name,
3146
                                    str(uuid.uuid4()),
3147
                                    id_
3148
                                    ))
3149
        new_id = cursor.lastrowid
3150
        cnx.commit()
3151
        cursor.close()
3152
        cnx.close()
3153
3154
        resp.status = falcon.HTTP_201
3155
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
3156
3157
3158
class EnergyStorageContainerHVACItem:
@@ 1878-1998 (lines=121) @@
1875
        resp.status = falcon.HTTP_204
1876
1877
1878
class EnergyStorageContainerFirecontrolCollection:
1879
    def __init__(self):
1880
        """Initializes Class"""
1881
        pass
1882
1883
    @staticmethod
1884
    def on_options(req, resp, id_):
1885
        _=req
1886
        resp.status = falcon.HTTP_200
1887
        _=id_
1888
    @staticmethod
1889
    def on_get(req, resp, id_):
1890
        access_control(req)
1891
        if not id_.isdigit() or int(id_) <= 0:
1892
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1893
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1894
1895
        cnx = mysql.connector.connect(**config.myems_system_db)
1896
        cursor = cnx.cursor()
1897
1898
        cursor.execute(" SELECT name "
1899
                       " FROM tbl_energy_storage_containers "
1900
                       " WHERE id = %s ", (id_,))
1901
        if cursor.fetchone() is None:
1902
            cursor.close()
1903
            cnx.close()
1904
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1905
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1906
1907
        query = (" SELECT id, name, uuid "
1908
                 " FROM tbl_energy_storage_containers_firecontrols "
1909
                 " WHERE energy_storage_container_id = %s "
1910
                 " ORDER BY name ")
1911
        cursor.execute(query, (id_,))
1912
        rows = cursor.fetchall()
1913
1914
        result = list()
1915
        if rows is not None and len(rows) > 0:
1916
            for row in rows:
1917
                meta_result = {"id": row[0],
1918
                               "name": row[1],
1919
                               "uuid": row[2]
1920
                               }
1921
                result.append(meta_result)
1922
1923
        resp.text = json.dumps(result)
1924
1925
    @staticmethod
1926
    @user_logger
1927
    def on_post(req, resp, id_):
1928
        """Handles POST requests"""
1929
        admin_control(req)
1930
        try:
1931
            raw_json = req.stream.read().decode('utf-8')
1932
        except Exception as ex:
1933
            print(str(ex))
1934
            raise falcon.HTTPError(status=falcon.HTTP_400,
1935
                                   title='API.BAD_REQUEST',
1936
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1937
        if not id_.isdigit() or int(id_) <= 0:
1938
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1939
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1940
1941
        cnx = mysql.connector.connect(**config.myems_system_db)
1942
        cursor = cnx.cursor()
1943
1944
        cursor.execute(" SELECT name "
1945
                       " FROM tbl_energy_storage_containers "
1946
                       " WHERE id = %s ", (id_,))
1947
        if cursor.fetchone() is None:
1948
            cursor.close()
1949
            cnx.close()
1950
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1951
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1952
1953
        new_values = json.loads(raw_json)
1954
1955
        if 'name' not in new_values['data'].keys() or \
1956
                not isinstance(new_values['data']['name'], str) or \
1957
                len(str.strip(new_values['data']['name'])) == 0:
1958
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1959
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
1960
        name = str.strip(new_values['data']['name'])
1961
1962
        cnx = mysql.connector.connect(**config.myems_system_db)
1963
        cursor = cnx.cursor()
1964
1965
        cursor.execute(" SELECT name "
1966
                       " FROM tbl_energy_storage_containers "
1967
                       " WHERE id = %s ",
1968
                       (id_,))
1969
        if cursor.fetchone() is None:
1970
            cursor.close()
1971
            cnx.close()
1972
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1973
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1974
1975
        cursor.execute(" SELECT name "
1976
                       " FROM tbl_energy_storage_containers_firecontrols "
1977
                       " WHERE energy_storage_container_id = %s AND name = %s ",
1978
                       (id_, name,))
1979
        if cursor.fetchone() is not None:
1980
            cursor.close()
1981
            cnx.close()
1982
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1983
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
1984
1985
        add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols "
1986
                      "    (name, uuid, energy_storage_container_id) "
1987
                      " VALUES (%s, %s, %s) ")
1988
        cursor.execute(add_values, (name,
1989
                                    str(uuid.uuid4()),
1990
                                    id_
1991
                                    ))
1992
        new_id = cursor.lastrowid
1993
        cnx.commit()
1994
        cursor.close()
1995
        cnx.close()
1996
1997
        resp.status = falcon.HTTP_201
1998
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
1999
2000
2001
class EnergyStorageContainerFirecontrolItem:
@@ 5013-5131 (lines=119) @@
5010
        resp.status = falcon.HTTP_200
5011
5012
5013
class EnergyStorageContainerSTSCollection:
5014
    def __init__(self):
5015
        """Initializes Class"""
5016
        pass
5017
5018
    @staticmethod
5019
    def on_options(req, resp, id_):
5020
        resp.status = falcon.HTTP_200
5021
5022
    @staticmethod
5023
    def on_get(req, resp, id_):
5024
        access_control(req)
5025
        if not id_.isdigit() or int(id_) <= 0:
5026
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5027
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5028
5029
        cnx = mysql.connector.connect(**config.myems_system_db)
5030
        cursor = cnx.cursor()
5031
5032
        cursor.execute(" SELECT name "
5033
                       " FROM tbl_energy_storage_containers "
5034
                       " WHERE id = %s ", (id_,))
5035
        if cursor.fetchone() is None:
5036
            cursor.close()
5037
            cnx.close()
5038
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5039
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5040
5041
        query = (" SELECT id, name, uuid "
5042
                 " FROM tbl_energy_storage_containers_stses "
5043
                 " WHERE energy_storage_container_id = %s "
5044
                 " ORDER BY name ")
5045
        cursor.execute(query, (id_,))
5046
        rows = cursor.fetchall()
5047
5048
        result = list()
5049
        if rows is not None and len(rows) > 0:
5050
            for row in rows:
5051
                meta_result = {"id": row[0],
5052
                               "name": row[1],
5053
                               "uuid": row[2]
5054
                               }
5055
                result.append(meta_result)
5056
5057
        resp.text = json.dumps(result)
5058
5059
    @staticmethod
5060
    @user_logger
5061
    def on_post(req, resp, id_):
5062
        """Handles POST requests"""
5063
        admin_control(req)
5064
        try:
5065
            raw_json = req.stream.read().decode('utf-8')
5066
        except Exception as ex:
5067
            raise falcon.HTTPError(status=falcon.HTTP_400,
5068
                                   title='API.BAD_REQUEST',
5069
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5070
        if not id_.isdigit() or int(id_) <= 0:
5071
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5072
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5073
5074
        cnx = mysql.connector.connect(**config.myems_system_db)
5075
        cursor = cnx.cursor()
5076
5077
        cursor.execute(" SELECT name "
5078
                       " FROM tbl_energy_storage_containers "
5079
                       " WHERE id = %s ", (id_,))
5080
        if cursor.fetchone() is None:
5081
            cursor.close()
5082
            cnx.close()
5083
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5084
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5085
5086
        new_values = json.loads(raw_json)
5087
5088
        if 'name' not in new_values['data'].keys() or \
5089
                not isinstance(new_values['data']['name'], str) or \
5090
                len(str.strip(new_values['data']['name'])) == 0:
5091
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5092
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME')
5093
        name = str.strip(new_values['data']['name'])
5094
5095
        cnx = mysql.connector.connect(**config.myems_system_db)
5096
        cursor = cnx.cursor()
5097
5098
        cursor.execute(" SELECT name "
5099
                       " FROM tbl_energy_storage_containers "
5100
                       " WHERE id = %s ",
5101
                       (id_,))
5102
        if cursor.fetchone() is None:
5103
            cursor.close()
5104
            cnx.close()
5105
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5106
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5107
5108
        cursor.execute(" SELECT name "
5109
                       " FROM tbl_energy_storage_containers_stses "
5110
                       " WHERE energy_storage_container_id = %s AND name = %s ",
5111
                       (id_, name,))
5112
        if cursor.fetchone() is not None:
5113
            cursor.close()
5114
            cnx.close()
5115
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5116
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE')
5117
5118
        add_values = (" INSERT INTO tbl_energy_storage_containers_stses "
5119
                      "    (name, uuid, energy_storage_container_id) "
5120
                      " VALUES (%s, %s, %s) ")
5121
        cursor.execute(add_values, (name,
5122
                                    str(uuid.uuid4()),
5123
                                    id_
5124
                                    ))
5125
        new_id = cursor.lastrowid
5126
        cnx.commit()
5127
        cursor.close()
5128
        cnx.close()
5129
5130
        resp.status = falcon.HTTP_201
5131
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id)
5132
5133
5134
class EnergyStorageContainerSTSItem:
@@ 1397-1515 (lines=119) @@
1394
        resp.status = falcon.HTTP_204
1395
1396
1397
class EnergyStorageContainerDCDCCollection:
1398
    def __init__(self):
1399
        """Initializes Class"""
1400
        pass
1401
1402
    @staticmethod
1403
    def on_options(req, resp, id_):
1404
        resp.status = falcon.HTTP_200
1405
1406
    @staticmethod
1407
    def on_get(req, resp, id_):
1408
        access_control(req)
1409
        if not id_.isdigit() or int(id_) <= 0:
1410
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1411
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1412
1413
        cnx = mysql.connector.connect(**config.myems_system_db)
1414
        cursor = cnx.cursor()
1415
1416
        cursor.execute(" SELECT name "
1417
                       " FROM tbl_energy_storage_containers "
1418
                       " WHERE id = %s ", (id_,))
1419
        if cursor.fetchone() is None:
1420
            cursor.close()
1421
            cnx.close()
1422
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1423
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1424
1425
        query = (" SELECT id, name, uuid "
1426
                 " FROM tbl_energy_storage_containers_dcdcs "
1427
                 " WHERE energy_storage_container_id = %s "
1428
                 " ORDER BY name ")
1429
        cursor.execute(query, (id_,))
1430
        rows = cursor.fetchall()
1431
1432
        result = list()
1433
        if rows is not None and len(rows) > 0:
1434
            for row in rows:
1435
                meta_result = {"id": row[0],
1436
                               "name": row[1],
1437
                               "uuid": row[2]
1438
                               }
1439
                result.append(meta_result)
1440
1441
        resp.text = json.dumps(result)
1442
1443
    @staticmethod
1444
    @user_logger
1445
    def on_post(req, resp, id_):
1446
        """Handles POST requests"""
1447
        admin_control(req)
1448
        try:
1449
            raw_json = req.stream.read().decode('utf-8')
1450
        except Exception as ex:
1451
            raise falcon.HTTPError(status=falcon.HTTP_400,
1452
                                   title='API.BAD_REQUEST',
1453
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1454
        if not id_.isdigit() or int(id_) <= 0:
1455
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1456
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1457
1458
        cnx = mysql.connector.connect(**config.myems_system_db)
1459
        cursor = cnx.cursor()
1460
1461
        cursor.execute(" SELECT name "
1462
                       " FROM tbl_energy_storage_containers "
1463
                       " WHERE id = %s ", (id_,))
1464
        if cursor.fetchone() is None:
1465
            cursor.close()
1466
            cnx.close()
1467
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1468
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1469
1470
        new_values = json.loads(raw_json)
1471
1472
        if 'name' not in new_values['data'].keys() or \
1473
                not isinstance(new_values['data']['name'], str) or \
1474
                len(str.strip(new_values['data']['name'])) == 0:
1475
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1476
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1477
        name = str.strip(new_values['data']['name'])
1478
1479
        cnx = mysql.connector.connect(**config.myems_system_db)
1480
        cursor = cnx.cursor()
1481
1482
        cursor.execute(" SELECT name "
1483
                       " FROM tbl_energy_storage_containers "
1484
                       " WHERE id = %s ",
1485
                       (id_,))
1486
        if cursor.fetchone() is None:
1487
            cursor.close()
1488
            cnx.close()
1489
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1490
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1491
1492
        cursor.execute(" SELECT name "
1493
                       " FROM tbl_energy_storage_containers_dcdcs "
1494
                       " WHERE energy_storage_container_id = %s AND name = %s ",
1495
                       (id_, name,))
1496
        if cursor.fetchone() is not None:
1497
            cursor.close()
1498
            cnx.close()
1499
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1500
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
1501
1502
        add_values = (" INSERT INTO tbl_energy_storage_containers_dcdcs "
1503
                      "    (name, uuid, energy_storage_container_id) "
1504
                      " VALUES (%s, %s, %s) ")
1505
        cursor.execute(add_values, (name,
1506
                                    str(uuid.uuid4()),
1507
                                    id_
1508
                                    ))
1509
        new_id = cursor.lastrowid
1510
        cnx.commit()
1511
        cursor.close()
1512
        cnx.close()
1513
1514
        resp.status = falcon.HTTP_201
1515
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1516
1517
1518
class EnergyStorageContainerDCDCItem: