Code Duplication    Length = 74-75 lines in 2 locations

myems-api/core/microgrid.py 1 location

@@ 19-93 (lines=75) @@
16
    def on_options(req, resp):
17
        resp.status = falcon.HTTP_200
18
19
    @staticmethod
20
    def on_get(req, resp):
21
        access_control(req)
22
        cnx = mysql.connector.connect(**config.myems_system_db)
23
        cursor = cnx.cursor()
24
25
        query = (" SELECT id, name, uuid "
26
                 " FROM tbl_contacts ")
27
        cursor.execute(query)
28
        rows_contacts = cursor.fetchall()
29
30
        contact_dict = dict()
31
        if rows_contacts is not None and len(rows_contacts) > 0:
32
            for row in rows_contacts:
33
                contact_dict[row[0]] = {"id": row[0],
34
                                        "name": row[1],
35
                                        "uuid": row[2]}
36
37
        query = (" SELECT id, name, uuid "
38
                 " FROM tbl_cost_centers ")
39
        cursor.execute(query)
40
        rows_cost_centers = cursor.fetchall()
41
42
        cost_center_dict = dict()
43
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
44
            for row in rows_cost_centers:
45
                cost_center_dict[row[0]] = {"id": row[0],
46
                                            "name": row[1],
47
                                            "uuid": row[2]}
48
        svg_dict = dict()
49
50
        query = (" SELECT id, name, uuid "
51
                 " FROM tbl_svgs ")
52
        cursor.execute(query)
53
        rows_svgs = cursor.fetchall()
54
        if rows_svgs is not None and len(rows_svgs) > 0:
55
            for row in rows_svgs:
56
                svg_dict[row[0]] = {"id": row[0],
57
                                    "name": row[1],
58
                                    "uuid": row[2]}
59
60
        query = (" SELECT id, name, uuid, "
61
                 "        address, postal_code, latitude, longitude, rated_capacity, rated_power, "
62
                 "        contact_id, cost_center_id, serial_number, svg_id, is_cost_data_displayed, "
63
                 "        phase_of_lifecycle, description "
64
                 " FROM tbl_microgrids "
65
                 " ORDER BY id ")
66
        cursor.execute(query)
67
        rows_microgrids = cursor.fetchall()
68
69
        result = list()
70
        if rows_microgrids is not None and len(rows_microgrids) > 0:
71
            for row in rows_microgrids:
72
                meta_result = {"id": row[0],
73
                               "name": row[1],
74
                               "uuid": row[2],
75
                               "address": row[3],
76
                               "postal_code": row[4],
77
                               "latitude": row[5],
78
                               "longitude": row[6],
79
                               "rated_capacity": row[7],
80
                               "rated_power": row[8],
81
                               "contact": contact_dict.get(row[9], None),
82
                               "cost_center": cost_center_dict.get(row[10], None),
83
                               "serial_number": row[11],
84
                               "svg": svg_dict.get(row[12], None),
85
                               "is_cost_data_displayed": bool(row[13]),
86
                               "phase_of_lifecycle": row[14],
87
                               "description": row[15],
88
                               "qrcode": 'microgrid:' + row[2]}
89
                result.append(meta_result)
90
91
        cursor.close()
92
        cnx.close()
93
        resp.text = json.dumps(result)
94
95
    @staticmethod
96
    @user_logger

myems-api/core/photovoltaicpowerstation.py 1 location

@@ 20-93 (lines=74) @@
17
    def on_options(req, resp):
18
        resp.status = falcon.HTTP_200
19
20
    @staticmethod
21
    def on_get(req, resp):
22
        access_control(req)
23
        cnx = mysql.connector.connect(**config.myems_system_db)
24
        cursor = cnx.cursor()
25
26
        query = (" SELECT id, name, uuid "
27
                 " FROM tbl_contacts ")
28
        cursor.execute(query)
29
        rows_contacts = cursor.fetchall()
30
31
        contact_dict = dict()
32
        if rows_contacts is not None and len(rows_contacts) > 0:
33
            for row in rows_contacts:
34
                contact_dict[row[0]] = {"id": row[0],
35
                                        "name": row[1],
36
                                        "uuid": row[2]}
37
38
        query = (" SELECT id, name, uuid "
39
                 " FROM tbl_cost_centers ")
40
        cursor.execute(query)
41
        rows_cost_centers = cursor.fetchall()
42
43
        cost_center_dict = dict()
44
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
45
            for row in rows_cost_centers:
46
                cost_center_dict[row[0]] = {"id": row[0],
47
                                            "name": row[1],
48
                                            "uuid": row[2]}
49
50
        svg_dict = dict()
51
52
        query = (" SELECT id, name, uuid "
53
                 " FROM tbl_svgs ")
54
        cursor.execute(query)
55
        rows_svgs = cursor.fetchall()
56
        if rows_svgs is not None and len(rows_svgs) > 0:
57
            for row in rows_svgs:
58
                svg_dict[row[0]] = {"id": row[0],
59
                                    "name": row[1],
60
                                    "uuid": row[2]}
61
62
        query = (" SELECT id, name, uuid, "
63
                 "        station_code, address, latitude, longitude, rated_capacity, rated_power, "
64
                 "        contact_id, cost_center_id, svg_id, is_cost_data_displayed, phase_of_lifecycle, description "
65
                 " FROM tbl_photovoltaic_power_stations "
66
                 " ORDER BY id ")
67
        cursor.execute(query)
68
        rows_photovoltaic_power_stations = cursor.fetchall()
69
70
        result = list()
71
        if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0:
72
            for row in rows_photovoltaic_power_stations:
73
                meta_result = {"id": row[0],
74
                               "name": row[1],
75
                               "uuid": row[2],
76
                               "station_code": row[3],
77
                               "address": row[4],
78
                               "latitude": row[5],
79
                               "longitude": row[6],
80
                               "rated_capacity": row[7],
81
                               "rated_power": row[8],
82
                               "contact": contact_dict.get(row[9], None),
83
                               "cost_center": cost_center_dict.get(row[10], None),
84
                               "svg": svg_dict.get(row[11], None),
85
                               "is_cost_data_displayed": bool(row[12]),
86
                               "phase_of_lifecycle": row[13],
87
                               "description": row[14],
88
                               "qrcode": 'photovoltaicpowerstation:' + row[2]}
89
                result.append(meta_result)
90
91
        cursor.close()
92
        cnx.close()
93
        resp.text = json.dumps(result)
94
95
    @staticmethod
96
    @user_logger