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
@@ 4234-4290 (lines=57) @@
4231
    def on_options(req, resp, id_):
4232
        resp.status = falcon.HTTP_200
4233
4234
    @staticmethod
4235
    def on_get(req, resp, id_):
4236
        access_control(req)
4237
        if not id_.isdigit() or int(id_) <= 0:
4238
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
4239
                                   description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')
4240
4241
        cnx = mysql.connector.connect(**config.myems_system_db)
4242
        cursor = cnx.cursor()
4243
4244
        query = (" SELECT id, name, uuid "
4245
                 " FROM tbl_contacts ")
4246
        cursor.execute(query)
4247
        rows_contacts = cursor.fetchall()
4248
4249
        contact_dict = dict()
4250
        if rows_contacts is not None and len(rows_contacts) > 0:
4251
            for row in rows_contacts:
4252
                contact_dict[row[0]] = {"id": row[0],
4253
                                        "name": row[1],
4254
                                        "uuid": row[2]}
4255
4256
        query = (" SELECT id, name, uuid "
4257
                 " FROM tbl_cost_centers ")
4258
        cursor.execute(query)
4259
        rows_cost_centers = cursor.fetchall()
4260
4261
        cost_center_dict = dict()
4262
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
4263
            for row in rows_cost_centers:
4264
                cost_center_dict[row[0]] = {"id": row[0],
4265
                                            "name": row[1],
4266
                                            "uuid": row[2]}
4267
4268
        query = (" SELECT id, name, uuid, "
4269
                 "        rated_capacity, rated_power, contact_id, cost_center_id, description "
4270
                 " FROM tbl_energy_storage_containers "
4271
                 " WHERE id = %s ")
4272
        cursor.execute(query, (id_,))
4273
        row = cursor.fetchone()
4274
        cursor.close()
4275
        cnx.close()
4276
4277
        if row is None:
4278
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
4279
                                   description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
4280
        else:
4281
            meta_result = {"id": row[0],
4282
                           "name": row[1],
4283
                           "uuid": row[2],
4284
                           "rated_capacity": row[3],
4285
                           "rated_power": row[4],
4286
                           "contact": contact_dict.get(row[5], None),
4287
                           "cost_center": cost_center_dict.get(row[6], None),
4288
                           "description": row[7]}
4289
4290
        resp.text = json.dumps(meta_result)
4291
4292
4293
class EnergyStorageContainerImport: