Code Duplication    Length = 122-122 lines in 4 locations

myems-api/core/energystoragecontainer.py 4 locations

@@ 5370-5491 (lines=122) @@
5367
        resp.status = falcon.HTTP_200
5368
5369
5370
class EnergyStorageContainerSTSCollection:
5371
    def __init__(self):
5372
        pass
5373
5374
    @staticmethod
5375
    def on_options(req, resp, id_):
5376
        _ = req
5377
        resp.status = falcon.HTTP_200
5378
        _ = id_
5379
5380
    @staticmethod
5381
    def on_get(req, resp, id_):
5382
        access_control(req)
5383
        if not id_.isdigit() or int(id_) <= 0:
5384
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5385
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5386
5387
        cnx = mysql.connector.connect(**config.myems_system_db)
5388
        cursor = cnx.cursor()
5389
5390
        cursor.execute(" SELECT name "
5391
                       " FROM tbl_energy_storage_containers "
5392
                       " WHERE id = %s ", (id_,))
5393
        if cursor.fetchone() is None:
5394
            cursor.close()
5395
            cnx.close()
5396
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5397
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5398
5399
        query = (" SELECT id, name, uuid "
5400
                 " FROM tbl_energy_storage_containers_stses "
5401
                 " WHERE energy_storage_container_id = %s "
5402
                 " ORDER BY name ")
5403
        cursor.execute(query, (id_,))
5404
        rows = cursor.fetchall()
5405
5406
        result = list()
5407
        if rows is not None and len(rows) > 0:
5408
            for row in rows:
5409
                meta_result = {"id": row[0],
5410
                               "name": row[1],
5411
                               "uuid": row[2]
5412
                               }
5413
                result.append(meta_result)
5414
5415
        resp.text = json.dumps(result)
5416
5417
    @staticmethod
5418
    @user_logger
5419
    def on_post(req, resp, id_):
5420
        """Handles POST requests"""
5421
        admin_control(req)
5422
        try:
5423
            raw_json = req.stream.read().decode('utf-8')
5424
        except Exception as ex:
5425
            print(str(ex))
5426
            raise falcon.HTTPError(status=falcon.HTTP_400,
5427
                                   title='API.BAD_REQUEST',
5428
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5429
        if not id_.isdigit() or int(id_) <= 0:
5430
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5431
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5432
5433
        cnx = mysql.connector.connect(**config.myems_system_db)
5434
        cursor = cnx.cursor()
5435
5436
        cursor.execute(" SELECT name "
5437
                       " FROM tbl_energy_storage_containers "
5438
                       " WHERE id = %s ", (id_,))
5439
        if cursor.fetchone() is None:
5440
            cursor.close()
5441
            cnx.close()
5442
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5443
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5444
5445
        new_values = json.loads(raw_json)
5446
5447
        if 'name' not in new_values['data'].keys() or \
5448
                not isinstance(new_values['data']['name'], str) or \
5449
                len(str.strip(new_values['data']['name'])) == 0:
5450
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5451
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME')
5452
        name = str.strip(new_values['data']['name'])
5453
5454
        cnx = mysql.connector.connect(**config.myems_system_db)
5455
        cursor = cnx.cursor()
5456
5457
        cursor.execute(" SELECT name "
5458
                       " FROM tbl_energy_storage_containers "
5459
                       " WHERE id = %s ",
5460
                       (id_,))
5461
        if cursor.fetchone() is None:
5462
            cursor.close()
5463
            cnx.close()
5464
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5465
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5466
5467
        cursor.execute(" SELECT name "
5468
                       " FROM tbl_energy_storage_containers_stses "
5469
                       " WHERE energy_storage_container_id = %s AND name = %s ",
5470
                       (id_, name,))
5471
        if cursor.fetchone() is not None:
5472
            cursor.close()
5473
            cnx.close()
5474
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5475
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE')
5476
5477
        add_values = (" INSERT INTO tbl_energy_storage_containers_stses "
5478
                      "    (name, uuid, energy_storage_container_id) "
5479
                      " VALUES (%s, %s, %s) ")
5480
        cursor.execute(add_values, (name,
5481
                                    str(uuid.uuid4()),
5482
                                    id_
5483
                                    ))
5484
        new_id = cursor.lastrowid
5485
        cnx.commit()
5486
        cursor.close()
5487
        cnx.close()
5488
5489
        resp.status = falcon.HTTP_201
5490
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id)
5491
5492
5493
class EnergyStorageContainerSTSItem:
5494
    def __init__(self):
@@ 3373-3494 (lines=122) @@
3370
        resp.status = falcon.HTTP_204
3371
3372
3373
class EnergyStorageContainerHVACCollection:
3374
    def __init__(self):
3375
        pass
3376
3377
    @staticmethod
3378
    def on_options(req, resp, id_):
3379
        _ = req
3380
        resp.status = falcon.HTTP_200
3381
        _ = id_
3382
3383
    @staticmethod
3384
    def on_get(req, resp, id_):
3385
        access_control(req)
3386
        if not id_.isdigit() or int(id_) <= 0:
3387
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3388
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3389
3390
        cnx = mysql.connector.connect(**config.myems_system_db)
3391
        cursor = cnx.cursor()
3392
3393
        cursor.execute(" SELECT name "
3394
                       " FROM tbl_energy_storage_containers "
3395
                       " WHERE id = %s ", (id_,))
3396
        if cursor.fetchone() is None:
3397
            cursor.close()
3398
            cnx.close()
3399
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3400
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3401
3402
        query = (" SELECT id, name, uuid "
3403
                 " FROM tbl_energy_storage_containers_hvacs "
3404
                 " WHERE energy_storage_container_id = %s "
3405
                 " ORDER BY name ")
3406
        cursor.execute(query, (id_,))
3407
        rows = cursor.fetchall()
3408
3409
        result = list()
3410
        if rows is not None and len(rows) > 0:
3411
            for row in rows:
3412
                meta_result = {"id": row[0],
3413
                               "name": row[1],
3414
                               "uuid": row[2]
3415
                               }
3416
                result.append(meta_result)
3417
3418
        resp.text = json.dumps(result)
3419
3420
    @staticmethod
3421
    @user_logger
3422
    def on_post(req, resp, id_):
3423
        """Handles POST requests"""
3424
        admin_control(req)
3425
        try:
3426
            raw_json = req.stream.read().decode('utf-8')
3427
        except Exception as ex:
3428
            print(str(ex))
3429
            raise falcon.HTTPError(status=falcon.HTTP_400,
3430
                                   title='API.BAD_REQUEST',
3431
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3432
        if not id_.isdigit() or int(id_) <= 0:
3433
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3434
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3435
3436
        cnx = mysql.connector.connect(**config.myems_system_db)
3437
        cursor = cnx.cursor()
3438
3439
        cursor.execute(" SELECT name "
3440
                       " FROM tbl_energy_storage_containers "
3441
                       " WHERE id = %s ", (id_,))
3442
        if cursor.fetchone() is None:
3443
            cursor.close()
3444
            cnx.close()
3445
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3446
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3447
3448
        new_values = json.loads(raw_json)
3449
3450
        if 'name' not in new_values['data'].keys() or \
3451
                not isinstance(new_values['data']['name'], str) or \
3452
                len(str.strip(new_values['data']['name'])) == 0:
3453
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3454
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
3455
        name = str.strip(new_values['data']['name'])
3456
3457
        cnx = mysql.connector.connect(**config.myems_system_db)
3458
        cursor = cnx.cursor()
3459
3460
        cursor.execute(" SELECT name "
3461
                       " FROM tbl_energy_storage_containers "
3462
                       " WHERE id = %s ",
3463
                       (id_,))
3464
        if cursor.fetchone() is None:
3465
            cursor.close()
3466
            cnx.close()
3467
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3468
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3469
3470
        cursor.execute(" SELECT name "
3471
                       " FROM tbl_energy_storage_containers_hvacs "
3472
                       " WHERE energy_storage_container_id = %s AND name = %s ",
3473
                       (id_, name,))
3474
        if cursor.fetchone() is not None:
3475
            cursor.close()
3476
            cnx.close()
3477
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3478
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
3479
3480
        add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs "
3481
                      "    (name, uuid, energy_storage_container_id) "
3482
                      " VALUES (%s, %s, %s) ")
3483
        cursor.execute(add_values, (name,
3484
                                    str(uuid.uuid4()),
3485
                                    id_
3486
                                    ))
3487
        new_id = cursor.lastrowid
3488
        cnx.commit()
3489
        cursor.close()
3490
        cnx.close()
3491
3492
        resp.status = falcon.HTTP_201
3493
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
3494
3495
3496
class EnergyStorageContainerHVACItem:
3497
    def __init__(self):
@@ 2216-2337 (lines=122) @@
2213
        resp.status = falcon.HTTP_204
2214
2215
2216
class EnergyStorageContainerFirecontrolCollection:
2217
    def __init__(self):
2218
        pass
2219
2220
    @staticmethod
2221
    def on_options(req, resp, id_):
2222
        _ = req
2223
        resp.status = falcon.HTTP_200
2224
        _ = id_
2225
2226
    @staticmethod
2227
    def on_get(req, resp, id_):
2228
        access_control(req)
2229
        if not id_.isdigit() or int(id_) <= 0:
2230
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2231
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2232
2233
        cnx = mysql.connector.connect(**config.myems_system_db)
2234
        cursor = cnx.cursor()
2235
2236
        cursor.execute(" SELECT name "
2237
                       " FROM tbl_energy_storage_containers "
2238
                       " WHERE id = %s ", (id_,))
2239
        if cursor.fetchone() is None:
2240
            cursor.close()
2241
            cnx.close()
2242
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2243
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2244
2245
        query = (" SELECT id, name, uuid "
2246
                 " FROM tbl_energy_storage_containers_firecontrols "
2247
                 " WHERE energy_storage_container_id = %s "
2248
                 " ORDER BY name ")
2249
        cursor.execute(query, (id_,))
2250
        rows = cursor.fetchall()
2251
2252
        result = list()
2253
        if rows is not None and len(rows) > 0:
2254
            for row in rows:
2255
                meta_result = {"id": row[0],
2256
                               "name": row[1],
2257
                               "uuid": row[2]
2258
                               }
2259
                result.append(meta_result)
2260
2261
        resp.text = json.dumps(result)
2262
2263
    @staticmethod
2264
    @user_logger
2265
    def on_post(req, resp, id_):
2266
        """Handles POST requests"""
2267
        admin_control(req)
2268
        try:
2269
            raw_json = req.stream.read().decode('utf-8')
2270
        except Exception as ex:
2271
            print(str(ex))
2272
            raise falcon.HTTPError(status=falcon.HTTP_400,
2273
                                   title='API.BAD_REQUEST',
2274
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2275
        if not id_.isdigit() or int(id_) <= 0:
2276
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2277
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2278
2279
        cnx = mysql.connector.connect(**config.myems_system_db)
2280
        cursor = cnx.cursor()
2281
2282
        cursor.execute(" SELECT name "
2283
                       " FROM tbl_energy_storage_containers "
2284
                       " WHERE id = %s ", (id_,))
2285
        if cursor.fetchone() is None:
2286
            cursor.close()
2287
            cnx.close()
2288
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2289
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2290
2291
        new_values = json.loads(raw_json)
2292
2293
        if 'name' not in new_values['data'].keys() or \
2294
                not isinstance(new_values['data']['name'], str) or \
2295
                len(str.strip(new_values['data']['name'])) == 0:
2296
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2297
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
2298
        name = str.strip(new_values['data']['name'])
2299
2300
        cnx = mysql.connector.connect(**config.myems_system_db)
2301
        cursor = cnx.cursor()
2302
2303
        cursor.execute(" SELECT name "
2304
                       " FROM tbl_energy_storage_containers "
2305
                       " WHERE id = %s ",
2306
                       (id_,))
2307
        if cursor.fetchone() is None:
2308
            cursor.close()
2309
            cnx.close()
2310
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2311
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2312
2313
        cursor.execute(" SELECT name "
2314
                       " FROM tbl_energy_storage_containers_firecontrols "
2315
                       " WHERE energy_storage_container_id = %s AND name = %s ",
2316
                       (id_, name,))
2317
        if cursor.fetchone() is not None:
2318
            cursor.close()
2319
            cnx.close()
2320
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2321
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
2322
2323
        add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols "
2324
                      "    (name, uuid, energy_storage_container_id) "
2325
                      " VALUES (%s, %s, %s) ")
2326
        cursor.execute(add_values, (name,
2327
                                    str(uuid.uuid4()),
2328
                                    id_
2329
                                    ))
2330
        new_id = cursor.lastrowid
2331
        cnx.commit()
2332
        cursor.close()
2333
        cnx.close()
2334
2335
        resp.status = falcon.HTTP_201
2336
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
2337
2338
2339
class EnergyStorageContainerFirecontrolItem:
2340
    def __init__(self):
@@ 1730-1851 (lines=122) @@
1727
        resp.text = json.dumps(result)
1728
1729
1730
class EnergyStorageContainerDCDCCollection:
1731
    def __init__(self):
1732
        pass
1733
1734
    @staticmethod
1735
    def on_options(req, resp, id_):
1736
        _ = req
1737
        resp.status = falcon.HTTP_200
1738
        _ = id_
1739
1740
    @staticmethod
1741
    def on_get(req, resp, id_):
1742
        access_control(req)
1743
        if not id_.isdigit() or int(id_) <= 0:
1744
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1745
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1746
1747
        cnx = mysql.connector.connect(**config.myems_system_db)
1748
        cursor = cnx.cursor()
1749
1750
        cursor.execute(" SELECT name "
1751
                       " FROM tbl_energy_storage_containers "
1752
                       " WHERE id = %s ", (id_,))
1753
        if cursor.fetchone() is None:
1754
            cursor.close()
1755
            cnx.close()
1756
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1757
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1758
1759
        query = (" SELECT id, name, uuid "
1760
                 " FROM tbl_energy_storage_containers_dcdcs "
1761
                 " WHERE energy_storage_container_id = %s "
1762
                 " ORDER BY name ")
1763
        cursor.execute(query, (id_,))
1764
        rows = cursor.fetchall()
1765
1766
        result = list()
1767
        if rows is not None and len(rows) > 0:
1768
            for row in rows:
1769
                meta_result = {"id": row[0],
1770
                               "name": row[1],
1771
                               "uuid": row[2]
1772
                               }
1773
                result.append(meta_result)
1774
1775
        resp.text = json.dumps(result)
1776
1777
    @staticmethod
1778
    @user_logger
1779
    def on_post(req, resp, id_):
1780
        """Handles POST requests"""
1781
        admin_control(req)
1782
        try:
1783
            raw_json = req.stream.read().decode('utf-8')
1784
        except Exception as ex:
1785
            print(str(ex))
1786
            raise falcon.HTTPError(status=falcon.HTTP_400,
1787
                                   title='API.BAD_REQUEST',
1788
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1789
        if not id_.isdigit() or int(id_) <= 0:
1790
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1791
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1792
1793
        cnx = mysql.connector.connect(**config.myems_system_db)
1794
        cursor = cnx.cursor()
1795
1796
        cursor.execute(" SELECT name "
1797
                       " FROM tbl_energy_storage_containers "
1798
                       " WHERE id = %s ", (id_,))
1799
        if cursor.fetchone() is None:
1800
            cursor.close()
1801
            cnx.close()
1802
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1803
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1804
1805
        new_values = json.loads(raw_json)
1806
1807
        if 'name' not in new_values['data'].keys() or \
1808
                not isinstance(new_values['data']['name'], str) or \
1809
                len(str.strip(new_values['data']['name'])) == 0:
1810
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1811
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1812
        name = str.strip(new_values['data']['name'])
1813
1814
        cnx = mysql.connector.connect(**config.myems_system_db)
1815
        cursor = cnx.cursor()
1816
1817
        cursor.execute(" SELECT name "
1818
                       " FROM tbl_energy_storage_containers "
1819
                       " WHERE id = %s ",
1820
                       (id_,))
1821
        if cursor.fetchone() is None:
1822
            cursor.close()
1823
            cnx.close()
1824
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1825
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1826
1827
        cursor.execute(" SELECT name "
1828
                       " FROM tbl_energy_storage_containers_dcdcs "
1829
                       " WHERE energy_storage_container_id = %s AND name = %s ",
1830
                       (id_, name,))
1831
        if cursor.fetchone() is not None:
1832
            cursor.close()
1833
            cnx.close()
1834
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1835
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
1836
1837
        add_values = (" INSERT INTO tbl_energy_storage_containers_dcdcs "
1838
                      "    (name, uuid, energy_storage_container_id) "
1839
                      " VALUES (%s, %s, %s) ")
1840
        cursor.execute(add_values, (name,
1841
                                    str(uuid.uuid4()),
1842
                                    id_
1843
                                    ))
1844
        new_id = cursor.lastrowid
1845
        cnx.commit()
1846
        cursor.close()
1847
        cnx.close()
1848
1849
        resp.status = falcon.HTTP_201
1850
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1851
1852
1853
class EnergyStorageContainerDCDCItem:
1854
    def __init__(self):