Code Duplication    Length = 63-64 lines in 3 locations

myems-api/core/shopfloor.py 1 location

@@ 247-309 (lines=63) @@
244
        _ = req
245
        _ = id_
246
247
    @staticmethod
248
    def on_get(req, resp, id_):
249
        if 'API-KEY' not in req.headers or \
250
                not isinstance(req.headers['API-KEY'], str) or \
251
                len(str.strip(req.headers['API-KEY'])) == 0:
252
            access_control(req)
253
        else:
254
            api_key_control(req)
255
        if not id_.isdigit() or int(id_) <= 0:
256
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
257
                                   description='API.INVALID_SHOPFLOOR_ID')
258
259
        cnx = mysql.connector.connect(**config.myems_system_db)
260
        cursor = cnx.cursor()
261
262
        query = (" SELECT id, name, uuid "
263
                 " FROM tbl_contacts ")
264
        cursor.execute(query)
265
        rows_contacts = cursor.fetchall()
266
267
        contact_dict = dict()
268
        if rows_contacts is not None and len(rows_contacts) > 0:
269
            for row in rows_contacts:
270
                contact_dict[row[0]] = {"id": row[0],
271
                                        "name": row[1],
272
                                        "uuid": row[2]}
273
274
        query = (" SELECT id, name, uuid "
275
                 " FROM tbl_cost_centers ")
276
        cursor.execute(query)
277
        rows_cost_centers = cursor.fetchall()
278
279
        cost_center_dict = dict()
280
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
281
            for row in rows_cost_centers:
282
                cost_center_dict[row[0]] = {"id": row[0],
283
                                            "name": row[1],
284
                                            "uuid": row[2]}
285
286
        query = (" SELECT id, name, uuid, "
287
                 "        area, is_input_counted, contact_id, cost_center_id, description "
288
                 " FROM tbl_shopfloors "
289
                 " WHERE id = %s ")
290
        cursor.execute(query, (id_,))
291
        row = cursor.fetchone()
292
        cursor.close()
293
        cnx.close()
294
295
        if row is None:
296
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
297
                                   description='API.SHOPFLOOR_NOT_FOUND')
298
        else:
299
            meta_result = {"id": row[0],
300
                           "name": row[1],
301
                           "uuid": row[2],
302
                           "area": row[3],
303
                           "is_input_counted": bool(row[4]),
304
                           "contact": contact_dict.get(row[5], None),
305
                           "cost_center": cost_center_dict.get(row[6], None),
306
                           "description": row[7],
307
                           "qrcode": "shopfloor:" + row[2]}
308
309
        resp.text = json.dumps(meta_result)
310
311
    @staticmethod
312
    @user_logger

myems-api/core/combinedequipment.py 1 location

@@ 260-323 (lines=64) @@
257
        resp.status = falcon.HTTP_200
258
        _ = id_
259
260
    @staticmethod
261
    def on_get(req, resp, id_):
262
        if 'API-KEY' not in req.headers or \
263
                not isinstance(req.headers['API-KEY'], str) or \
264
                len(str.strip(req.headers['API-KEY'])) == 0:
265
            access_control(req)
266
        else:
267
            api_key_control(req)
268
        if not id_.isdigit() or int(id_) <= 0:
269
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
270
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
271
272
        cnx = mysql.connector.connect(**config.myems_system_db)
273
        cursor = cnx.cursor()
274
275
        query = (" SELECT id, name, uuid "
276
                 " FROM tbl_cost_centers ")
277
        cursor.execute(query)
278
        rows_cost_centers = cursor.fetchall()
279
280
        cost_center_dict = dict()
281
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
282
            for row in rows_cost_centers:
283
                cost_center_dict[row[0]] = {"id": row[0],
284
                                            "name": row[1],
285
                                            "uuid": row[2]}
286
287
        svg_dict = dict()
288
        query = (" SELECT id, name, uuid "
289
                 " FROM tbl_svgs ")
290
        cursor.execute(query)
291
        rows_svgs = cursor.fetchall()
292
        if rows_svgs is not None and len(rows_svgs) > 0:
293
            for row in rows_svgs:
294
                svg_dict[row[0]] = {"id": row[0],
295
                                    "name": row[1],
296
                                    "uuid": row[2]}
297
298
        query = (" SELECT id, name, uuid, "
299
                 "        is_input_counted, is_output_counted, "
300
                 "        cost_center_id, svg_id, camera_url, description "
301
                 " FROM tbl_combined_equipments "
302
                 " WHERE id = %s ")
303
        cursor.execute(query, (id_,))
304
        row = cursor.fetchone()
305
        cursor.close()
306
        cnx.close()
307
308
        if row is None:
309
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
310
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
311
        else:
312
            meta_result = {"id": row[0],
313
                           "name": row[1],
314
                           "uuid": row[2],
315
                           "is_input_counted": bool(row[3]),
316
                           "is_output_counted": bool(row[4]),
317
                           "cost_center": cost_center_dict.get(row[5], None),
318
                           "svg": svg_dict.get(row[6], None),
319
                           "camera_url": row[7],
320
                           "description": row[8],
321
                           "qrcode": 'combinedequipment:' + row[2]}
322
323
        resp.text = json.dumps(meta_result)
324
325
    @staticmethod
326
    @user_logger

myems-api/core/equipment.py 1 location

@@ 228-291 (lines=64) @@
225
        resp.status = falcon.HTTP_200
226
        _ = id_
227
228
    @staticmethod
229
    def on_get(req, resp, id_):
230
        if 'API-KEY' not in req.headers or \
231
                not isinstance(req.headers['API-KEY'], str) or \
232
                len(str.strip(req.headers['API-KEY'])) == 0:
233
            access_control(req)
234
        else:
235
            api_key_control(req)
236
        if not id_.isdigit() or int(id_) <= 0:
237
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
238
                                   description='API.INVALID_EQUIPMENT_ID')
239
240
        cnx = mysql.connector.connect(**config.myems_system_db)
241
        cursor = cnx.cursor()
242
243
        query = (" SELECT id, name, uuid "
244
                 " FROM tbl_cost_centers ")
245
        cursor.execute(query)
246
        rows_cost_centers = cursor.fetchall()
247
248
        cost_center_dict = dict()
249
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
250
            for row in rows_cost_centers:
251
                cost_center_dict[row[0]] = {"id": row[0],
252
                                            "name": row[1],
253
                                            "uuid": row[2]}
254
255
        svg_dict = dict()
256
        query = (" SELECT id, name, uuid "
257
                 " FROM tbl_svgs ")
258
        cursor.execute(query)
259
        rows_svgs = cursor.fetchall()
260
        if rows_svgs is not None and len(rows_svgs) > 0:
261
            for row in rows_svgs:
262
                svg_dict[row[0]] = {"id": row[0],
263
                                    "name": row[1],
264
                                    "uuid": row[2]}
265
266
        query = (" SELECT id, name, uuid, "
267
                 "        is_input_counted, is_output_counted, "
268
                 "        cost_center_id, svg_id, camera_url, description "
269
                 " FROM tbl_equipments "
270
                 " WHERE id = %s ")
271
        cursor.execute(query, (id_,))
272
        row = cursor.fetchone()
273
        cursor.close()
274
        cnx.close()
275
276
        if row is None:
277
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
278
                                   description='API.EQUIPMENT_NOT_FOUND')
279
        else:
280
            meta_result = {"id": row[0],
281
                           "name": row[1],
282
                           "uuid": row[2],
283
                           "is_input_counted": bool(row[3]),
284
                           "is_output_counted": bool(row[4]),
285
                           "cost_center": cost_center_dict.get(row[5], None),
286
                           "svg": svg_dict.get(row[6], None),
287
                           "camera_url": row[7],
288
                           "description": row[8],
289
                           "qrcode": 'equipment:' + row[2]}
290
291
        resp.text = json.dumps(meta_result)
292
293
    @staticmethod
294
    @user_logger