Code Duplication    Length = 122-122 lines in 4 locations

myems-api/core/energystoragecontainer.py 4 locations

@@ 5351-5472 (lines=122) @@
5348
        resp.status = falcon.HTTP_200
5349
5350
5351
class EnergyStorageContainerSTSCollection:
5352
    def __init__(self):
5353
        """Initializes Class"""
5354
        pass
5355
5356
    @staticmethod
5357
    def on_options(req, resp, id_):
5358
        _ = req
5359
        resp.status = falcon.HTTP_200
5360
        _ = id_
5361
5362
    @staticmethod
5363
    def on_get(req, resp, id_):
5364
        access_control(req)
5365
        if not id_.isdigit() or int(id_) <= 0:
5366
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5367
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5368
5369
        cnx = mysql.connector.connect(**config.myems_system_db)
5370
        cursor = cnx.cursor()
5371
5372
        cursor.execute(" SELECT name "
5373
                       " FROM tbl_energy_storage_containers "
5374
                       " WHERE id = %s ", (id_,))
5375
        if cursor.fetchone() is None:
5376
            cursor.close()
5377
            cnx.close()
5378
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5379
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5380
5381
        query = (" SELECT id, name, uuid "
5382
                 " FROM tbl_energy_storage_containers_stses "
5383
                 " WHERE energy_storage_container_id = %s "
5384
                 " ORDER BY name ")
5385
        cursor.execute(query, (id_,))
5386
        rows = cursor.fetchall()
5387
5388
        result = list()
5389
        if rows is not None and len(rows) > 0:
5390
            for row in rows:
5391
                meta_result = {"id": row[0],
5392
                               "name": row[1],
5393
                               "uuid": row[2]
5394
                               }
5395
                result.append(meta_result)
5396
5397
        resp.text = json.dumps(result)
5398
5399
    @staticmethod
5400
    @user_logger
5401
    def on_post(req, resp, id_):
5402
        """Handles POST requests"""
5403
        admin_control(req)
5404
        try:
5405
            raw_json = req.stream.read().decode('utf-8')
5406
        except Exception as ex:
5407
            print(str(ex))
5408
            raise falcon.HTTPError(status=falcon.HTTP_400,
5409
                                   title='API.BAD_REQUEST',
5410
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5411
        if not id_.isdigit() or int(id_) <= 0:
5412
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5413
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5414
5415
        cnx = mysql.connector.connect(**config.myems_system_db)
5416
        cursor = cnx.cursor()
5417
5418
        cursor.execute(" SELECT name "
5419
                       " FROM tbl_energy_storage_containers "
5420
                       " WHERE id = %s ", (id_,))
5421
        if cursor.fetchone() is None:
5422
            cursor.close()
5423
            cnx.close()
5424
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5425
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5426
5427
        new_values = json.loads(raw_json)
5428
5429
        if 'name' not in new_values['data'].keys() or \
5430
                not isinstance(new_values['data']['name'], str) or \
5431
                len(str.strip(new_values['data']['name'])) == 0:
5432
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5433
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME')
5434
        name = str.strip(new_values['data']['name'])
5435
5436
        cnx = mysql.connector.connect(**config.myems_system_db)
5437
        cursor = cnx.cursor()
5438
5439
        cursor.execute(" SELECT name "
5440
                       " FROM tbl_energy_storage_containers "
5441
                       " WHERE id = %s ",
5442
                       (id_,))
5443
        if cursor.fetchone() is None:
5444
            cursor.close()
5445
            cnx.close()
5446
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5447
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5448
5449
        cursor.execute(" SELECT name "
5450
                       " FROM tbl_energy_storage_containers_stses "
5451
                       " WHERE energy_storage_container_id = %s AND name = %s ",
5452
                       (id_, name,))
5453
        if cursor.fetchone() is not None:
5454
            cursor.close()
5455
            cnx.close()
5456
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5457
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE')
5458
5459
        add_values = (" INSERT INTO tbl_energy_storage_containers_stses "
5460
                      "    (name, uuid, energy_storage_container_id) "
5461
                      " VALUES (%s, %s, %s) ")
5462
        cursor.execute(add_values, (name,
5463
                                    str(uuid.uuid4()),
5464
                                    id_
5465
                                    ))
5466
        new_id = cursor.lastrowid
5467
        cnx.commit()
5468
        cursor.close()
5469
        cnx.close()
5470
5471
        resp.status = falcon.HTTP_201
5472
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id)
5473
5474
5475
class EnergyStorageContainerSTSItem:
@@ 3340-3461 (lines=122) @@
3337
        resp.status = falcon.HTTP_204
3338
3339
3340
class EnergyStorageContainerHVACCollection:
3341
    def __init__(self):
3342
        """Initializes Class"""
3343
        pass
3344
3345
    @staticmethod
3346
    def on_options(req, resp, id_):
3347
        _ = req
3348
        resp.status = falcon.HTTP_200
3349
        _ = id_
3350
3351
    @staticmethod
3352
    def on_get(req, resp, id_):
3353
        access_control(req)
3354
        if not id_.isdigit() or int(id_) <= 0:
3355
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3356
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3357
3358
        cnx = mysql.connector.connect(**config.myems_system_db)
3359
        cursor = cnx.cursor()
3360
3361
        cursor.execute(" SELECT name "
3362
                       " FROM tbl_energy_storage_containers "
3363
                       " WHERE id = %s ", (id_,))
3364
        if cursor.fetchone() is None:
3365
            cursor.close()
3366
            cnx.close()
3367
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3368
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3369
3370
        query = (" SELECT id, name, uuid "
3371
                 " FROM tbl_energy_storage_containers_hvacs "
3372
                 " WHERE energy_storage_container_id = %s "
3373
                 " ORDER BY name ")
3374
        cursor.execute(query, (id_,))
3375
        rows = cursor.fetchall()
3376
3377
        result = list()
3378
        if rows is not None and len(rows) > 0:
3379
            for row in rows:
3380
                meta_result = {"id": row[0],
3381
                               "name": row[1],
3382
                               "uuid": row[2]
3383
                               }
3384
                result.append(meta_result)
3385
3386
        resp.text = json.dumps(result)
3387
3388
    @staticmethod
3389
    @user_logger
3390
    def on_post(req, resp, id_):
3391
        """Handles POST requests"""
3392
        admin_control(req)
3393
        try:
3394
            raw_json = req.stream.read().decode('utf-8')
3395
        except Exception as ex:
3396
            print(str(ex))
3397
            raise falcon.HTTPError(status=falcon.HTTP_400,
3398
                                   title='API.BAD_REQUEST',
3399
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3400
        if not id_.isdigit() or int(id_) <= 0:
3401
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3402
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3403
3404
        cnx = mysql.connector.connect(**config.myems_system_db)
3405
        cursor = cnx.cursor()
3406
3407
        cursor.execute(" SELECT name "
3408
                       " FROM tbl_energy_storage_containers "
3409
                       " WHERE id = %s ", (id_,))
3410
        if cursor.fetchone() is None:
3411
            cursor.close()
3412
            cnx.close()
3413
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3414
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3415
3416
        new_values = json.loads(raw_json)
3417
3418
        if 'name' not in new_values['data'].keys() or \
3419
                not isinstance(new_values['data']['name'], str) or \
3420
                len(str.strip(new_values['data']['name'])) == 0:
3421
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3422
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
3423
        name = str.strip(new_values['data']['name'])
3424
3425
        cnx = mysql.connector.connect(**config.myems_system_db)
3426
        cursor = cnx.cursor()
3427
3428
        cursor.execute(" SELECT name "
3429
                       " FROM tbl_energy_storage_containers "
3430
                       " WHERE id = %s ",
3431
                       (id_,))
3432
        if cursor.fetchone() is None:
3433
            cursor.close()
3434
            cnx.close()
3435
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3436
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3437
3438
        cursor.execute(" SELECT name "
3439
                       " FROM tbl_energy_storage_containers_hvacs "
3440
                       " WHERE energy_storage_container_id = %s AND name = %s ",
3441
                       (id_, name,))
3442
        if cursor.fetchone() is not None:
3443
            cursor.close()
3444
            cnx.close()
3445
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3446
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
3447
3448
        add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs "
3449
                      "    (name, uuid, energy_storage_container_id) "
3450
                      " VALUES (%s, %s, %s) ")
3451
        cursor.execute(add_values, (name,
3452
                                    str(uuid.uuid4()),
3453
                                    id_
3454
                                    ))
3455
        new_id = cursor.lastrowid
3456
        cnx.commit()
3457
        cursor.close()
3458
        cnx.close()
3459
3460
        resp.status = falcon.HTTP_201
3461
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
3462
3463
3464
class EnergyStorageContainerHVACItem:
@@ 2175-2296 (lines=122) @@
2172
        resp.status = falcon.HTTP_204
2173
2174
2175
class EnergyStorageContainerFirecontrolCollection:
2176
    def __init__(self):
2177
        """Initializes Class"""
2178
        pass
2179
2180
    @staticmethod
2181
    def on_options(req, resp, id_):
2182
        _ = req
2183
        resp.status = falcon.HTTP_200
2184
        _ = id_
2185
2186
    @staticmethod
2187
    def on_get(req, resp, id_):
2188
        access_control(req)
2189
        if not id_.isdigit() or int(id_) <= 0:
2190
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2191
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2192
2193
        cnx = mysql.connector.connect(**config.myems_system_db)
2194
        cursor = cnx.cursor()
2195
2196
        cursor.execute(" SELECT name "
2197
                       " FROM tbl_energy_storage_containers "
2198
                       " WHERE id = %s ", (id_,))
2199
        if cursor.fetchone() is None:
2200
            cursor.close()
2201
            cnx.close()
2202
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2203
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2204
2205
        query = (" SELECT id, name, uuid "
2206
                 " FROM tbl_energy_storage_containers_firecontrols "
2207
                 " WHERE energy_storage_container_id = %s "
2208
                 " ORDER BY name ")
2209
        cursor.execute(query, (id_,))
2210
        rows = cursor.fetchall()
2211
2212
        result = list()
2213
        if rows is not None and len(rows) > 0:
2214
            for row in rows:
2215
                meta_result = {"id": row[0],
2216
                               "name": row[1],
2217
                               "uuid": row[2]
2218
                               }
2219
                result.append(meta_result)
2220
2221
        resp.text = json.dumps(result)
2222
2223
    @staticmethod
2224
    @user_logger
2225
    def on_post(req, resp, id_):
2226
        """Handles POST requests"""
2227
        admin_control(req)
2228
        try:
2229
            raw_json = req.stream.read().decode('utf-8')
2230
        except Exception as ex:
2231
            print(str(ex))
2232
            raise falcon.HTTPError(status=falcon.HTTP_400,
2233
                                   title='API.BAD_REQUEST',
2234
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2235
        if not id_.isdigit() or int(id_) <= 0:
2236
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2237
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2238
2239
        cnx = mysql.connector.connect(**config.myems_system_db)
2240
        cursor = cnx.cursor()
2241
2242
        cursor.execute(" SELECT name "
2243
                       " FROM tbl_energy_storage_containers "
2244
                       " WHERE id = %s ", (id_,))
2245
        if cursor.fetchone() is None:
2246
            cursor.close()
2247
            cnx.close()
2248
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2249
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2250
2251
        new_values = json.loads(raw_json)
2252
2253
        if 'name' not in new_values['data'].keys() or \
2254
                not isinstance(new_values['data']['name'], str) or \
2255
                len(str.strip(new_values['data']['name'])) == 0:
2256
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2257
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
2258
        name = str.strip(new_values['data']['name'])
2259
2260
        cnx = mysql.connector.connect(**config.myems_system_db)
2261
        cursor = cnx.cursor()
2262
2263
        cursor.execute(" SELECT name "
2264
                       " FROM tbl_energy_storage_containers "
2265
                       " WHERE id = %s ",
2266
                       (id_,))
2267
        if cursor.fetchone() is None:
2268
            cursor.close()
2269
            cnx.close()
2270
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2271
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2272
2273
        cursor.execute(" SELECT name "
2274
                       " FROM tbl_energy_storage_containers_firecontrols "
2275
                       " WHERE energy_storage_container_id = %s AND name = %s ",
2276
                       (id_, name,))
2277
        if cursor.fetchone() is not None:
2278
            cursor.close()
2279
            cnx.close()
2280
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2281
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
2282
2283
        add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols "
2284
                      "    (name, uuid, energy_storage_container_id) "
2285
                      " VALUES (%s, %s, %s) ")
2286
        cursor.execute(add_values, (name,
2287
                                    str(uuid.uuid4()),
2288
                                    id_
2289
                                    ))
2290
        new_id = cursor.lastrowid
2291
        cnx.commit()
2292
        cursor.close()
2293
        cnx.close()
2294
2295
        resp.status = falcon.HTTP_201
2296
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
2297
2298
2299
class EnergyStorageContainerFirecontrolItem:
@@ 1685-1806 (lines=122) @@
1682
        resp.text = json.dumps(result)
1683
1684
1685
class EnergyStorageContainerDCDCCollection:
1686
    def __init__(self):
1687
        """Initializes Class"""
1688
        pass
1689
1690
    @staticmethod
1691
    def on_options(req, resp, id_):
1692
        _ = req
1693
        resp.status = falcon.HTTP_200
1694
        _ = id_
1695
1696
    @staticmethod
1697
    def on_get(req, resp, id_):
1698
        access_control(req)
1699
        if not id_.isdigit() or int(id_) <= 0:
1700
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1701
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1702
1703
        cnx = mysql.connector.connect(**config.myems_system_db)
1704
        cursor = cnx.cursor()
1705
1706
        cursor.execute(" SELECT name "
1707
                       " FROM tbl_energy_storage_containers "
1708
                       " WHERE id = %s ", (id_,))
1709
        if cursor.fetchone() is None:
1710
            cursor.close()
1711
            cnx.close()
1712
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1713
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1714
1715
        query = (" SELECT id, name, uuid "
1716
                 " FROM tbl_energy_storage_containers_dcdcs "
1717
                 " WHERE energy_storage_container_id = %s "
1718
                 " ORDER BY name ")
1719
        cursor.execute(query, (id_,))
1720
        rows = cursor.fetchall()
1721
1722
        result = list()
1723
        if rows is not None and len(rows) > 0:
1724
            for row in rows:
1725
                meta_result = {"id": row[0],
1726
                               "name": row[1],
1727
                               "uuid": row[2]
1728
                               }
1729
                result.append(meta_result)
1730
1731
        resp.text = json.dumps(result)
1732
1733
    @staticmethod
1734
    @user_logger
1735
    def on_post(req, resp, id_):
1736
        """Handles POST requests"""
1737
        admin_control(req)
1738
        try:
1739
            raw_json = req.stream.read().decode('utf-8')
1740
        except Exception as ex:
1741
            print(str(ex))
1742
            raise falcon.HTTPError(status=falcon.HTTP_400,
1743
                                   title='API.BAD_REQUEST',
1744
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1745
        if not id_.isdigit() or int(id_) <= 0:
1746
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1747
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1748
1749
        cnx = mysql.connector.connect(**config.myems_system_db)
1750
        cursor = cnx.cursor()
1751
1752
        cursor.execute(" SELECT name "
1753
                       " FROM tbl_energy_storage_containers "
1754
                       " WHERE id = %s ", (id_,))
1755
        if cursor.fetchone() is None:
1756
            cursor.close()
1757
            cnx.close()
1758
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1759
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1760
1761
        new_values = json.loads(raw_json)
1762
1763
        if 'name' not in new_values['data'].keys() or \
1764
                not isinstance(new_values['data']['name'], str) or \
1765
                len(str.strip(new_values['data']['name'])) == 0:
1766
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1767
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1768
        name = str.strip(new_values['data']['name'])
1769
1770
        cnx = mysql.connector.connect(**config.myems_system_db)
1771
        cursor = cnx.cursor()
1772
1773
        cursor.execute(" SELECT name "
1774
                       " FROM tbl_energy_storage_containers "
1775
                       " WHERE id = %s ",
1776
                       (id_,))
1777
        if cursor.fetchone() is None:
1778
            cursor.close()
1779
            cnx.close()
1780
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1781
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1782
1783
        cursor.execute(" SELECT name "
1784
                       " FROM tbl_energy_storage_containers_dcdcs "
1785
                       " WHERE energy_storage_container_id = %s AND name = %s ",
1786
                       (id_, name,))
1787
        if cursor.fetchone() is not None:
1788
            cursor.close()
1789
            cnx.close()
1790
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1791
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
1792
1793
        add_values = (" INSERT INTO tbl_energy_storage_containers_dcdcs "
1794
                      "    (name, uuid, energy_storage_container_id) "
1795
                      " VALUES (%s, %s, %s) ")
1796
        cursor.execute(add_values, (name,
1797
                                    str(uuid.uuid4()),
1798
                                    id_
1799
                                    ))
1800
        new_id = cursor.lastrowid
1801
        cnx.commit()
1802
        cursor.close()
1803
        cnx.close()
1804
1805
        resp.status = falcon.HTTP_201
1806
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1807
1808
1809
class EnergyStorageContainerDCDCItem: