Code Duplication    Length = 30-30 lines in 3 locations

myems-api/core/virtualmeter.py 3 locations

@@ 1160-1189 (lines=30) @@
1157
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
1158
1159
        for variable in new_values['expression']['variables']:
1160
            if variable['meter_type'].lower() == 'meter':
1161
                cursor.execute(" SELECT name "
1162
                               " FROM tbl_meters "
1163
                               " WHERE id = %s ", (variable['meter_id'],))
1164
                if cursor.fetchone() is None:
1165
                    cursor.close()
1166
                    cnx.close()
1167
                    raise falcon.HTTPError(status=falcon.HTTP_404,
1168
                                           title='API.NOT_FOUND',
1169
                                           description='API.METER_OF_VARIABLE_NOT_FOUND')
1170
            elif variable['meter_type'].lower() == 'offline_meter':
1171
                cursor.execute(" SELECT name "
1172
                               " FROM tbl_offline_meters "
1173
                               " WHERE id = %s ", (variable['meter_id'],))
1174
                if cursor.fetchone() is None:
1175
                    cursor.close()
1176
                    cnx.close()
1177
                    raise falcon.HTTPError(status=falcon.HTTP_404,
1178
                                           title='API.NOT_FOUND',
1179
                                           description='API.OFFLINE_METER_OF_VARIABLE_NOT_FOUND')
1180
            elif variable['meter_type'].lower() == 'virtual_meter':
1181
                cursor.execute(" SELECT name "
1182
                               " FROM tbl_virtual_meters "
1183
                               " WHERE id = %s ", (variable['meter_id'],))
1184
                if cursor.fetchone() is None:
1185
                    cursor.close()
1186
                    cnx.close()
1187
                    raise falcon.HTTPError(status=falcon.HTTP_404,
1188
                                           title='API.NOT_FOUND',
1189
                                           description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
1190
1191
        add_values = (" INSERT INTO tbl_virtual_meters "
1192
                      "     (name, uuid, equation, energy_category_id, is_counted, "
@@ 797-826 (lines=30) @@
794
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
795
796
        for variable in new_values['data']['expression']['variables']:
797
            if variable['meter_type'].lower() == 'meter':
798
                cursor.execute(" SELECT name "
799
                               " FROM tbl_meters "
800
                               " WHERE id = %s ", (variable['meter_id'],))
801
                if cursor.fetchone() is None:
802
                    cursor.close()
803
                    cnx.close()
804
                    raise falcon.HTTPError(status=falcon.HTTP_404,
805
                                           title='API.NOT_FOUND',
806
                                           description='API.METER_OF_VARIABLE_NOT_FOUND')
807
            elif variable['meter_type'].lower() == 'offline_meter':
808
                cursor.execute(" SELECT name "
809
                               " FROM tbl_offline_meters "
810
                               " WHERE id = %s ", (variable['meter_id'],))
811
                if cursor.fetchone() is None:
812
                    cursor.close()
813
                    cnx.close()
814
                    raise falcon.HTTPError(status=falcon.HTTP_404,
815
                                           title='API.NOT_FOUND',
816
                                           description='API.OFFLINE_METER_OF_VARIABLE_NOT_FOUND')
817
            elif variable['meter_type'].lower() == 'virtual_meter':
818
                cursor.execute(" SELECT name "
819
                               " FROM tbl_virtual_meters "
820
                               " WHERE id = %s ", (variable['meter_id'],))
821
                if cursor.fetchone() is None:
822
                    cursor.close()
823
                    cnx.close()
824
                    raise falcon.HTTPError(status=falcon.HTTP_404,
825
                                           title='API.NOT_FOUND',
826
                                           description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
827
        try:
828
            update_row = (" UPDATE tbl_virtual_meters "
829
                          " SET name = %s, equation = %s, energy_category_id = %s, is_counted = %s, "
@@ 282-311 (lines=30) @@
279
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
280
281
        for variable in new_values['data']['expression']['variables']:
282
            if variable['meter_type'].lower() == 'meter':
283
                cursor.execute(" SELECT name "
284
                               " FROM tbl_meters "
285
                               " WHERE id = %s ", (variable['meter_id'],))
286
                if cursor.fetchone() is None:
287
                    cursor.close()
288
                    cnx.close()
289
                    raise falcon.HTTPError(status=falcon.HTTP_404,
290
                                           title='API.NOT_FOUND',
291
                                           description='API.METER_OF_VARIABLE_NOT_FOUND')
292
            elif variable['meter_type'].lower() == 'offline_meter':
293
                cursor.execute(" SELECT name "
294
                               " FROM tbl_offline_meters "
295
                               " WHERE id = %s ", (variable['meter_id'],))
296
                if cursor.fetchone() is None:
297
                    cursor.close()
298
                    cnx.close()
299
                    raise falcon.HTTPError(status=falcon.HTTP_404,
300
                                           title='API.NOT_FOUND',
301
                                           description='API.OFFLINE_METER_OF_VARIABLE_NOT_FOUND')
302
            elif variable['meter_type'].lower() == 'virtual_meter':
303
                cursor.execute(" SELECT name "
304
                               " FROM tbl_virtual_meters "
305
                               " WHERE id = %s ", (variable['meter_id'],))
306
                if cursor.fetchone() is None:
307
                    cursor.close()
308
                    cnx.close()
309
                    raise falcon.HTTPError(status=falcon.HTTP_404,
310
                                           title='API.NOT_FOUND',
311
                                           description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
312
313
        add_values = (" INSERT INTO tbl_virtual_meters "
314
                      "     (name, uuid, equation, energy_category_id, is_counted, "