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