Code Duplication    Length = 41-43 lines in 3 locations

myems-api/core/energystoragepowerstation.py 1 location

@@ 452-494 (lines=43) @@
449
450
        resp.text = json.dumps(meta_result)
451
452
    @staticmethod
453
    @user_logger
454
    def on_delete(req, resp, id_):
455
        admin_control(req)
456
        if not id_.isdigit() or int(id_) <= 0:
457
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
458
                                   description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID')
459
460
        cnx = mysql.connector.connect(**config.myems_system_db)
461
        cursor = cnx.cursor()
462
463
        cursor.execute(" SELECT name "
464
                       " FROM tbl_energy_storage_power_stations "
465
                       " WHERE id = %s ", (id_,))
466
        if cursor.fetchone() is None:
467
            cursor.close()
468
            cnx.close()
469
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
470
                                   description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND')
471
472
        # check relation with spaces
473
        cursor.execute(" SELECT id "
474
                       " FROM tbl_spaces_energy_storage_power_stations "
475
                       " WHERE energy_storage_power_station_id = %s ", (id_,))
476
        rows_spaces = cursor.fetchall()
477
        if rows_spaces is not None and len(rows_spaces) > 0:
478
            cursor.close()
479
            cnx.close()
480
            raise falcon.HTTPError(status=falcon.HTTP_400,
481
                                   title='API.BAD_REQUEST',
482
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
483
484
        cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers "
485
                       " WHERE energy_storage_power_station_id = %s ", (id_,))
486
487
        cursor.execute(" DELETE FROM tbl_energy_storage_power_stations "
488
                       " WHERE id = %s ", (id_,))
489
        cnx.commit()
490
491
        cursor.close()
492
        cnx.close()
493
494
        resp.status = falcon.HTTP_204
495
496
    @staticmethod
497
    @user_logger

myems-api/core/tariff.py 1 location

@@ 282-323 (lines=42) @@
279
280
        resp.text = json.dumps(result)
281
282
    @staticmethod
283
    @user_logger
284
    def on_delete(req, resp, id_):
285
        """Handles DELETE requests"""
286
        admin_control(req)
287
        if not id_.isdigit() or int(id_) <= 0:
288
            raise falcon.HTTPError(status=falcon.HTTP_400,
289
                                   title='API.BAD_REQUEST',
290
                                   description='API.INVALID_TARIFF_ID')
291
292
        cnx = mysql.connector.connect(**config.myems_system_db)
293
        cursor = cnx.cursor()
294
295
        cursor.execute(" SELECT name "
296
                       " FROM tbl_tariffs "
297
                       " WHERE id = %s ", (id_,))
298
        if cursor.fetchone() is None:
299
            cursor.close()
300
            cnx.close()
301
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
302
                                   description='API.TARIFF_NOT_FOUND')
303
304
        cursor.execute(" SELECT id "
305
                       " FROM tbl_cost_centers_tariffs "
306
                       " WHERE tariff_id = %s ", (id_,))
307
        rows = cursor.fetchall()
308
        if rows is not None and len(rows) > 0:
309
            cursor.close()
310
            cnx.close()
311
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
312
                                   description='API.TARIFF_IN_USE')
313
314
        cursor.execute(" DELETE FROM tbl_tariffs_timeofuses WHERE tariff_id = %s ", (id_,))
315
        cnx.commit()
316
317
        cursor.execute(" DELETE FROM tbl_tariffs WHERE id = %s ", (id_,))
318
        cnx.commit()
319
320
        cursor.close()
321
        cnx.close()
322
323
        resp.status = falcon.HTTP_204
324
325
    @staticmethod
326
    @user_logger

myems-api/core/distributionsystem.py 1 location

@@ 199-239 (lines=41) @@
196
197
        resp.text = json.dumps(meta_result)
198
199
    @staticmethod
200
    @user_logger
201
    def on_delete(req, resp, id_):
202
        admin_control(req)
203
        if not id_.isdigit() or int(id_) <= 0:
204
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
205
                                   description='API.INVALID_DISTRIBUTION_SYSTEM_ID')
206
        cnx = mysql.connector.connect(**config.myems_system_db)
207
        cursor = cnx.cursor()
208
209
        cursor.execute(" SELECT name "
210
                       " FROM tbl_distribution_systems "
211
                       " WHERE id = %s ", (id_,))
212
        if cursor.fetchone() is None:
213
            cursor.close()
214
            cnx.close()
215
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
216
                                   description='API.DISTRIBUTION_SYSTEM_NOT_FOUND')
217
218
        # check relation with spaces
219
        cursor.execute(" SELECT id "
220
                       " FROM tbl_spaces_distribution_systems "
221
                       " WHERE distribution_system_id = %s ", (id_,))
222
        rows_spaces = cursor.fetchall()
223
        if rows_spaces is not None and len(rows_spaces) > 0:
224
            cursor.close()
225
            cnx.close()
226
            raise falcon.HTTPError(status=falcon.HTTP_400,
227
                                   title='API.BAD_REQUEST',
228
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
229
230
        cursor.execute(" DELETE FROM tbl_distribution_circuits_points WHERE distribution_circuit_id "
231
                       "IN (SELECT id FROM tbl_distribution_circuits WHERE distribution_system_id = %s) ", (id_,))
232
        cursor.execute(" DELETE FROM tbl_distribution_circuits WHERE distribution_system_id = %s ", (id_,))
233
        cursor.execute(" DELETE FROM tbl_distribution_systems WHERE id = %s ", (id_,))
234
        cnx.commit()
235
236
        cursor.close()
237
        cnx.close()
238
239
        resp.status = falcon.HTTP_204
240
241
    @staticmethod
242
    @user_logger