Code Duplication    Length = 126-127 lines in 2 locations

myems-api/core/equipment.py 1 location

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

myems-api/core/combinedequipment.py 1 location

@@ 326-452 (lines=127) @@
323
324
        resp.status = falcon.HTTP_204
325
326
    @staticmethod
327
    @user_logger
328
    def on_put(req, resp, id_):
329
        """Handles PUT requests"""
330
        admin_control(req)
331
        if not id_.isdigit() or int(id_) <= 0:
332
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
333
                                   description='API.INVALID_COMBINED_EQUIPMENT_ID')
334
        try:
335
            raw_json = req.stream.read().decode('utf-8')
336
        except Exception as ex:
337
            print(ex)
338
            raise falcon.HTTPError(status=falcon.HTTP_400,
339
                                   title='API.BAD_REQUEST',
340
                                   description='API.FAILED_TO_READ_REQUEST_STREAM')
341
342
        new_values = json.loads(raw_json)
343
344
        if 'name' not in new_values['data'].keys() or \
345
                not isinstance(new_values['data']['name'], str) or \
346
                len(str.strip(new_values['data']['name'])) == 0:
347
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
348
                                   description='API.INVALID_COMBINED_EQUIPMENT_NAME')
349
        name = str.strip(new_values['data']['name'])
350
351
        if 'is_input_counted' not in new_values['data'].keys() or \
352
                not isinstance(new_values['data']['is_input_counted'], bool):
353
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
354
                                   description='API.INVALID_IS_INPUT_COUNTED_VALUE')
355
        is_input_counted = new_values['data']['is_input_counted']
356
357
        if 'is_output_counted' not in new_values['data'].keys() or \
358
                not isinstance(new_values['data']['is_output_counted'], bool):
359
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
360
                                   description='API.INVALID_IS_OUTPUT_COUNTED_VALUE')
361
        is_output_counted = new_values['data']['is_output_counted']
362
363
        if 'cost_center_id' not in new_values['data'].keys() or \
364
                not isinstance(new_values['data']['cost_center_id'], int) or \
365
                new_values['data']['cost_center_id'] <= 0:
366
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
367
                                   description='API.INVALID_COST_CENTER_ID')
368
        cost_center_id = new_values['data']['cost_center_id']
369
370
        if 'svg_id' in new_values['data'].keys() and \
371
                isinstance(new_values['data']['svg_id'], int) and \
372
                new_values['data']['svg_id'] > 0:
373
            svg_id = new_values['data']['svg_id']
374
        else:
375
            svg_id = None
376
377
        if 'camera_url' in new_values['data'].keys() and \
378
                new_values['data']['camera_url'] is not None and \
379
                len(str(new_values['data']['camera_url'])) > 0:
380
            camera_url = str.strip(new_values['data']['camera_url'])
381
        else:
382
            camera_url = None
383
384
        if 'description' in new_values['data'].keys() and \
385
                new_values['data']['description'] is not None and \
386
                len(str(new_values['data']['description'])) > 0:
387
            description = str.strip(new_values['data']['description'])
388
        else:
389
            description = None
390
391
        cnx = mysql.connector.connect(**config.myems_system_db)
392
        cursor = cnx.cursor()
393
394
        cursor.execute(" SELECT name "
395
                       " FROM tbl_combined_equipments "
396
                       " WHERE id = %s ", (id_,))
397
        if cursor.fetchone() is None:
398
            cursor.close()
399
            cnx.close()
400
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
401
                                   description='API.COMBINED_EQUIPMENT_NOT_FOUND')
402
403
        cursor.execute(" SELECT name "
404
                       " FROM tbl_combined_equipments "
405
                       " WHERE name = %s AND id != %s ", (name, id_))
406
        if cursor.fetchone() is not None:
407
            cursor.close()
408
            cnx.close()
409
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
410
                                   description='API.COMBINED_EQUIPMENT_NAME_IS_ALREADY_IN_USE')
411
412
        cursor.execute(" SELECT name "
413
                       " FROM tbl_cost_centers "
414
                       " WHERE id = %s ",
415
                       (new_values['data']['cost_center_id'],))
416
        row = cursor.fetchone()
417
        if row is None:
418
            cursor.close()
419
            cnx.close()
420
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
421
                                   description='API.COST_CENTER_NOT_FOUND')
422
423
        if svg_id is not None:
424
            cursor.execute(" SELECT name "
425
                           " FROM tbl_svgs "
426
                           " WHERE id = %s ",
427
                           (new_values['data']['svg_id'],))
428
            row = cursor.fetchone()
429
            if row is None:
430
                cursor.close()
431
                cnx.close()
432
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
433
                                       description='API.SVG_NOT_FOUND')
434
435
        update_row = (" UPDATE tbl_combined_equipments "
436
                      " SET name = %s, is_input_counted = %s, is_output_counted = %s, "
437
                      "     cost_center_id = %s, svg_id = %s, camera_url = %s, description = %s "
438
                      " WHERE id = %s ")
439
        cursor.execute(update_row, (name,
440
                                    is_input_counted,
441
                                    is_output_counted,
442
                                    cost_center_id,
443
                                    svg_id,
444
                                    camera_url,
445
                                    description,
446
                                    id_))
447
        cnx.commit()
448
449
        cursor.close()
450
        cnx.close()
451
452
        resp.status = falcon.HTTP_200
453
454
    # Clone a Combined Equipment
455
    @staticmethod