Code Duplication    Length = 102-102 lines in 2 locations

core/tenant.py 1 location

@@ 383-484 (lines=102) @@
380
381
        resp.body = json.dumps(meta_result)
382
383
    @staticmethod
384
    def on_delete(req, resp, id_):
385
        if not id_.isdigit() or int(id_) <= 0:
386
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
387
                                   description='API.INVALID_TENANT_ID')
388
389
        cnx = mysql.connector.connect(**config.myems_system_db)
390
        cursor = cnx.cursor()
391
392
        cursor.execute(" SELECT name "
393
                       " FROM tbl_tenants "
394
                       " WHERE id = %s ", (id_,))
395
        if cursor.fetchone() is None:
396
            cursor.close()
397
            cnx.disconnect()
398
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
399
                                   description='API.TENANT_NOT_FOUND')
400
401
        # check relation with space
402
        cursor.execute(" SELECT space_id "
403
                       " FROM tbl_spaces_tenants "
404
                       " WHERE tenant_id = %s ",
405
                       (id_,))
406
        rows_spaces = cursor.fetchall()
407
        if rows_spaces is not None and len(rows_spaces) > 0:
408
            cursor.close()
409
            cnx.disconnect()
410
            raise falcon.HTTPError(falcon.HTTP_400,
411
                                   title='API.BAD_REQUEST',
412
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
413
414
        # check relation with meter
415
        cursor.execute(" SELECT meter_id "
416
                       " FROM tbl_tenants_meters "
417
                       " WHERE tenant_id = %s ",
418
                       (id_,))
419
        rows_meters = cursor.fetchall()
420
        if rows_meters is not None and len(rows_meters) > 0:
421
            cursor.close()
422
            cnx.disconnect()
423
            raise falcon.HTTPError(falcon.HTTP_400,
424
                                   title='API.BAD_REQUEST',
425
                                   description='API.THERE_IS_RELATION_WITH_METERS')
426
427
        # check relation with offline meter
428
        cursor.execute(" SELECT offline_meter_id "
429
                       " FROM tbl_tenants_offline_meters "
430
                       " WHERE tenant_id = %s ",
431
                       (id_,))
432
        rows_offline_meters = cursor.fetchall()
433
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
434
            cursor.close()
435
            cnx.disconnect()
436
            raise falcon.HTTPError(falcon.HTTP_400,
437
                                   title='API.BAD_REQUEST',
438
                                   description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS')
439
440
        # check relation with points
441
        cursor.execute(" SELECT point_id "
442
                       " FROM tbl_tenants_points "
443
                       " WHERE tenant_id = %s ", (id_,))
444
        rows_points = cursor.fetchall()
445
        if rows_points is not None and len(rows_points) > 0:
446
            cursor.close()
447
            cnx.disconnect()
448
            raise falcon.HTTPError(falcon.HTTP_400,
449
                                   title='API.BAD_REQUEST',
450
                                   description='API.THERE_IS_RELATION_WITH_POINTS')
451
452
        # check relation with sensor
453
        cursor.execute(" SELECT sensor_id "
454
                       " FROM tbl_tenants_sensors "
455
                       " WHERE tenant_id = %s ",
456
                       (id_,))
457
        rows_sensors = cursor.fetchall()
458
        if rows_sensors is not None and len(rows_sensors) > 0:
459
            cursor.close()
460
            cnx.disconnect()
461
            raise falcon.HTTPError(falcon.HTTP_400,
462
                                   title='API.BAD_REQUEST',
463
                                   description='API.THERE_IS_RELATION_WITH_SENSOR')
464
465
        # check relation with virtual meter
466
        cursor.execute(" SELECT virtual_meter_id "
467
                       " FROM tbl_tenants_virtual_meters "
468
                       " WHERE tenant_id = %s ",
469
                       (id_,))
470
        rows_virtual_meters = cursor.fetchall()
471
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
472
            cursor.close()
473
            cnx.disconnect()
474
            raise falcon.HTTPError(falcon.HTTP_400,
475
                                   title='API.BAD_REQUEST',
476
                                   description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER')
477
478
        cursor.execute(" DELETE FROM tbl_tenants WHERE id = %s ", (id_,))
479
        cnx.commit()
480
481
        cursor.close()
482
        cnx.disconnect()
483
484
        resp.status = falcon.HTTP_204
485
486
    @staticmethod
487
    def on_put(req, resp, id_):

core/store.py 1 location

@@ 326-427 (lines=102) @@
323
324
        resp.body = json.dumps(meta_result)
325
326
    @staticmethod
327
    def on_delete(req, resp, id_):
328
        if not id_.isdigit() or int(id_) <= 0:
329
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
330
                                   description='API.INVALID_STORE_ID')
331
332
        cnx = mysql.connector.connect(**config.myems_system_db)
333
        cursor = cnx.cursor()
334
335
        cursor.execute(" SELECT name "
336
                       " FROM tbl_stores "
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.STORE_NOT_FOUND')
343
344
        # check relation with space
345
        cursor.execute(" SELECT space_id "
346
                       " FROM tbl_spaces_stores "
347
                       " WHERE store_id = %s ",
348
                       (id_,))
349
        rows_spaces = cursor.fetchall()
350
        if rows_spaces is not None and len(rows_spaces) > 0:
351
            cursor.close()
352
            cnx.disconnect()
353
            raise falcon.HTTPError(falcon.HTTP_400,
354
                                   title='API.BAD_REQUEST',
355
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
356
357
        # check relation with meter
358
        cursor.execute(" SELECT meter_id "
359
                       " FROM tbl_stores_meters "
360
                       " WHERE store_id = %s ",
361
                       (id_,))
362
        rows_meters = cursor.fetchall()
363
        if rows_meters is not None and len(rows_meters) > 0:
364
            cursor.close()
365
            cnx.disconnect()
366
            raise falcon.HTTPError(falcon.HTTP_400,
367
                                   title='API.BAD_REQUEST',
368
                                   description='API.THERE_IS_RELATION_WITH_METERS')
369
370
        # check relation with offline meter
371
        cursor.execute(" SELECT offline_meter_id "
372
                       " FROM tbl_stores_offline_meters "
373
                       " WHERE store_id = %s ",
374
                       (id_,))
375
        rows_offline_meters = cursor.fetchall()
376
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
377
            cursor.close()
378
            cnx.disconnect()
379
            raise falcon.HTTPError(falcon.HTTP_400,
380
                                   title='API.BAD_REQUEST',
381
                                   description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS')
382
383
        # check relation with points
384
        cursor.execute(" SELECT point_id "
385
                       " FROM tbl_stores_points "
386
                       " WHERE store_id = %s ", (id_,))
387
        rows_points = cursor.fetchall()
388
        if rows_points is not None and len(rows_points) > 0:
389
            cursor.close()
390
            cnx.disconnect()
391
            raise falcon.HTTPError(falcon.HTTP_400,
392
                                   title='API.BAD_REQUEST',
393
                                   description='API.THERE_IS_RELATION_WITH_POINTS')
394
395
        # check relation with sensor
396
        cursor.execute(" SELECT sensor_id "
397
                       " FROM tbl_stores_sensors "
398
                       " WHERE store_id = %s ",
399
                       (id_,))
400
        rows_sensors = cursor.fetchall()
401
        if rows_sensors is not None and len(rows_sensors) > 0:
402
            cursor.close()
403
            cnx.disconnect()
404
            raise falcon.HTTPError(falcon.HTTP_400,
405
                                   title='API.BAD_REQUEST',
406
                                   description='API.THERE_IS_RELATION_WITH_SENSORS')
407
408
        # check relation with virtual meter
409
        cursor.execute(" SELECT virtual_meter_id "
410
                       " FROM tbl_stores_virtual_meters "
411
                       " WHERE store_id = %s ",
412
                       (id_,))
413
        rows_virtual_meters = cursor.fetchall()
414
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
415
            cursor.close()
416
            cnx.disconnect()
417
            raise falcon.HTTPError(falcon.HTTP_400,
418
                                   title='API.BAD_REQUEST',
419
                                   description='API.THERE_IS_RELATION_WITH_VIRTUAL_METERS')
420
421
        cursor.execute(" DELETE FROM tbl_stores WHERE id = %s ", (id_,))
422
        cnx.commit()
423
424
        cursor.close()
425
        cnx.disconnect()
426
427
        resp.status = falcon.HTTP_204
428
429
    @staticmethod
430
    def on_put(req, resp, id_):