Code Duplication    Length = 77-79 lines in 2 locations

myems-api/core/microgrid.py 1 location

@@ 299-377 (lines=79) @@
296
        resp.status = falcon.HTTP_200
297
        _ = id_
298
299
    @staticmethod
300
    def on_get(req, resp, id_):
301
        access_control(req)
302
        if not id_.isdigit() or int(id_) <= 0:
303
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
304
                                   description='API.INVALID_MICROGRID_ID')
305
306
        cnx = mysql.connector.connect(**config.myems_system_db)
307
        cursor = cnx.cursor()
308
309
        query = (" SELECT id, name, uuid "
310
                 " FROM tbl_contacts ")
311
        cursor.execute(query)
312
        rows_contacts = cursor.fetchall()
313
314
        contact_dict = dict()
315
        if rows_contacts is not None and len(rows_contacts) > 0:
316
            for row in rows_contacts:
317
                contact_dict[row[0]] = {"id": row[0],
318
                                        "name": row[1],
319
                                        "uuid": row[2]}
320
321
        query = (" SELECT id, name, uuid "
322
                 " FROM tbl_cost_centers ")
323
        cursor.execute(query)
324
        rows_cost_centers = cursor.fetchall()
325
326
        cost_center_dict = dict()
327
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
328
            for row in rows_cost_centers:
329
                cost_center_dict[row[0]] = {"id": row[0],
330
                                            "name": row[1],
331
                                            "uuid": row[2]}
332
        svg_dict = dict()
333
334
        query = (" SELECT id, name, uuid "
335
                 " FROM tbl_svgs ")
336
        cursor.execute(query)
337
        rows_svgs = cursor.fetchall()
338
        if rows_svgs is not None and len(rows_svgs) > 0:
339
            for row in rows_svgs:
340
                svg_dict[row[0]] = {"id": row[0],
341
                                    "name": row[1],
342
                                    "uuid": row[2]}
343
344
        query = (" SELECT id, name, uuid, "
345
                 "        address, postal_code, latitude, longitude, rated_capacity, rated_power, "
346
                 "        contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, "
347
                 "        phase_of_lifecycle, description "
348
                 " FROM tbl_microgrids "
349
                 " WHERE id = %s ")
350
        cursor.execute(query, (id_,))
351
        row = cursor.fetchone()
352
        cursor.close()
353
        cnx.close()
354
355
        if row is None:
356
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
357
                                   description='API.MICROGRID_NOT_FOUND')
358
        else:
359
            meta_result = {"id": row[0],
360
                           "name": row[1],
361
                           "uuid": row[2],
362
                           "address": row[3],
363
                           "postal_code": row[4],
364
                           "latitude": row[5],
365
                           "longitude": row[6],
366
                           "rated_capacity": row[7],
367
                           "rated_power": row[8],
368
                           "contact": contact_dict.get(row[9], None),
369
                           "cost_center": cost_center_dict.get(row[10], None),
370
                           "serial_number": row[11],
371
                           "svg": svg_dict.get(row[12], None),
372
                           "is_cost_data_displayed": bool(row[13]),
373
                           "phase_of_lifecycle": row[14],
374
                           "description": row[15],
375
                           "qrcode": 'microgrid:' + row[2]}
376
377
        resp.text = json.dumps(meta_result)
378
379
    @staticmethod
380
    @user_logger

myems-api/core/photovoltaicpowerstation.py 1 location

@@ 291-367 (lines=77) @@
288
        resp.status = falcon.HTTP_200
289
        _ = id_
290
291
    @staticmethod
292
    def on_get(req, resp, id_):
293
        access_control(req)
294
        if not id_.isdigit() or int(id_) <= 0:
295
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
296
                                   description='API.INVALID_PHOTOVOLTAIC_POWER_STATION_ID')
297
298
        cnx = mysql.connector.connect(**config.myems_system_db)
299
        cursor = cnx.cursor()
300
301
        query = (" SELECT id, name, uuid "
302
                 " FROM tbl_contacts ")
303
        cursor.execute(query)
304
        rows_contacts = cursor.fetchall()
305
306
        contact_dict = dict()
307
        if rows_contacts is not None and len(rows_contacts) > 0:
308
            for row in rows_contacts:
309
                contact_dict[row[0]] = {"id": row[0],
310
                                        "name": row[1],
311
                                        "uuid": row[2]}
312
313
        query = (" SELECT id, name, uuid "
314
                 " FROM tbl_cost_centers ")
315
        cursor.execute(query)
316
        rows_cost_centers = cursor.fetchall()
317
318
        cost_center_dict = dict()
319
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
320
            for row in rows_cost_centers:
321
                cost_center_dict[row[0]] = {"id": row[0],
322
                                            "name": row[1],
323
                                            "uuid": row[2]}
324
325
        svg_dict = dict()
326
        query = (" SELECT id, name, uuid "
327
                 " FROM tbl_svgs ")
328
        cursor.execute(query)
329
        rows_svgs = cursor.fetchall()
330
        if rows_svgs is not None and len(rows_svgs) > 0:
331
            for row in rows_svgs:
332
                svg_dict[row[0]] = {"id": row[0],
333
                                    "name": row[1],
334
                                    "uuid": row[2]}
335
336
        query = (" SELECT id, name, uuid, "
337
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
338
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
339
                 " FROM tbl_photovoltaic_power_stations "
340
                 " WHERE id = %s ")
341
        cursor.execute(query, (id_,))
342
        row = cursor.fetchone()
343
        cursor.close()
344
        cnx.close()
345
346
        if row is None:
347
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
348
                                   description='API.PHOTOVOLTAIC_POWER_STATION_NOT_FOUND')
349
        else:
350
            meta_result = {"id": row[0],
351
                           "name": row[1],
352
                           "uuid": row[2],
353
                           "station_code": row[3],
354
                           "address": row[4],
355
                           "latitude": row[5],
356
                           "longitude": row[6],
357
                           "rated_capacity": row[7],
358
                           "rated_power": row[8],
359
                           "contact": contact_dict.get(row[9], None),
360
                           "cost_center": cost_center_dict.get(row[10], None),
361
                           "svg": svg_dict.get(row[11], None),
362
                           "is_cost_data_displayed": bool(row[12]),
363
                           "phase_of_lifecycle": row[13],
364
                           "description": row[14],
365
                           "qrcode": 'photovoltaicpowerstation:' + row[2]}
366
367
        resp.text = json.dumps(meta_result)
368
369
    @staticmethod
370
    @user_logger