Code Duplication    Length = 127-127 lines in 2 locations

myems-api/core/equipment.py 1 location

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

myems-api/core/combinedequipment.py 1 location

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