Code Duplication    Length = 127-127 lines in 2 locations

myems-api/core/combinedequipment.py 1 location

@@ 376-502 (lines=127) @@
373
374
        resp.status = falcon.HTTP_204
375
376
    @staticmethod
377
    @user_logger
378
    def on_put(req, resp, id_):
379
        """Handles PUT requests"""
380
        admin_control(req)
381
        if not id_.isdigit() or int(id_) <= 0:
382
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
383
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
384
        try:
385
            raw_json = req.stream.read().decode('utf-8')
386
        except Exception as ex:
387
            print(ex)
388
            raise falcon.HTTPError(status=falcon.HTTP_400,
389
                                   title='API.BAD_REQUEST',
390
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
391
392
        new_values = json.loads(raw_json)
393
394
        if 'name' not in new_values['data'].keys() or \
395
                not isinstance(new_values['data']['name'], str) or \
396
                len(str.strip(new_values['data']['name'])) == 0:
397
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
398
                                   description='API.INVALID_COMBINED_EQUIPMENT_NAME')
399
        name = str.strip(new_values['data']['name'])
400
401
        if 'is_input_counted' not in new_values['data'].keys() or \
402
                not isinstance(new_values['data']['is_input_counted'], bool):
403
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
404
                                   description='API.INVALID_IS_INPUT_COUNTED_VALUE')
405
        is_input_counted = new_values['data']['is_input_counted']
406
407
        if 'is_output_counted' not in new_values['data'].keys() or \
408
                not isinstance(new_values['data']['is_output_counted'], bool):
409
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
410
                                   description='API.INVALID_IS_OUTPUT_COUNTED_VALUE')
411
        is_output_counted = new_values['data']['is_output_counted']
412
413
        if 'cost_center_id' not in new_values['data'].keys() or \
414
                not isinstance(new_values['data']['cost_center_id'], int) or \
415
                new_values['data']['cost_center_id'] <= 0:
416
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
417
                                   description='API.INVALID_COST_CENTER_ID')
418
        cost_center_id = new_values['data']['cost_center_id']
419
420
        if 'svg_id' in new_values['data'].keys() and \
421
                isinstance(new_values['data']['svg_id'], int) and \
422
                new_values['data']['svg_id'] > 0:
423
            svg_id = new_values['data']['svg_id']
424
        else:
425
            svg_id = None
426
427
        if 'camera_url' in new_values['data'].keys() and \
428
                new_values['data']['camera_url'] is not None and \
429
                len(str(new_values['data']['camera_url'])) > 0:
430
            camera_url = str.strip(new_values['data']['camera_url'])
431
        else:
432
            camera_url = None
433
434
        if 'description' in new_values['data'].keys() and \
435
                new_values['data']['description'] is not None and \
436
                len(str(new_values['data']['description'])) > 0:
437
            description = str.strip(new_values['data']['description'])
438
        else:
439
            description = None
440
441
        cnx = mysql.connector.connect(**config.myems_system_db)
442
        cursor = cnx.cursor()
443
444
        cursor.execute(" SELECT name "
445
                       " FROM tbl_combined_equipments "
446
                       " WHERE id = %s ", (id_,))
447
        if cursor.fetchone() is None:
448
            cursor.close()
449
            cnx.close()
450
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
451
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
452
453
        cursor.execute(" SELECT name "
454
                       " FROM tbl_combined_equipments "
455
                       " WHERE name = %s AND id != %s ", (name, id_))
456
        if cursor.fetchone() is not None:
457
            cursor.close()
458
            cnx.close()
459
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
460
                                   description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE')
461
462
        cursor.execute(" SELECT name "
463
                       " FROM tbl_cost_centers "
464
                       " WHERE id = %s ",
465
                       (new_values['data']['cost_center_id'],))
466
        row = cursor.fetchone()
467
        if row is None:
468
            cursor.close()
469
            cnx.close()
470
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
471
                                   description='API.COST_CENTER_NOT_FOUND')
472
473
        if svg_id is not None:
474
            cursor.execute(" SELECT name "
475
                           " FROM tbl_svgs "
476
                           " WHERE id = %s ",
477
                           (new_values['data']['svg_id'],))
478
            row = cursor.fetchone()
479
            if row is None:
480
                cursor.close()
481
                cnx.close()
482
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
483
                                       description='API.SVG_NOT_FOUND')
484
485
        update_row = (" UPDATE tbl_combined_equipments "
486
                      " SET name = %s, is_input_counted = %s, is_output_counted = %s, "
487
                      "     cost_center_id = %s, svg_id = %s, camera_url = %s, description = %s "
488
                      " WHERE id = %s ")
489
        cursor.execute(update_row, (name,
490
                                    is_input_counted,
491
                                    is_output_counted,
492
                                    cost_center_id,
493
                                    svg_id,
494
                                    camera_url,
495
                                    description,
496
                                    id_))
497
        cnx.commit()
498
499
        cursor.close()
500
        cnx.close()
501
502
        resp.status = falcon.HTTP_200
503
504
    # Clone a Combined Equipment
505
    @staticmethod

myems-api/core/equipment.py 1 location

@@ 368-494 (lines=127) @@
365
366
        resp.status = falcon.HTTP_204
367
368
    @staticmethod
369
    @user_logger
370
    def on_put(req, resp, id_):
371
        """Handles PUT requests"""
372
        admin_control(req)
373
        if not id_.isdigit() or int(id_) <= 0:
374
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
375
                                   description='API.INVALID_EQUIPMENT_ID')
376
        try:
377
            raw_json = req.stream.read().decode('utf-8')
378
        except Exception as ex:
379
            print(str(ex))
380
            raise falcon.HTTPError(status=falcon.HTTP_400,
381
                                   title='API.BAD_REQUEST',
382
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
383
384
        new_values = json.loads(raw_json)
385
386
        if 'name' not in new_values['data'].keys() or \
387
                not isinstance(new_values['data']['name'], str) or \
388
                len(str.strip(new_values['data']['name'])) == 0:
389
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
390
                                   description='API.INVALID_EQUIPMENT_NAME')
391
        name = str.strip(new_values['data']['name'])
392
393
        if 'is_input_counted' not in new_values['data'].keys() or \
394
                not isinstance(new_values['data']['is_input_counted'], bool):
395
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
396
                                   description='API.INVALID_IS_INPUT_COUNTED_VALUE')
397
        is_input_counted = new_values['data']['is_input_counted']
398
399
        if 'is_output_counted' not in new_values['data'].keys() or \
400
                not isinstance(new_values['data']['is_output_counted'], bool):
401
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
402
                                   description='API.INVALID_IS_OUTPUT_COUNTED_VALUE')
403
        is_output_counted = new_values['data']['is_output_counted']
404
405
        if 'cost_center_id' not in new_values['data'].keys() or \
406
                not isinstance(new_values['data']['cost_center_id'], int) or \
407
                new_values['data']['cost_center_id'] <= 0:
408
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
409
                                   description='API.INVALID_COST_CENTER_ID')
410
        cost_center_id = new_values['data']['cost_center_id']
411
412
        if 'svg_id' in new_values['data'].keys() and \
413
                isinstance(new_values['data']['svg_id'], int) and \
414
                new_values['data']['svg_id'] > 0:
415
            svg_id = new_values['data']['svg_id']
416
        else:
417
            svg_id = None
418
419
        if 'description' in new_values['data'].keys() and \
420
                new_values['data']['description'] is not None and \
421
                len(str(new_values['data']['description'])) > 0:
422
            description = str.strip(new_values['data']['description'])
423
        else:
424
            description = None
425
426
        if 'camera_url' in new_values['data'].keys() and \
427
                new_values['data']['camera_url'] is not None and \
428
                len(str(new_values['data']['camera_url'])) > 0:
429
            camera_url = str.strip(new_values['data']['camera_url'])
430
        else:
431
            camera_url = None
432
433
        cnx = mysql.connector.connect(**config.myems_system_db)
434
        cursor = cnx.cursor()
435
436
        cursor.execute(" SELECT name "
437
                       " FROM tbl_equipments "
438
                       " WHERE id = %s ", (id_,))
439
        if cursor.fetchone() is None:
440
            cursor.close()
441
            cnx.close()
442
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
443
                                   description='API.EQUIPMENT_NOT_FOUND')
444
445
        cursor.execute(" SELECT name "
446
                       " FROM tbl_equipments "
447
                       " WHERE name = %s AND id != %s ", (name, id_))
448
        if cursor.fetchone() is not None:
449
            cursor.close()
450
            cnx.close()
451
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
452
                                   description='API.EQUIPMENT_NAME_IS_ALREADY_IN_USE')
453
454
        cursor.execute(" SELECT name "
455
                       " FROM tbl_cost_centers "
456
                       " WHERE id = %s ",
457
                       (new_values['data']['cost_center_id'],))
458
        row = cursor.fetchone()
459
        if row is None:
460
            cursor.close()
461
            cnx.close()
462
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
463
                                   description='API.COST_CENTER_NOT_FOUND')
464
465
        if svg_id is not None:
466
            cursor.execute(" SELECT name "
467
                           " FROM tbl_svgs "
468
                           " WHERE id = %s ",
469
                           (new_values['data']['svg_id'],))
470
            row = cursor.fetchone()
471
            if row is None:
472
                cursor.close()
473
                cnx.close()
474
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
475
                                       description='API.SVG_NOT_FOUND')
476
477
        update_row = (" UPDATE tbl_equipments "
478
                      " SET name = %s, is_input_counted = %s, is_output_counted = %s, "
479
                      "     cost_center_id = %s, svg_id = %s, camera_url = %s, description = %s "
480
                      " WHERE id = %s ")
481
        cursor.execute(update_row, (name,
482
                                    is_input_counted,
483
                                    is_output_counted,
484
                                    cost_center_id,
485
                                    svg_id,
486
                                    camera_url,
487
                                    description,
488
                                    id_))
489
        cnx.commit()
490
491
        cursor.close()
492
        cnx.close()
493
494
        resp.status = falcon.HTTP_200
495
496
    # Clone an Equipment
497
    @staticmethod