Code Duplication    Length = 57-58 lines in 2 locations

myems-api/core/energystoragecontainer.py 2 locations

@@ 199-256 (lines=58) @@
196
        resp.status = falcon.HTTP_200
197
        _ = id_
198
199
    @staticmethod
200
    def on_get(req, resp, id_):
201
        access_control(req)
202
        if not id_.isdigit() or int(id_) <= 0:
203
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
204
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
205
206
        cnx = mysql.connector.connect(**config.myems_system_db)
207
        cursor = cnx.cursor()
208
209
        query = (" SELECT id, name, uuid "
210
                 " FROM tbl_contacts ")
211
        cursor.execute(query)
212
        rows_contacts = cursor.fetchall()
213
214
        contact_dict = dict()
215
        if rows_contacts is not None and len(rows_contacts) > 0:
216
            for row in rows_contacts:
217
                contact_dict[row[0]] = {"id": row[0],
218
                                        "name": row[1],
219
                                        "uuid": row[2]}
220
221
        query = (" SELECT id, name, uuid "
222
                 " FROM tbl_cost_centers ")
223
        cursor.execute(query)
224
        rows_cost_centers = cursor.fetchall()
225
226
        cost_center_dict = dict()
227
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
228
            for row in rows_cost_centers:
229
                cost_center_dict[row[0]] = {"id": row[0],
230
                                            "name": row[1],
231
                                            "uuid": row[2]}
232
233
        query = (" SELECT id, name, uuid, "
234
                 "        rated_capacity, rated_power, contact_id, cost_center_id, description "
235
                 " FROM tbl_energy_storage_containers "
236
                 " WHERE id = %s ")
237
        cursor.execute(query, (id_,))
238
        row = cursor.fetchone()
239
        cursor.close()
240
        cnx.close()
241
242
        if row is None:
243
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
244
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
245
        else:
246
            meta_result = {"id": row[0],
247
                           "name": row[1],
248
                           "uuid": row[2],
249
                           "rated_capacity": row[3],
250
                           "rated_power": row[4],
251
                           "contact": contact_dict.get(row[5], None),
252
                           "cost_center": cost_center_dict.get(row[6], None),
253
                           "description": row[7],
254
                           "qrcode": 'energystoragecontainer:' + row[2]}
255
256
        resp.text = json.dumps(meta_result)
257
258
    @staticmethod
259
    @user_logger
@@ 5919-5975 (lines=57) @@
5916
        resp.status = falcon.HTTP_200
5917
        _ = id_
5918
5919
    @staticmethod
5920
    def on_get(req, resp, id_):
5921
        access_control(req)
5922
        if not id_.isdigit() or int(id_) <= 0:
5923
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
5924
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
5925
5926
        cnx = mysql.connector.connect(**config.myems_system_db)
5927
        cursor = cnx.cursor()
5928
5929
        query = (" SELECT id, name, uuid "
5930
                 " FROM tbl_contacts ")
5931
        cursor.execute(query)
5932
        rows_contacts = cursor.fetchall()
5933
5934
        contact_dict = dict()
5935
        if rows_contacts is not None and len(rows_contacts) > 0:
5936
            for row in rows_contacts:
5937
                contact_dict[row[0]] = {"id": row[0],
5938
                                        "name": row[1],
5939
                                        "uuid": row[2]}
5940
5941
        query = (" SELECT id, name, uuid "
5942
                 " FROM tbl_cost_centers ")
5943
        cursor.execute(query)
5944
        rows_cost_centers = cursor.fetchall()
5945
5946
        cost_center_dict = dict()
5947
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
5948
            for row in rows_cost_centers:
5949
                cost_center_dict[row[0]] = {"id": row[0],
5950
                                            "name": row[1],
5951
                                            "uuid": row[2]}
5952
5953
        query = (" SELECT id, name, uuid, "
5954
                 "        rated_capacity, rated_power, contact_id, cost_center_id, description "
5955
                 " FROM tbl_energy_storage_containers "
5956
                 " WHERE id = %s ")
5957
        cursor.execute(query, (id_,))
5958
        row = cursor.fetchone()
5959
        cursor.close()
5960
        cnx.close()
5961
5962
        if row is None:
5963
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
5964
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
5965
        else:
5966
            meta_result = {"id": row[0],
5967
                           "name": row[1],
5968
                           "uuid": row[2],
5969
                           "rated_capacity": row[3],
5970
                           "rated_power": row[4],
5971
                           "contact": contact_dict.get(row[5], None),
5972
                           "cost_center": cost_center_dict.get(row[6], None),
5973
                           "description": row[7]}
5974
5975
        resp.text = json.dumps(meta_result)
5976
5977
5978
class EnergyStorageContainerImport: