Code Duplication    Length = 119-119 lines in 4 locations

myems-api/core/energystoragecontainer.py 4 locations

@@ 5258-5376 (lines=119) @@
5255
        resp.status = falcon.HTTP_200
5256
5257
5258
class EnergyStorageContainerSTSCollection:
5259
    def __init__(self):
5260
        """Initializes Class"""
5261
        pass
5262
5263
    @staticmethod
5264
    def on_options(req, resp, id_):
5265
        resp.status = falcon.HTTP_200
5266
5267
    @staticmethod
5268
    def on_get(req, resp, id_):
5269
        access_control(req)
5270
        if not id_.isdigit() or int(id_) <= 0:
5271
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5272
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5273
5274
        cnx = mysql.connector.connect(**config.myems_system_db)
5275
        cursor = cnx.cursor()
5276
5277
        cursor.execute(" SELECT name "
5278
                       " FROM tbl_energy_storage_containers "
5279
                       " WHERE id = %s ", (id_,))
5280
        if cursor.fetchone() is None:
5281
            cursor.close()
5282
            cnx.close()
5283
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5284
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5285
5286
        query = (" SELECT id, name, uuid "
5287
                 " FROM tbl_energy_storage_containers_stses "
5288
                 " WHERE energy_storage_container_id = %s "
5289
                 " ORDER BY name ")
5290
        cursor.execute(query, (id_,))
5291
        rows = cursor.fetchall()
5292
5293
        result = list()
5294
        if rows is not None and len(rows) > 0:
5295
            for row in rows:
5296
                meta_result = {"id": row[0],
5297
                               "name": row[1],
5298
                               "uuid": row[2]
5299
                               }
5300
                result.append(meta_result)
5301
5302
        resp.text = json.dumps(result)
5303
5304
    @staticmethod
5305
    @user_logger
5306
    def on_post(req, resp, id_):
5307
        """Handles POST requests"""
5308
        admin_control(req)
5309
        try:
5310
            raw_json = req.stream.read().decode('utf-8')
5311
        except Exception as ex:
5312
            raise falcon.HTTPError(status=falcon.HTTP_400,
5313
                                   title='API.BAD_REQUEST',
5314
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
5315
        if not id_.isdigit() or int(id_) <= 0:
5316
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5317
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5318
5319
        cnx = mysql.connector.connect(**config.myems_system_db)
5320
        cursor = cnx.cursor()
5321
5322
        cursor.execute(" SELECT name "
5323
                       " FROM tbl_energy_storage_containers "
5324
                       " WHERE id = %s ", (id_,))
5325
        if cursor.fetchone() is None:
5326
            cursor.close()
5327
            cnx.close()
5328
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5329
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5330
5331
        new_values = json.loads(raw_json)
5332
5333
        if 'name' not in new_values['data'].keys() or \
5334
                not isinstance(new_values['data']['name'], str) or \
5335
                len(str.strip(new_values['data']['name'])) == 0:
5336
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5337
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_STS_NAME')
5338
        name = str.strip(new_values['data']['name'])
5339
5340
        cnx = mysql.connector.connect(**config.myems_system_db)
5341
        cursor = cnx.cursor()
5342
5343
        cursor.execute(" SELECT name "
5344
                       " FROM tbl_energy_storage_containers "
5345
                       " WHERE id = %s ",
5346
                       (id_,))
5347
        if cursor.fetchone() is None:
5348
            cursor.close()
5349
            cnx.close()
5350
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5351
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5352
5353
        cursor.execute(" SELECT name "
5354
                       " FROM tbl_energy_storage_containers_stses "
5355
                       " WHERE energy_storage_container_id = %s AND name = %s ",
5356
                       (id_, name,))
5357
        if cursor.fetchone() is not None:
5358
            cursor.close()
5359
            cnx.close()
5360
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5361
                                   description='API.ENERGY_STORAGE_CONTAINER_STS_NAME_IS_ALREADY_IN_USE')
5362
5363
        add_values = (" INSERT INTO tbl_energy_storage_containers_stses "
5364
                      "    (name, uuid, energy_storage_container_id) "
5365
                      " VALUES (%s, %s, %s) ")
5366
        cursor.execute(add_values, (name,
5367
                                    str(uuid.uuid4()),
5368
                                    id_
5369
                                    ))
5370
        new_id = cursor.lastrowid
5371
        cnx.commit()
5372
        cursor.close()
5373
        cnx.close()
5374
5375
        resp.status = falcon.HTTP_201
5376
        resp.location = '/energystoragecontainers/' + str(id_) + '/stses/' + str(new_id)
5377
5378
5379
class EnergyStorageContainerSTSItem:
@@ 3286-3404 (lines=119) @@
3283
        resp.status = falcon.HTTP_204
3284
3285
3286
class EnergyStorageContainerHVACCollection:
3287
    def __init__(self):
3288
        """Initializes Class"""
3289
        pass
3290
3291
    @staticmethod
3292
    def on_options(req, resp, id_):
3293
        resp.status = falcon.HTTP_200
3294
3295
    @staticmethod
3296
    def on_get(req, resp, id_):
3297
        access_control(req)
3298
        if not id_.isdigit() or int(id_) <= 0:
3299
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3300
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3301
3302
        cnx = mysql.connector.connect(**config.myems_system_db)
3303
        cursor = cnx.cursor()
3304
3305
        cursor.execute(" SELECT name "
3306
                       " FROM tbl_energy_storage_containers "
3307
                       " WHERE id = %s ", (id_,))
3308
        if cursor.fetchone() is None:
3309
            cursor.close()
3310
            cnx.close()
3311
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3312
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3313
3314
        query = (" SELECT id, name, uuid "
3315
                 " FROM tbl_energy_storage_containers_hvacs "
3316
                 " WHERE energy_storage_container_id = %s "
3317
                 " ORDER BY name ")
3318
        cursor.execute(query, (id_,))
3319
        rows = cursor.fetchall()
3320
3321
        result = list()
3322
        if rows is not None and len(rows) > 0:
3323
            for row in rows:
3324
                meta_result = {"id": row[0],
3325
                               "name": row[1],
3326
                               "uuid": row[2]
3327
                               }
3328
                result.append(meta_result)
3329
3330
        resp.text = json.dumps(result)
3331
3332
    @staticmethod
3333
    @user_logger
3334
    def on_post(req, resp, id_):
3335
        """Handles POST requests"""
3336
        admin_control(req)
3337
        try:
3338
            raw_json = req.stream.read().decode('utf-8')
3339
        except Exception as ex:
3340
            raise falcon.HTTPError(status=falcon.HTTP_400,
3341
                                   title='API.BAD_REQUEST',
3342
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
3343
        if not id_.isdigit() or int(id_) <= 0:
3344
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3345
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
3346
3347
        cnx = mysql.connector.connect(**config.myems_system_db)
3348
        cursor = cnx.cursor()
3349
3350
        cursor.execute(" SELECT name "
3351
                       " FROM tbl_energy_storage_containers "
3352
                       " WHERE id = %s ", (id_,))
3353
        if cursor.fetchone() is None:
3354
            cursor.close()
3355
            cnx.close()
3356
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3357
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3358
3359
        new_values = json.loads(raw_json)
3360
3361
        if 'name' not in new_values['data'].keys() or \
3362
                not isinstance(new_values['data']['name'], str) or \
3363
                len(str.strip(new_values['data']['name'])) == 0:
3364
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3365
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_HVAC_NAME')
3366
        name = str.strip(new_values['data']['name'])
3367
3368
        cnx = mysql.connector.connect(**config.myems_system_db)
3369
        cursor = cnx.cursor()
3370
3371
        cursor.execute(" SELECT name "
3372
                       " FROM tbl_energy_storage_containers "
3373
                       " WHERE id = %s ",
3374
                       (id_,))
3375
        if cursor.fetchone() is None:
3376
            cursor.close()
3377
            cnx.close()
3378
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
3379
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
3380
3381
        cursor.execute(" SELECT name "
3382
                       " FROM tbl_energy_storage_containers_hvacs "
3383
                       " WHERE energy_storage_container_id = %s AND name = %s ",
3384
                       (id_, name,))
3385
        if cursor.fetchone() is not None:
3386
            cursor.close()
3387
            cnx.close()
3388
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
3389
                                   description='API.ENERGY_STORAGE_CONTAINER_HVAC_NAME_IS_ALREADY_IN_USE')
3390
3391
        add_values = (" INSERT INTO tbl_energy_storage_containers_hvacs "
3392
                      "    (name, uuid, energy_storage_container_id) "
3393
                      " VALUES (%s, %s, %s) ")
3394
        cursor.execute(add_values, (name,
3395
                                    str(uuid.uuid4()),
3396
                                    id_
3397
                                    ))
3398
        new_id = cursor.lastrowid
3399
        cnx.commit()
3400
        cursor.close()
3401
        cnx.close()
3402
3403
        resp.status = falcon.HTTP_201
3404
        resp.location = '/energystoragecontainers/' + str(id_) + '/hvacs/' + str(new_id)
3405
3406
3407
class EnergyStorageContainerHVACItem:
@@ 2143-2261 (lines=119) @@
2140
        resp.status = falcon.HTTP_204
2141
2142
2143
class EnergyStorageContainerFirecontrolCollection:
2144
    def __init__(self):
2145
        """Initializes Class"""
2146
        pass
2147
2148
    @staticmethod
2149
    def on_options(req, resp, id_):
2150
        resp.status = falcon.HTTP_200
2151
2152
    @staticmethod
2153
    def on_get(req, resp, id_):
2154
        access_control(req)
2155
        if not id_.isdigit() or int(id_) <= 0:
2156
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2157
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2158
2159
        cnx = mysql.connector.connect(**config.myems_system_db)
2160
        cursor = cnx.cursor()
2161
2162
        cursor.execute(" SELECT name "
2163
                       " FROM tbl_energy_storage_containers "
2164
                       " WHERE id = %s ", (id_,))
2165
        if cursor.fetchone() is None:
2166
            cursor.close()
2167
            cnx.close()
2168
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2169
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2170
2171
        query = (" SELECT id, name, uuid "
2172
                 " FROM tbl_energy_storage_containers_firecontrols "
2173
                 " WHERE energy_storage_container_id = %s "
2174
                 " ORDER BY name ")
2175
        cursor.execute(query, (id_,))
2176
        rows = cursor.fetchall()
2177
2178
        result = list()
2179
        if rows is not None and len(rows) > 0:
2180
            for row in rows:
2181
                meta_result = {"id": row[0],
2182
                               "name": row[1],
2183
                               "uuid": row[2]
2184
                               }
2185
                result.append(meta_result)
2186
2187
        resp.text = json.dumps(result)
2188
2189
    @staticmethod
2190
    @user_logger
2191
    def on_post(req, resp, id_):
2192
        """Handles POST requests"""
2193
        admin_control(req)
2194
        try:
2195
            raw_json = req.stream.read().decode('utf-8')
2196
        except Exception as ex:
2197
            raise falcon.HTTPError(status=falcon.HTTP_400,
2198
                                   title='API.BAD_REQUEST',
2199
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
2200
        if not id_.isdigit() or int(id_) <= 0:
2201
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2202
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
2203
2204
        cnx = mysql.connector.connect(**config.myems_system_db)
2205
        cursor = cnx.cursor()
2206
2207
        cursor.execute(" SELECT name "
2208
                       " FROM tbl_energy_storage_containers "
2209
                       " WHERE id = %s ", (id_,))
2210
        if cursor.fetchone() is None:
2211
            cursor.close()
2212
            cnx.close()
2213
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2214
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2215
2216
        new_values = json.loads(raw_json)
2217
2218
        if 'name' not in new_values['data'].keys() or \
2219
                not isinstance(new_values['data']['name'], str) or \
2220
                len(str.strip(new_values['data']['name'])) == 0:
2221
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2222
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME')
2223
        name = str.strip(new_values['data']['name'])
2224
2225
        cnx = mysql.connector.connect(**config.myems_system_db)
2226
        cursor = cnx.cursor()
2227
2228
        cursor.execute(" SELECT name "
2229
                       " FROM tbl_energy_storage_containers "
2230
                       " WHERE id = %s ",
2231
                       (id_,))
2232
        if cursor.fetchone() is None:
2233
            cursor.close()
2234
            cnx.close()
2235
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
2236
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
2237
2238
        cursor.execute(" SELECT name "
2239
                       " FROM tbl_energy_storage_containers_firecontrols "
2240
                       " WHERE energy_storage_container_id = %s AND name = %s ",
2241
                       (id_, name,))
2242
        if cursor.fetchone() is not None:
2243
            cursor.close()
2244
            cnx.close()
2245
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
2246
                                   description='API.ENERGY_STORAGE_CONTAINER_FIRECONTROL_NAME_IS_ALREADY_IN_USE')
2247
2248
        add_values = (" INSERT INTO tbl_energy_storage_containers_firecontrols "
2249
                      "    (name, uuid, energy_storage_container_id) "
2250
                      " VALUES (%s, %s, %s) ")
2251
        cursor.execute(add_values, (name,
2252
                                    str(uuid.uuid4()),
2253
                                    id_
2254
                                    ))
2255
        new_id = cursor.lastrowid
2256
        cnx.commit()
2257
        cursor.close()
2258
        cnx.close()
2259
2260
        resp.status = falcon.HTTP_201
2261
        resp.location = '/energystoragecontainers/' + str(id_) + '/firecontrols/' + str(new_id)
2262
2263
2264
class EnergyStorageContainerFirecontrolItem:
@@ 1664-1782 (lines=119) @@
1661
        resp.status = falcon.HTTP_204
1662
1663
1664
class EnergyStorageContainerDCDCCollection:
1665
    def __init__(self):
1666
        """Initializes Class"""
1667
        pass
1668
1669
    @staticmethod
1670
    def on_options(req, resp, id_):
1671
        resp.status = falcon.HTTP_200
1672
1673
    @staticmethod
1674
    def on_get(req, resp, id_):
1675
        access_control(req)
1676
        if not id_.isdigit() or int(id_) <= 0:
1677
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1678
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1679
1680
        cnx = mysql.connector.connect(**config.myems_system_db)
1681
        cursor = cnx.cursor()
1682
1683
        cursor.execute(" SELECT name "
1684
                       " FROM tbl_energy_storage_containers "
1685
                       " WHERE id = %s ", (id_,))
1686
        if cursor.fetchone() is None:
1687
            cursor.close()
1688
            cnx.close()
1689
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1690
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1691
1692
        query = (" SELECT id, name, uuid "
1693
                 " FROM tbl_energy_storage_containers_dcdcs "
1694
                 " WHERE energy_storage_container_id = %s "
1695
                 " ORDER BY name ")
1696
        cursor.execute(query, (id_,))
1697
        rows = cursor.fetchall()
1698
1699
        result = list()
1700
        if rows is not None and len(rows) > 0:
1701
            for row in rows:
1702
                meta_result = {"id": row[0],
1703
                               "name": row[1],
1704
                               "uuid": row[2]
1705
                               }
1706
                result.append(meta_result)
1707
1708
        resp.text = json.dumps(result)
1709
1710
    @staticmethod
1711
    @user_logger
1712
    def on_post(req, resp, id_):
1713
        """Handles POST requests"""
1714
        admin_control(req)
1715
        try:
1716
            raw_json = req.stream.read().decode('utf-8')
1717
        except Exception as ex:
1718
            raise falcon.HTTPError(status=falcon.HTTP_400,
1719
                                   title='API.BAD_REQUEST',
1720
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
1721
        if not id_.isdigit() or int(id_) <= 0:
1722
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1723
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
1724
1725
        cnx = mysql.connector.connect(**config.myems_system_db)
1726
        cursor = cnx.cursor()
1727
1728
        cursor.execute(" SELECT name "
1729
                       " FROM tbl_energy_storage_containers "
1730
                       " WHERE id = %s ", (id_,))
1731
        if cursor.fetchone() is None:
1732
            cursor.close()
1733
            cnx.close()
1734
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1735
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
1736
1737
        new_values = json.loads(raw_json)
1738
1739
        if 'name' not in new_values['data'].keys() or \
1740
                not isinstance(new_values['data']['name'], str) or \
1741
                len(str.strip(new_values['data']['name'])) == 0:
1742
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1743
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_DCDC_NAME')
1744
        name = str.strip(new_values['data']['name'])
1745
1746
        cnx = mysql.connector.connect(**config.myems_system_db)
1747
        cursor = cnx.cursor()
1748
1749
        cursor.execute(" SELECT name "
1750
                       " FROM tbl_energy_storage_containers "
1751
                       " WHERE id = %s ",
1752
                       (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
        cursor.execute(" SELECT name "
1760
                       " FROM tbl_energy_storage_containers_dcdcs "
1761
                       " WHERE energy_storage_container_id = %s AND name = %s ",
1762
                       (id_, name,))
1763
        if cursor.fetchone() is not None:
1764
            cursor.close()
1765
            cnx.close()
1766
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
1767
                                   description='API.ENERGY_STORAGE_CONTAINER_DCDC_NAME_IS_ALREADY_IN_USE')
1768
1769
        add_values = (" INSERT INTO tbl_energy_storage_containers_dcdcs "
1770
                      "    (name, uuid, energy_storage_container_id) "
1771
                      " VALUES (%s, %s, %s) ")
1772
        cursor.execute(add_values, (name,
1773
                                    str(uuid.uuid4()),
1774
                                    id_
1775
                                    ))
1776
        new_id = cursor.lastrowid
1777
        cnx.commit()
1778
        cursor.close()
1779
        cnx.close()
1780
1781
        resp.status = falcon.HTTP_201
1782
        resp.location = '/energystoragecontainers/' + str(id_) + '/dcdcs/' + str(new_id)
1783
1784
1785
class EnergyStorageContainerDCDCItem: