Code Duplication    Length = 53-53 lines in 2 locations

myems-api/core/equipment.py 1 location

@@ 186-238 (lines=53) @@
183
    def on_options(req, resp, id_):
184
        resp.status = falcon.HTTP_200
185
186
    @staticmethod
187
    def on_get(req, resp, id_):
188
        if 'API-KEY' not in req.headers or \
189
                not isinstance(req.headers['API-KEY'], str) or \
190
                len(str.strip(req.headers['API-KEY'])) == 0:
191
            access_control(req)
192
        else:
193
            api_key_control(req)
194
        if not id_.isdigit() or int(id_) <= 0:
195
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
196
                                   description='API.INVALID_EQUIPMENT_ID')
197
198
        cnx = mysql.connector.connect(**config.myems_system_db)
199
        cursor = cnx.cursor()
200
201
        query = (" SELECT id, name, uuid "
202
                 " FROM tbl_cost_centers ")
203
        cursor.execute(query)
204
        rows_cost_centers = cursor.fetchall()
205
206
        cost_center_dict = dict()
207
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
208
            for row in rows_cost_centers:
209
                cost_center_dict[row[0]] = {"id": row[0],
210
                                            "name": row[1],
211
                                            "uuid": row[2]}
212
213
        query = (" SELECT id, name, uuid, "
214
                 "        is_input_counted, is_output_counted, "
215
                 "        cost_center_id, svg, camera_url, description "
216
                 " FROM tbl_equipments "
217
                 " WHERE id = %s ")
218
        cursor.execute(query, (id_,))
219
        row = cursor.fetchone()
220
        cursor.close()
221
        cnx.close()
222
223
        if row is None:
224
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
225
                                   description='API.EQUIPMENT_NOT_FOUND')
226
        else:
227
            meta_result = {"id": row[0],
228
                           "name": row[1],
229
                           "uuid": row[2],
230
                           "is_input_counted": bool(row[3]),
231
                           "is_output_counted": bool(row[4]),
232
                           "cost_center": cost_center_dict.get(row[5], None),
233
                           "svg": row[6],
234
                           "camera_url": row[7],
235
                           "description": row[8],
236
                           "qrcode": 'equipment:' + row[2]}
237
238
        resp.text = json.dumps(meta_result)
239
240
    @staticmethod
241
    @user_logger

myems-api/core/combinedequipment.py 1 location

@@ 186-238 (lines=53) @@
183
    def on_options(req, resp, id_):
184
        resp.status = falcon.HTTP_200
185
186
    @staticmethod
187
    def on_get(req, resp, id_):
188
        if 'API-KEY' not in req.headers or \
189
                not isinstance(req.headers['API-KEY'], str) or \
190
                len(str.strip(req.headers['API-KEY'])) == 0:
191
            access_control(req)
192
        else:
193
            api_key_control(req)
194
        if not id_.isdigit() or int(id_) <= 0:
195
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
196
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
197
198
        cnx = mysql.connector.connect(**config.myems_system_db)
199
        cursor = cnx.cursor()
200
201
        query = (" SELECT id, name, uuid "
202
                 " FROM tbl_cost_centers ")
203
        cursor.execute(query)
204
        rows_cost_centers = cursor.fetchall()
205
206
        cost_center_dict = dict()
207
        if rows_cost_centers is not None and len(rows_cost_centers) > 0:
208
            for row in rows_cost_centers:
209
                cost_center_dict[row[0]] = {"id": row[0],
210
                                            "name": row[1],
211
                                            "uuid": row[2]}
212
213
        query = (" SELECT id, name, uuid, "
214
                 "        is_input_counted, is_output_counted, "
215
                 "        cost_center_id, svg, camera_url, description "
216
                 " FROM tbl_combined_equipments "
217
                 " WHERE id = %s ")
218
        cursor.execute(query, (id_,))
219
        row = cursor.fetchone()
220
        cursor.close()
221
        cnx.close()
222
223
        if row is None:
224
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
225
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
226
        else:
227
            meta_result = {"id": row[0],
228
                           "name": row[1],
229
                           "uuid": row[2],
230
                           "is_input_counted": bool(row[3]),
231
                           "is_output_counted": bool(row[4]),
232
                           "cost_center": cost_center_dict.get(row[5], None),
233
                           "svg": row[6],
234
                           "camera_url": row[7],
235
                           "description": row[8],
236
                           "qrcode": 'combinedequipment:' + row[2]}
237
238
        resp.text = json.dumps(meta_result)
239
240
    @staticmethod
241
    @user_logger