Code Duplication    Length = 57-58 lines in 2 locations

myems-api/core/energystoragecontainer.py 2 locations

@@ 195-252 (lines=58) @@
192
    def on_options(req, resp, id_):
193
        resp.status = falcon.HTTP_200
194
195
    @staticmethod
196
    def on_get(req, resp, id_):
197
        access_control(req)
198
        if not id_.isdigit() or int(id_) <= 0:
199
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
200
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
201
202
        cnx = mysql.connector.connect(**config.myems_system_db)
203
        cursor = cnx.cursor()
204
205
        query = (" SELECT id, name, uuid "
206
                 " FROM tbl_contacts ")
207
        cursor.execute(query)
208
        rows_contacts = cursor.fetchall()
209
210
        contact_dict = dict()
211
        if rows_contacts is not None and len(rows_contacts) > 0:
212
            for row in rows_contacts:
213
                contact_dict[row[0]] = {"id": row[0],
214
                                        "name": row[1],
215
                                        "uuid": row[2]}
216
217
        query = (" SELECT id, name, uuid "
218
                 " FROM tbl_cost_centers ")
219
        cursor.execute(query)
220
        rows_cost_centers = cursor.fetchall()
221
222
        cost_center_dict = dict()
223
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
224
            for row in rows_cost_centers:
225
                cost_center_dict[row[0]] = {"id": row[0],
226
                                            "name": row[1],
227
                                            "uuid": row[2]}
228
229
        query = (" SELECT id, name, uuid, "
230
                 "        rated_capacity, rated_power, contact_id, cost_center_id, description "
231
                 " FROM tbl_energy_storage_containers "
232
                 " WHERE id = %s ")
233
        cursor.execute(query, (id_,))
234
        row = cursor.fetchone()
235
        cursor.close()
236
        cnx.close()
237
238
        if row is None:
239
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
240
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
241
        else:
242
            meta_result = {"id": row[0],
243
                           "name": row[1],
244
                           "uuid": row[2],
245
                           "rated_capacity": row[3],
246
                           "rated_power": row[4],
247
                           "contact": contact_dict.get(row[5], None),
248
                           "cost_center": cost_center_dict.get(row[6], None),
249
                           "description": row[7],
250
                           "qrcode": 'energystoragecontainer:' + row[2]}
251
252
        resp.text = json.dumps(meta_result)
253
254
    @staticmethod
255
    @user_logger
@@ 5332-5388 (lines=57) @@
5329
    def on_options(req, resp, id_):
5330
        resp.status = falcon.HTTP_200
5331
5332
    @staticmethod
5333
    def on_get(req, resp, id_):
5334
        access_control(req)
5335
        if not id_.isdigit() or int(id_) <= 0:
5336
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5337
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5338
5339
        cnx = mysql.connector.connect(**config.myems_system_db)
5340
        cursor = cnx.cursor()
5341
5342
        query = (" SELECT id, name, uuid "
5343
                 " FROM tbl_contacts ")
5344
        cursor.execute(query)
5345
        rows_contacts = cursor.fetchall()
5346
5347
        contact_dict = dict()
5348
        if rows_contacts is not None and len(rows_contacts) > 0:
5349
            for row in rows_contacts:
5350
                contact_dict[row[0]] = {"id": row[0],
5351
                                        "name": row[1],
5352
                                        "uuid": row[2]}
5353
5354
        query = (" SELECT id, name, uuid "
5355
                 " FROM tbl_cost_centers ")
5356
        cursor.execute(query)
5357
        rows_cost_centers = cursor.fetchall()
5358
5359
        cost_center_dict = dict()
5360
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
5361
            for row in rows_cost_centers:
5362
                cost_center_dict[row[0]] = {"id": row[0],
5363
                                            "name": row[1],
5364
                                            "uuid": row[2]}
5365
5366
        query = (" SELECT id, name, uuid, "
5367
                 "        rated_capacity, rated_power, contact_id, cost_center_id, description "
5368
                 " FROM tbl_energy_storage_containers "
5369
                 " WHERE id = %s ")
5370
        cursor.execute(query, (id_,))
5371
        row = cursor.fetchone()
5372
        cursor.close()
5373
        cnx.close()
5374
5375
        if row is None:
5376
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5377
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5378
        else:
5379
            meta_result = {"id": row[0],
5380
                           "name": row[1],
5381
                           "uuid": row[2],
5382
                           "rated_capacity": row[3],
5383
                           "rated_power": row[4],
5384
                           "contact": contact_dict.get(row[5], None),
5385
                           "cost_center": cost_center_dict.get(row[6], None),
5386
                           "description": row[7]}
5387
5388
        resp.text = json.dumps(meta_result)
5389
5390
5391
class EnergyStorageContainerImport: