Code Duplication    Length = 63-64 lines in 3 locations

myems-api/core/equipment.py 1 location

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

myems-api/core/shopfloor.py 1 location

@@ 207-269 (lines=63) @@
204
        _ = req
205
        _ = id_
206
207
    @staticmethod
208
    def on_get(req, resp, id_):
209
        if 'API-KEY' not in req.headers or \
210
                not isinstance(req.headers['API-KEY'], str) or \
211
                len(str.strip(req.headers['API-KEY'])) == 0:
212
            access_control(req)
213
        else:
214
            api_key_control(req)
215
        if not id_.isdigit() or int(id_) <= 0:
216
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
217
                                   description='API.INVALID_SHOPFLOOR_ID')
218
219
        cnx = mysql.connector.connect(**config.myems_system_db)
220
        cursor = cnx.cursor()
221
222
        query = (" SELECT id, name, uuid "
223
                 " FROM tbl_contacts ")
224
        cursor.execute(query)
225
        rows_contacts = cursor.fetchall()
226
227
        contact_dict = dict()
228
        if rows_contacts is not None and len(rows_contacts) > 0:
229
            for row in rows_contacts:
230
                contact_dict[row[0]] = {"id": row[0],
231
                                        "name": row[1],
232
                                        "uuid": row[2]}
233
234
        query = (" SELECT id, name, uuid "
235
                 " FROM tbl_cost_centers ")
236
        cursor.execute(query)
237
        rows_cost_centers = cursor.fetchall()
238
239
        cost_center_dict = dict()
240
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
241
            for row in rows_cost_centers:
242
                cost_center_dict[row[0]] = {"id": row[0],
243
                                            "name": row[1],
244
                                            "uuid": row[2]}
245
246
        query = (" SELECT id, name, uuid, "
247
                 "        area, is_input_counted, contact_id, cost_center_id, description "
248
                 " FROM tbl_shopfloors "
249
                 " WHERE id = %s ")
250
        cursor.execute(query, (id_,))
251
        row = cursor.fetchone()
252
        cursor.close()
253
        cnx.close()
254
255
        if row is None:
256
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
257
                                   description='API.SHOPFLOOR_NOT_FOUND')
258
        else:
259
            meta_result = {"id": row[0],
260
                           "name": row[1],
261
                           "uuid": row[2],
262
                           "area": row[3],
263
                           "is_input_counted": bool(row[4]),
264
                           "contact": contact_dict.get(row[5], None),
265
                           "cost_center": cost_center_dict.get(row[6], None),
266
                           "description": row[7],
267
                           "qrcode": "shopfloor:" + row[2]}
268
269
        resp.text = json.dumps(meta_result)
270
271
    @staticmethod
272
    @user_logger

myems-api/core/combinedequipment.py 1 location

@@ 211-274 (lines=64) @@
208
        resp.status = falcon.HTTP_200
209
        _ = id_
210
211
    @staticmethod
212
    def on_get(req, resp, id_):
213
        if 'API-KEY' not in req.headers or \
214
                not isinstance(req.headers['API-KEY'], str) or \
215
                len(str.strip(req.headers['API-KEY'])) == 0:
216
            access_control(req)
217
        else:
218
            api_key_control(req)
219
        if not id_.isdigit() or int(id_) <= 0:
220
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
221
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
222
223
        cnx = mysql.connector.connect(**config.myems_system_db)
224
        cursor = cnx.cursor()
225
226
        query = (" SELECT id, name, uuid "
227
                 " FROM tbl_cost_centers ")
228
        cursor.execute(query)
229
        rows_cost_centers = cursor.fetchall()
230
231
        cost_center_dict = dict()
232
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
233
            for row in rows_cost_centers:
234
                cost_center_dict[row[0]] = {"id": row[0],
235
                                            "name": row[1],
236
                                            "uuid": row[2]}
237
238
        svg_dict = dict()
239
        query = (" SELECT id, name, uuid "
240
                 " FROM tbl_svgs ")
241
        cursor.execute(query)
242
        rows_svgs = cursor.fetchall()
243
        if rows_svgs is not None and len(rows_svgs) > 0:
244
            for row in rows_svgs:
245
                svg_dict[row[0]] = {"id": row[0],
246
                                    "name": row[1],
247
                                    "uuid": row[2]}
248
249
        query = (" SELECT id, name, uuid, "
250
                 "        is_input_counted, is_output_counted, "
251
                 "        cost_center_id, svg_id, camera_url, description "
252
                 " FROM tbl_combined_equipments "
253
                 " WHERE id = %s ")
254
        cursor.execute(query, (id_,))
255
        row = cursor.fetchone()
256
        cursor.close()
257
        cnx.close()
258
259
        if row is None:
260
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
261
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
262
        else:
263
            meta_result = {"id": row[0],
264
                           "name": row[1],
265
                           "uuid": row[2],
266
                           "is_input_counted": bool(row[3]),
267
                           "is_output_counted": bool(row[4]),
268
                           "cost_center": cost_center_dict.get(row[5], None),
269
                           "svg": svg_dict.get(row[6], None),
270
                           "camera_url": row[7],
271
                           "description": row[8],
272
                           "qrcode": 'combinedequipment:' + row[2]}
273
274
        resp.text = json.dumps(meta_result)
275
276
    @staticmethod
277
    @user_logger