Code Duplication    Length = 55-57 lines in 2 locations

energyitem.py 1 location

@@ 161-217 (lines=57) @@
158
                  "energy_category": energy_category}
159
        resp.body = json.dumps(result)
160
161
    @staticmethod
162
    def on_delete(req, resp, id_):
163
        if not id_.isdigit() or int(id_) <= 0:
164
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
165
                                   description='API.INVALID_ENERGY_ITEM_ID')
166
167
        cnx = mysql.connector.connect(**config.myems_system_db)
168
        cursor = cnx.cursor()
169
170
        cursor.execute(" SELECT name "
171
                       " FROM tbl_energy_items "
172
                       " WHERE id = %s ", (id_,))
173
        if cursor.fetchone() is None:
174
            cursor.close()
175
            cnx.disconnect()
176
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
177
                                   description='API.ENERGY_ITEM_NOT_FOUND')
178
179
        cursor.execute(" SELECT id "
180
                       " FROM tbl_meters "
181
                       " WHERE energy_item_id = %s ", (id_,))
182
        rows_meters = cursor.fetchall()
183
        if rows_meters is not None and len(rows_meters) > 0:
184
            cursor.close()
185
            cnx.disconnect()
186
            raise falcon.HTTPError(falcon.HTTP_400,
187
                                   title='API.BAD_REQUEST',
188
                                   description='API.ENERGY_ITEM_USED_IN_METER')
189
190
        cursor.execute(" SELECT id "
191
                       " FROM tbl_virtual_meters "
192
                       " WHERE energy_item_id = %s ", (id_,))
193
        rows_virtual_meters = cursor.fetchall()
194
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
195
            cursor.close()
196
            cnx.disconnect()
197
            raise falcon.HTTPError(falcon.HTTP_400,
198
                                   title='API.BAD_REQUEST',
199
                                   description='API.ENERGY_ITEM_USED_IN_VIRTUAL_METER')
200
201
        cursor.execute(" SELECT id "
202
                       " FROM tbl_offline_meters "
203
                       " WHERE energy_item_id = %s ", (id_,))
204
        rows_offline_meters = cursor.fetchall()
205
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
206
            cursor.close()
207
            cnx.disconnect()
208
            raise falcon.HTTPError(falcon.HTTP_400,
209
                                   title='API.BAD_REQUEST',
210
                                   description='API.ENERGY_ITEM_USED_IN_OFFLINE_METER')
211
212
        cursor.execute(" DELETE FROM tbl_energy_items WHERE id = %s ", (id_,))
213
        cnx.commit()
214
215
        cursor.close()
216
        cnx.disconnect()
217
        resp.status = falcon.HTTP_204
218
219
    @staticmethod
220
    def on_put(req, resp, id_):

tariff.py 1 location

@@ 294-348 (lines=55) @@
291
292
        resp.body = json.dumps(result)
293
294
    @staticmethod
295
    def on_delete(req, resp, id_):
296
        if not id_.isdigit() or int(id_) <= 0:
297
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
298
                                   description='API.INVALID_TARIFF_ID')
299
300
        cnx = mysql.connector.connect(**config.myems_system_db)
301
        cursor = cnx.cursor()
302
303
        cursor.execute(" SELECT name "
304
                       " FROM tbl_tariffs "
305
                       " WHERE id = %s ", (id_,))
306
        if cursor.fetchone() is None:
307
            cursor.close()
308
            cnx.disconnect()
309
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
310
                                   description='API.TARIFF_NOT_FOUND')
311
312
        cursor.execute(" SELECT id "
313
                       " FROM tbl_tariffs_blocks "
314
                       " WHERE tariff_id = %s ", (id_,))
315
        rows = cursor.fetchall()
316
        if rows is not None and len(rows) > 0:
317
            cursor.close()
318
            cnx.disconnect()
319
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
320
                                   description='API.TARIFF_NOT_EMPTY')
321
322
        cursor.execute(" SELECT id "
323
                       " FROM tbl_tariffs_timeofuses "
324
                       " WHERE tariff_id = %s ", (id_,))
325
        rows = cursor.fetchall()
326
        if rows is not None and len(rows) > 0:
327
            cursor.close()
328
            cnx.disconnect()
329
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
330
                                   description='API.TARIFF_NOT_EMPTY')
331
332
        cursor.execute(" SELECT id "
333
                       " FROM tbl_cost_centers_tariffs "
334
                       " WHERE tariff_id = %s ", (id_,))
335
        rows = cursor.fetchall()
336
        if rows is not None and len(rows) > 0:
337
            cursor.close()
338
            cnx.disconnect()
339
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
340
                                   description='API.TARIFF_IN_USE')
341
342
        cursor.execute(" DELETE FROM tbl_tariffs WHERE id = %s ", (id_,))
343
        cnx.commit()
344
345
        cursor.close()
346
        cnx.disconnect()
347
348
        resp.status = falcon.HTTP_204
349
350
    @staticmethod
351
    def on_put(req, resp, id_):