Code Duplication    Length = 17-17 lines in 10 locations

myems-api/core/meter.py 5 locations

@@ 1585-1601 (lines=17) @@
1582
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
1583
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
1584
1585
        if master_meter_id is not None:
1586
            cursor.execute(" SELECT name, energy_category_id "
1587
                           " FROM tbl_meters "
1588
                           " WHERE id = %s ",
1589
                           (master_meter_id,))
1590
            row = cursor.fetchone()
1591
            if row is None:
1592
                cursor.close()
1593
                cnx.close()
1594
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1595
                                       description='API.MASTER_METER_NOT_FOUND')
1596
            else:
1597
                if row[1] != energy_category_id:
1598
                    cursor.close()
1599
                    cnx.close()
1600
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
1601
                                           description='API.MASTER_METER_DOES_NOT_BELONG_TO_SAME_ENERGY_CATEGORY')
1602
1603
        add_values = (" INSERT INTO tbl_meters "
1604
                      "    (name, uuid, energy_category_id, is_counted, hourly_low_limit, hourly_high_limit,"
@@ 801-817 (lines=17) @@
798
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
799
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
800
801
        if master_meter_id is not None:
802
            cursor.execute(" SELECT name, energy_category_id "
803
                           " FROM tbl_meters "
804
                           " WHERE id = %s ",
805
                           (new_values['data']['master_meter_id'],))
806
            row = cursor.fetchone()
807
            if row is None:
808
                cursor.close()
809
                cnx.close()
810
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
811
                                       description='API.MASTER_METER_NOT_FOUND')
812
            else:
813
                if row[1] != energy_category_id:
814
                    cursor.close()
815
                    cnx.close()
816
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
817
                                           description='API.MASTER_METER_DOES_NOT_BELONG_TO_SAME_ENERGY_CATEGORY')
818
819
        # todo: check all descendants against new_values['data']['master_meter_id']
820
        if master_meter_id is not None:
@@ 783-799 (lines=17) @@
780
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
781
                                   description='API.COST_CENTER_NOT_FOUND')
782
783
        if energy_item_id is not None:
784
            cursor.execute(" SELECT name, energy_category_id "
785
                           " FROM tbl_energy_items "
786
                           " WHERE id = %s ",
787
                           (new_values['data']['energy_item_id'],))
788
            row = cursor.fetchone()
789
            if row is None:
790
                cursor.close()
791
                cnx.close()
792
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
793
                                       description='API.ENERGY_ITEM_NOT_FOUND')
794
            else:
795
                if row[1] != energy_category_id:
796
                    cursor.close()
797
                    cnx.close()
798
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
799
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
800
801
        if master_meter_id is not None:
802
            cursor.execute(" SELECT name, energy_category_id "
@@ 239-255 (lines=17) @@
236
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
237
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
238
239
        if master_meter_id is not None:
240
            cursor.execute(" SELECT name, energy_category_id "
241
                           " FROM tbl_meters "
242
                           " WHERE id = %s ",
243
                           (new_values['data']['master_meter_id'],))
244
            row = cursor.fetchone()
245
            if row is None:
246
                cursor.close()
247
                cnx.close()
248
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
249
                                       description='API.MASTER_METER_NOT_FOUND')
250
            else:
251
                if row[1] != energy_category_id:
252
                    cursor.close()
253
                    cnx.close()
254
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
255
                                           description='API.MASTER_METER_DOES_NOT_BELONG_TO_SAME_ENERGY_CATEGORY')
256
257
        add_values = (" INSERT INTO tbl_meters "
258
                      "    (name, uuid, energy_category_id, is_counted, hourly_low_limit, hourly_high_limit,"
@@ 221-237 (lines=17) @@
218
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
219
                                   description='API.COST_CENTER_NOT_FOUND')
220
221
        if energy_item_id is not None:
222
            cursor.execute(" SELECT name, energy_category_id "
223
                           " FROM tbl_energy_items "
224
                           " WHERE id = %s ",
225
                           (new_values['data']['energy_item_id'],))
226
            row = cursor.fetchone()
227
            if row is None:
228
                cursor.close()
229
                cnx.close()
230
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
231
                                       description='API.ENERGY_ITEM_NOT_FOUND')
232
            else:
233
                if row[1] != energy_category_id:
234
                    cursor.close()
235
                    cnx.close()
236
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
237
                                           description='API.ENERGY_ITEM_DOES_NOT_BELONG_TO_ENERGY_CATEGORY')
238
239
        if master_meter_id is not None:
240
            cursor.execute(" SELECT name, energy_category_id "

myems-api/core/virtualmeter.py 2 locations

@@ 1141-1157 (lines=17) @@
1138
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1139
                                   description='API.COST_CENTER_NOT_FOUND')
1140
1141
        if energy_item_id is not None:
1142
            cursor.execute(" SELECT name, energy_category_id "
1143
                           " FROM tbl_energy_items "
1144
                           " WHERE id = %s ",
1145
                           (new_values['energy_item']['id'],))
1146
            row = cursor.fetchone()
1147
            if row is None:
1148
                cursor.close()
1149
                cnx.close()
1150
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
1151
                                       description='API.ENERGY_ITEM_NOT_FOUND')
1152
            else:
1153
                if row[1] != energy_category_id:
1154
                    cursor.close()
1155
                    cnx.close()
1156
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
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':
@@ 263-279 (lines=17) @@
260
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
261
                                   description='API.COST_CENTER_NOT_FOUND')
262
263
        if energy_item_id is not None:
264
            cursor.execute(" SELECT name, energy_category_id "
265
                           " FROM tbl_energy_items "
266
                           " WHERE id = %s ",
267
                           (new_values['data']['energy_item_id'],))
268
            row = cursor.fetchone()
269
            if row is None:
270
                cursor.close()
271
                cnx.close()
272
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
273
                                       description='API.ENERGY_ITEM_NOT_FOUND')
274
            else:
275
                if row[1] != energy_category_id:
276
                    cursor.close()
277
                    cnx.close()
278
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
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':

myems-api/core/offlinemeter.py 3 locations

@@ 831-847 (lines=17) @@
828
            cnx.close()
829
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
830
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
831
        if energy_item_id is not None:
832
            cursor.execute(" SELECT name, energy_category_id "
833
                           " FROM tbl_energy_items "
834
                           " WHERE id = %s ",
835
                           (new_values['energy_item']['id'],))
836
            row = cursor.fetchone()
837
            if row is None:
838
                cursor.close()
839
                cnx.close()
840
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
841
                                       description='API.ENERGY_ITEM_NOT_FOUND')
842
            else:
843
                if row[1] != energy_category_id:
844
                    cursor.close()
845
                    cnx.close()
846
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
847
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
848
849
        cursor.execute(" SELECT name "
850
                       " FROM tbl_cost_centers "
@@ 600-616 (lines=17) @@
597
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
598
                                   description='API.COST_CENTER_NOT_FOUND')
599
600
        if energy_item_id is not None:
601
            cursor.execute(" SELECT name, energy_category_id "
602
                           " FROM tbl_energy_items "
603
                           " WHERE id = %s ",
604
                           (new_values['data']['energy_item_id'],))
605
            row = cursor.fetchone()
606
            if row is None:
607
                cursor.close()
608
                cnx.close()
609
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
610
                                       description='API.ENERGY_ITEM_NOT_FOUND')
611
            else:
612
                if row[1] != energy_category_id:
613
                    cursor.close()
614
                    cnx.close()
615
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
616
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
617
618
        update_row = (" UPDATE tbl_offline_meters "
619
                      " SET name = %s, energy_category_id = %s,"
@@ 187-203 (lines=17) @@
184
            cnx.close()
185
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
186
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
187
        if energy_item_id is not None:
188
            cursor.execute(" SELECT name, energy_category_id "
189
                           " FROM tbl_energy_items "
190
                           " WHERE id = %s ",
191
                           (new_values['data']['energy_item_id'],))
192
            row = cursor.fetchone()
193
            if row is None:
194
                cursor.close()
195
                cnx.close()
196
                raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
197
                                       description='API.ENERGY_ITEM_NOT_FOUND')
198
            else:
199
                if row[1] != energy_category_id:
200
                    cursor.close()
201
                    cnx.close()
202
                    raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST',
203
                                           description='API.ENERGY_ITEM_IS_NOT_BELONG_TO_ENERGY_CATEGORY')
204
205
        cursor.execute(" SELECT name "
206
                       " FROM tbl_cost_centers "