Code Duplication    Length = 77-79 lines in 2 locations

myems-api/core/microgrid.py 1 location

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

myems-api/core/photovoltaicpowerstation.py 1 location

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