Code Duplication    Length = 30-30 lines in 3 locations

myems-api/core/virtualmeter.py 3 locations

@@ 802-831 (lines=30) @@
799
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
800
801
        for variable in new_values['data']['expression']['variables']:
802
            if variable['meter_type'].lower() == 'meter':
803
                cursor.execute(" SELECT name "
804
                               " FROM tbl_meters "
805
                               " WHERE id = %s ", (variable['meter_id'],))
806
                if cursor.fetchone() is None:
807
                    cursor.close()
808
                    cnx.close()
809
                    raise falcon.HTTPError(status=falcon.HTTP_404,
810
                                           title='API.NOT_FOUND',
811
                                           description='API.METER_OF_VARIABLE_NOT_FOUND')
812
            elif variable['meter_type'].lower() == 'offline_meter':
813
                cursor.execute(" SELECT name "
814
                               " FROM tbl_offline_meters "
815
                               " WHERE id = %s ", (variable['meter_id'],))
816
                if cursor.fetchone() is None:
817
                    cursor.close()
818
                    cnx.close()
819
                    raise falcon.HTTPError(status=falcon.HTTP_404,
820
                                           title='API.NOT_FOUND',
821
                                           description='API.OFFLINE_METER_OF_VARIABLE_NOT_FOUND')
822
            elif variable['meter_type'].lower() == 'virtual_meter':
823
                cursor.execute(" SELECT name "
824
                               " FROM tbl_virtual_meters "
825
                               " WHERE id = %s ", (variable['meter_id'],))
826
                if cursor.fetchone() is None:
827
                    cursor.close()
828
                    cnx.close()
829
                    raise falcon.HTTPError(status=falcon.HTTP_404,
830
                                           title='API.NOT_FOUND',
831
                                           description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
832
        try:
833
            update_row = (" UPDATE tbl_virtual_meters "
834
                          " SET name = %s, equation = %s, energy_category_id = %s, is_counted = %s, "
@@ 284-313 (lines=30) @@
281
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
282
283
        for variable in new_values['data']['expression']['variables']:
284
            if variable['meter_type'].lower() == 'meter':
285
                cursor.execute(" SELECT name "
286
                               " FROM tbl_meters "
287
                               " WHERE id = %s ", (variable['meter_id'],))
288
                if cursor.fetchone() is None:
289
                    cursor.close()
290
                    cnx.close()
291
                    raise falcon.HTTPError(status=falcon.HTTP_404,
292
                                           title='API.NOT_FOUND',
293
                                           description='API.METER_OF_VARIABLE_NOT_FOUND')
294
            elif variable['meter_type'].lower() == 'offline_meter':
295
                cursor.execute(" SELECT name "
296
                               " FROM tbl_offline_meters "
297
                               " WHERE id = %s ", (variable['meter_id'],))
298
                if cursor.fetchone() is None:
299
                    cursor.close()
300
                    cnx.close()
301
                    raise falcon.HTTPError(status=falcon.HTTP_404,
302
                                           title='API.NOT_FOUND',
303
                                           description='API.OFFLINE_METER_OF_VARIABLE_NOT_FOUND')
304
            elif variable['meter_type'].lower() == 'virtual_meter':
305
                cursor.execute(" SELECT name "
306
                               " FROM tbl_virtual_meters "
307
                               " WHERE id = %s ", (variable['meter_id'],))
308
                if cursor.fetchone() is None:
309
                    cursor.close()
310
                    cnx.close()
311
                    raise falcon.HTTPError(status=falcon.HTTP_404,
312
                                           title='API.NOT_FOUND',
313
                                           description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
314
315
        add_values = (" INSERT INTO tbl_virtual_meters "
316
                      "     (name, uuid, equation, energy_category_id, is_counted, "
@@ 1177-1206 (lines=30) @@
1174
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
1175
1176
        for variable in new_values['expression']['variables']:
1177
            if variable['meter_type'].lower() == 'meter':
1178
                cursor.execute(" SELECT name "
1179
                               " FROM tbl_meters "
1180
                               " WHERE id = %s ", (variable['meter_id'],))
1181
                if cursor.fetchone() is None:
1182
                    cursor.close()
1183
                    cnx.close()
1184
                    raise falcon.HTTPError(status=falcon.HTTP_404,
1185
                                           title='API.NOT_FOUND',
1186
                                           description='API.METER_OF_VARIABLE_NOT_FOUND')
1187
            elif variable['meter_type'].lower() == 'offline_meter':
1188
                cursor.execute(" SELECT name "
1189
                               " FROM tbl_offline_meters "
1190
                               " WHERE id = %s ", (variable['meter_id'],))
1191
                if cursor.fetchone() is None:
1192
                    cursor.close()
1193
                    cnx.close()
1194
                    raise falcon.HTTPError(status=falcon.HTTP_404,
1195
                                           title='API.NOT_FOUND',
1196
                                           description='API.OFFLINE_METER_OF_VARIABLE_NOT_FOUND')
1197
            elif variable['meter_type'].lower() == 'virtual_meter':
1198
                cursor.execute(" SELECT name "
1199
                               " FROM tbl_virtual_meters "
1200
                               " WHERE id = %s ", (variable['meter_id'],))
1201
                if cursor.fetchone() is None:
1202
                    cursor.close()
1203
                    cnx.close()
1204
                    raise falcon.HTTPError(status=falcon.HTTP_404,
1205
                                           title='API.NOT_FOUND',
1206
                                           description='API.VIRTUAL_METER_OF_VARIABLE_NOT_FOUND')
1207
1208
        add_values = (" INSERT INTO tbl_virtual_meters "
1209
                      "     (name, uuid, equation, energy_category_id, is_counted, "