@@ 286-379 (lines=94) @@ | ||
283 | ||
284 | resp.status = falcon.HTTP_204 |
|
285 | ||
286 | @staticmethod |
|
287 | def on_put(req, resp, id_): |
|
288 | """Handles PUT requests""" |
|
289 | if not id_.isdigit() or int(id_) <= 0: |
|
290 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
291 | description='API.INVALID_EQUIPMENT_ID') |
|
292 | try: |
|
293 | raw_json = req.stream.read().decode('utf-8') |
|
294 | except Exception as ex: |
|
295 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
296 | ||
297 | new_values = json.loads(raw_json, encoding='utf-8') |
|
298 | ||
299 | if 'name' not in new_values['data'].keys() or \ |
|
300 | not isinstance(new_values['data']['name'], str) or \ |
|
301 | len(str.strip(new_values['data']['name'])) == 0: |
|
302 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
303 | description='API.INVALID_EQUIPMENT_NAME') |
|
304 | name = str.strip(new_values['data']['name']) |
|
305 | ||
306 | if 'is_input_counted' not in new_values['data'].keys() or \ |
|
307 | not isinstance(new_values['data']['is_input_counted'], bool): |
|
308 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
309 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
|
310 | is_input_counted = new_values['data']['is_input_counted'] |
|
311 | ||
312 | if 'is_output_counted' not in new_values['data'].keys() or \ |
|
313 | not isinstance(new_values['data']['is_output_counted'], bool): |
|
314 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
315 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
|
316 | is_output_counted = new_values['data']['is_output_counted'] |
|
317 | ||
318 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
319 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
320 | new_values['data']['cost_center_id'] <= 0: |
|
321 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
322 | description='API.INVALID_COST_CENTER_ID') |
|
323 | cost_center_id = new_values['data']['cost_center_id'] |
|
324 | ||
325 | if 'description' in new_values['data'].keys() and \ |
|
326 | new_values['data']['description'] is not None and \ |
|
327 | len(str(new_values['data']['description'])) > 0: |
|
328 | description = str.strip(new_values['data']['description']) |
|
329 | else: |
|
330 | description = None |
|
331 | ||
332 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
333 | cursor = cnx.cursor() |
|
334 | ||
335 | cursor.execute(" SELECT name " |
|
336 | " FROM tbl_equipments " |
|
337 | " WHERE id = %s ", (id_,)) |
|
338 | if cursor.fetchone() is None: |
|
339 | cursor.close() |
|
340 | cnx.disconnect() |
|
341 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
342 | description='API.EQUIPMENT_NOT_FOUND') |
|
343 | ||
344 | cursor.execute(" SELECT name " |
|
345 | " FROM tbl_equipments " |
|
346 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
347 | if cursor.fetchone() is not None: |
|
348 | cursor.close() |
|
349 | cnx.disconnect() |
|
350 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
|
351 | description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
|
352 | ||
353 | cursor.execute(" SELECT name " |
|
354 | " FROM tbl_cost_centers " |
|
355 | " WHERE id = %s ", |
|
356 | (new_values['data']['cost_center_id'],)) |
|
357 | row = cursor.fetchone() |
|
358 | if row is None: |
|
359 | cursor.close() |
|
360 | cnx.disconnect() |
|
361 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
362 | description='API.COST_CENTER_NOT_FOUND') |
|
363 | ||
364 | update_row = (" UPDATE tbl_equipments " |
|
365 | " SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
|
366 | " cost_center_id = %s, description = %s " |
|
367 | " WHERE id = %s ") |
|
368 | cursor.execute(update_row, (name, |
|
369 | is_input_counted, |
|
370 | is_output_counted, |
|
371 | cost_center_id, |
|
372 | description, |
|
373 | id_)) |
|
374 | cnx.commit() |
|
375 | ||
376 | cursor.close() |
|
377 | cnx.disconnect() |
|
378 | ||
379 | resp.status = falcon.HTTP_200 |
|
380 | ||
381 | # Clone an Equipment |
|
382 | @staticmethod |
@@ 273-366 (lines=94) @@ | ||
270 | ||
271 | resp.status = falcon.HTTP_204 |
|
272 | ||
273 | @staticmethod |
|
274 | def on_put(req, resp, id_): |
|
275 | """Handles PUT requests""" |
|
276 | if not id_.isdigit() or int(id_) <= 0: |
|
277 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
278 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
279 | try: |
|
280 | raw_json = req.stream.read().decode('utf-8') |
|
281 | except Exception as ex: |
|
282 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
|
283 | ||
284 | new_values = json.loads(raw_json, encoding='utf-8') |
|
285 | ||
286 | if 'name' not in new_values['data'].keys() or \ |
|
287 | not isinstance(new_values['data']['name'], str) or \ |
|
288 | len(str.strip(new_values['data']['name'])) == 0: |
|
289 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
290 | description='API.INVALID_COMBINED_EQUIPMENT_NAME') |
|
291 | name = str.strip(new_values['data']['name']) |
|
292 | ||
293 | if 'is_input_counted' not in new_values['data'].keys() or \ |
|
294 | not isinstance(new_values['data']['is_input_counted'], bool): |
|
295 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
296 | description='API.INVALID_IS_INPUT_COUNTED_VALUE') |
|
297 | is_input_counted = new_values['data']['is_input_counted'] |
|
298 | ||
299 | if 'is_output_counted' not in new_values['data'].keys() or \ |
|
300 | not isinstance(new_values['data']['is_output_counted'], bool): |
|
301 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
302 | description='API.INVALID_IS_OUTPUT_COUNTED_VALUE') |
|
303 | is_output_counted = new_values['data']['is_output_counted'] |
|
304 | ||
305 | if 'cost_center_id' not in new_values['data'].keys() or \ |
|
306 | not isinstance(new_values['data']['cost_center_id'], int) or \ |
|
307 | new_values['data']['cost_center_id'] <= 0: |
|
308 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
309 | description='API.INVALID_COST_CENTER_ID') |
|
310 | cost_center_id = new_values['data']['cost_center_id'] |
|
311 | ||
312 | if 'description' in new_values['data'].keys() and \ |
|
313 | new_values['data']['description'] is not None and \ |
|
314 | len(str(new_values['data']['description'])) > 0: |
|
315 | description = str.strip(new_values['data']['description']) |
|
316 | else: |
|
317 | description = None |
|
318 | ||
319 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
320 | cursor = cnx.cursor() |
|
321 | ||
322 | cursor.execute(" SELECT name " |
|
323 | " FROM tbl_combined_equipments " |
|
324 | " WHERE id = %s ", (id_,)) |
|
325 | if cursor.fetchone() is None: |
|
326 | cursor.close() |
|
327 | cnx.disconnect() |
|
328 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
329 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
330 | ||
331 | cursor.execute(" SELECT name " |
|
332 | " FROM tbl_combined_equipments " |
|
333 | " WHERE name = %s AND id != %s ", (name, id_)) |
|
334 | if cursor.fetchone() is not None: |
|
335 | cursor.close() |
|
336 | cnx.disconnect() |
|
337 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
|
338 | description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE') |
|
339 | ||
340 | cursor.execute(" SELECT name " |
|
341 | " FROM tbl_cost_centers " |
|
342 | " WHERE id = %s ", |
|
343 | (new_values['data']['cost_center_id'],)) |
|
344 | row = cursor.fetchone() |
|
345 | if row is None: |
|
346 | cursor.close() |
|
347 | cnx.disconnect() |
|
348 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
349 | description='API.COST_CENTER_NOT_FOUND') |
|
350 | ||
351 | update_row = (" UPDATE tbl_combined_equipments " |
|
352 | " SET name = %s, is_input_counted = %s, is_output_counted = %s, " |
|
353 | " cost_center_id = %s, description = %s " |
|
354 | " WHERE id = %s ") |
|
355 | cursor.execute(update_row, (name, |
|
356 | is_input_counted, |
|
357 | is_output_counted, |
|
358 | cost_center_id, |
|
359 | description, |
|
360 | id_)) |
|
361 | cnx.commit() |
|
362 | ||
363 | cursor.close() |
|
364 | cnx.disconnect() |
|
365 | ||
366 | resp.status = falcon.HTTP_200 |
|
367 | ||
368 | # Clone a Combined Equipment |
|
369 | @staticmethod |