Code Duplication    Length = 17-17 lines in 10 locations

myems-api/core/meter.py 5 locations

@@ 1728-1744 (lines=17) @@
1725
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
1726
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
1727
1728
        if master_meter_id is not None:
1729
            cursor.execute(" SELECT name, energy_category_id "
1730
                           " FROM tbl_meters "
1731
                           " WHERE id = %s ",
1732
                           (master_meter_id,))
1733
            row = cursor.fetchone()
1734
            if row is None:
1735
                cursor.close()
1736
                cnx.close()
1737
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1738
                                       description='API.MASTER_METER_NOT_FOUND')
1739
            else:
1740
                if row[1] != energy_category_id:
1741
                    cursor.close()
1742
                    cnx.close()
1743
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
1744
                                           description='API.MASTER_METER_DOES_NOT_BELONG_TO_SAME_ENERGY_CATEGORY')
1745
1746
        add_values = (" INSERT INTO tbl_meters "
1747
                      "    (name, uuid, energy_category_id, is_counted, hourly_low_limit, hourly_high_limit,"
@@ 922-938 (lines=17) @@
919
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
920
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
921
922
        if master_meter_id is not None:
923
            cursor.execute(" SELECT name, energy_category_id "
924
                           " FROM tbl_meters "
925
                           " WHERE id = %s ",
926
                           (new_values['data']['master_meter_id'],))
927
            row = cursor.fetchone()
928
            if row is None:
929
                cursor.close()
930
                cnx.close()
931
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
932
                                       description='API.MASTER_METER_NOT_FOUND')
933
            else:
934
                if row[1] != energy_category_id:
935
                    cursor.close()
936
                    cnx.close()
937
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
938
                                           description='API.MASTER_METER_DOES_NOT_BELONG_TO_SAME_ENERGY_CATEGORY')
939
940
        # todo: check all descendants against new_values['data']['master_meter_id']
941
        if master_meter_id is not None:
@@ 904-920 (lines=17) @@
901
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
902
                                   description='API.COST_CENTER_NOT_FOUND')
903
904
        if energy_item_id is not None:
905
            cursor.execute(" SELECT name, energy_category_id "
906
                           " FROM tbl_energy_items "
907
                           " WHERE id = %s ",
908
                           (new_values['data']['energy_item_id'],))
909
            row = cursor.fetchone()
910
            if row is None:
911
                cursor.close()
912
                cnx.close()
913
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
914
                                       description='API.ENERGY_ITEM_NOT_FOUND')
915
            else:
916
                if row[1] != energy_category_id:
917
                    cursor.close()
918
                    cnx.close()
919
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
920
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
921
922
        if master_meter_id is not None:
923
            cursor.execute(" SELECT name, energy_category_id "
@@ 266-282 (lines=17) @@
263
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
264
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
265
266
        if master_meter_id is not None:
267
            cursor.execute(" SELECT name, energy_category_id "
268
                           " FROM tbl_meters "
269
                           " WHERE id = %s ",
270
                           (new_values['data']['master_meter_id'],))
271
            row = cursor.fetchone()
272
            if row is None:
273
                cursor.close()
274
                cnx.close()
275
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
276
                                       description='API.MASTER_METER_NOT_FOUND')
277
            else:
278
                if row[1] != energy_category_id:
279
                    cursor.close()
280
                    cnx.close()
281
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
282
                                           description='API.MASTER_METER_DOES_NOT_BELONG_TO_SAME_ENERGY_CATEGORY')
283
284
        add_values = (" INSERT INTO tbl_meters "
285
                      "    (name, uuid, energy_category_id, is_counted, hourly_low_limit, hourly_high_limit,"
@@ 248-264 (lines=17) @@
245
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
246
                                   description='API.COST_CENTER_NOT_FOUND')
247
248
        if energy_item_id is not None:
249
            cursor.execute(" SELECT name, energy_category_id "
250
                           " FROM tbl_energy_items "
251
                           " WHERE id = %s ",
252
                           (new_values['data']['energy_item_id'],))
253
            row = cursor.fetchone()
254
            if row is None:
255
                cursor.close()
256
                cnx.close()
257
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
258
                                       description='API.ENERGY_ITEM_NOT_FOUND')
259
            else:
260
                if row[1] != energy_category_id:
261
                    cursor.close()
262
                    cnx.close()
263
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
264
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
265
266
        if master_meter_id is not None:
267
            cursor.execute(" SELECT name, energy_category_id "

myems-api/core/virtualmeter.py 2 locations

@@ 1216-1232 (lines=17) @@
1213
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1214
                                   description='API.COST_CENTER_NOT_FOUND')
1215
1216
        if energy_item_id is not None:
1217
            cursor.execute(" SELECT name, energy_category_id "
1218
                           " FROM tbl_energy_items "
1219
                           " WHERE id = %s ",
1220
                           (new_values['energy_item']['id'],))
1221
            row = cursor.fetchone()
1222
            if row is None:
1223
                cursor.close()
1224
                cnx.close()
1225
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1226
                                       description='API.ENERGY_ITEM_NOT_FOUND')
1227
            else:
1228
                if row[1] != energy_category_id:
1229
                    cursor.close()
1230
                    cnx.close()
1231
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
1232
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
1233
1234
        for variable in new_values['expression']['variables']:
1235
            if variable['meter_type'].lower() == 'meter':
@@ 289-305 (lines=17) @@
286
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
287
                                   description='API.COST_CENTER_NOT_FOUND')
288
289
        if energy_item_id is not None:
290
            cursor.execute(" SELECT name, energy_category_id "
291
                           " FROM tbl_energy_items "
292
                           " WHERE id = %s ",
293
                           (new_values['data']['energy_item_id'],))
294
            row = cursor.fetchone()
295
            if row is None:
296
                cursor.close()
297
                cnx.close()
298
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
299
                                       description='API.ENERGY_ITEM_NOT_FOUND')
300
            else:
301
                if row[1] != energy_category_id:
302
                    cursor.close()
303
                    cnx.close()
304
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
305
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
306
307
        for variable in new_values['data']['expression']['variables']:
308
            if variable['meter_type'].lower() == 'meter':

myems-api/core/offlinemeter.py 3 locations

@@ 896-912 (lines=17) @@
893
            cnx.close()
894
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
895
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
896
        if energy_item_id is not None:
897
            cursor.execute(" SELECT name, energy_category_id "
898
                           " FROM tbl_energy_items "
899
                           " WHERE id = %s ",
900
                           (new_values['energy_item']['id'],))
901
            row = cursor.fetchone()
902
            if row is None:
903
                cursor.close()
904
                cnx.close()
905
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
906
                                       description='API.ENERGY_ITEM_NOT_FOUND')
907
            else:
908
                if row[1] != energy_category_id:
909
                    cursor.close()
910
                    cnx.close()
911
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
912
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
913
914
        cursor.execute(" SELECT name "
915
                       " FROM tbl_cost_centers "
@@ 653-669 (lines=17) @@
650
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
651
                                   description='API.COST_CENTER_NOT_FOUND')
652
653
        if energy_item_id is not None:
654
            cursor.execute(" SELECT name, energy_category_id "
655
                           " FROM tbl_energy_items "
656
                           " WHERE id = %s ",
657
                           (new_values['data']['energy_item_id'],))
658
            row = cursor.fetchone()
659
            if row is None:
660
                cursor.close()
661
                cnx.close()
662
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
663
                                       description='API.ENERGY_ITEM_NOT_FOUND')
664
            else:
665
                if row[1] != energy_category_id:
666
                    cursor.close()
667
                    cnx.close()
668
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
669
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
670
671
        update_row = (" UPDATE tbl_offline_meters "
672
                      " SET name = %s, energy_category_id = %s,"
@@ 233-249 (lines=17) @@
230
            cnx.close()
231
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
232
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
233
        if energy_item_id is not None:
234
            cursor.execute(" SELECT name, energy_category_id "
235
                           " FROM tbl_energy_items "
236
                           " WHERE id = %s ",
237
                           (new_values['data']['energy_item_id'],))
238
            row = cursor.fetchone()
239
            if row is None:
240
                cursor.close()
241
                cnx.close()
242
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
243
                                       description='API.ENERGY_ITEM_NOT_FOUND')
244
            else:
245
                if row[1] != energy_category_id:
246
                    cursor.close()
247
                    cnx.close()
248
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
249
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
250
251
        cursor.execute(" SELECT name "
252
                       " FROM tbl_cost_centers "