Code Duplication    Length = 29-32 lines in 5 locations

myems-api/core/energystoragepowerstation.py 1 location

@@ 435-464 (lines=30) @@
432
433
        resp.text = json.dumps(meta_result)
434
435
    @staticmethod
436
    @user_logger
437
    def on_delete(req, resp, id_):
438
        admin_control(req)
439
        if not id_.isdigit() or int(id_) <= 0:
440
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
441
                                   description='API.INVALID_ENERGY_STORAGE_POWER_STATION_ID')
442
443
        cnx = mysql.connector.connect(**config.myems_system_db)
444
        cursor = cnx.cursor()
445
446
        cursor.execute(" SELECT name "
447
                       " FROM tbl_energy_storage_power_stations "
448
                       " WHERE id = %s ", (id_,))
449
        if cursor.fetchone() is None:
450
            cursor.close()
451
            cnx.close()
452
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
453
                                   description='API.ENERGY_STORAGE_POWER_STATION_NOT_FOUND')
454
        cursor.execute(" DELETE FROM tbl_energy_storage_power_stations_containers "
455
                       " WHERE energy_storage_power_station_id = %s ", (id_,))
456
457
        cursor.execute(" DELETE FROM tbl_energy_storage_power_stations "
458
                       " WHERE id = %s ", (id_,))
459
        cnx.commit()
460
461
        cursor.close()
462
        cnx.close()
463
464
        resp.status = falcon.HTTP_204
465
466
    @staticmethod
467
    @user_logger

myems-api/core/virtualpowerplant.py 1 location

@@ 281-309 (lines=29) @@
278
279
        resp.text = json.dumps(meta_result)
280
281
    @staticmethod
282
    @user_logger
283
    def on_delete(req, resp, id_):
284
        admin_control(req)
285
        if not id_.isdigit() or int(id_) <= 0:
286
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
287
                                   description='API.INVALID_VIRTUAL_POWER_PLANT_ID')
288
289
        cnx = mysql.connector.connect(**config.myems_system_db)
290
        cursor = cnx.cursor()
291
292
        cursor.execute(" SELECT name "
293
                       " FROM tbl_virtual_power_plants "
294
                       " WHERE id = %s ", (id_,))
295
        if cursor.fetchone() is None:
296
            cursor.close()
297
            cnx.close()
298
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
299
                                   description='API.VIRTUAL_POWER_PLANT_NOT_FOUND')
300
301
        cursor.execute(" DELETE FROM tbl_virtual_power_plants_microgrids WHERE virtual_power_plant_id = %s ", (id_,))
302
        cnx.commit()
303
        cursor.execute(" DELETE FROM tbl_virtual_power_plants WHERE id = %s ", (id_,))
304
        cnx.commit()
305
306
        cursor.close()
307
        cnx.close()
308
309
        resp.status = falcon.HTTP_204
310
311
    @staticmethod
312
    @user_logger

myems-api/core/controlmode.py 1 location

@@ 209-240 (lines=32) @@
206
207
        resp.text = json.dumps(result)
208
209
    @staticmethod
210
    @user_logger
211
    def on_delete(req, resp, id_):
212
        """Handles DELETE requests"""
213
        admin_control(req)
214
        if not id_.isdigit() or int(id_) <= 0:
215
            raise falcon.HTTPError(status=falcon.HTTP_400,
216
                                   title='API.BAD_REQUEST',
217
                                   description='API.INVALID_CONTROL_MODE_ID')
218
219
        cnx = mysql.connector.connect(**config.myems_system_db)
220
        cursor = cnx.cursor()
221
222
        cursor.execute(" SELECT name "
223
                       " FROM tbl_control_modes "
224
                       " WHERE id = %s ", (id_,))
225
        if cursor.fetchone() is None:
226
            cursor.close()
227
            cnx.close()
228
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
229
                                   description='API.CONTROL_MODE_NOT_FOUND')
230
231
        cursor.execute(" DELETE FROM tbl_control_modes_times WHERE control_mode_id = %s ", (id_,))
232
        cnx.commit()
233
234
        cursor.execute(" DELETE FROM tbl_control_modes WHERE id = %s ", (id_,))
235
        cnx.commit()
236
237
        cursor.close()
238
        cnx.close()
239
240
        resp.status = falcon.HTTP_204
241
242
    @staticmethod
243
    @user_logger

myems-api/core/distributioncircuit.py 1 location

@@ 246-276 (lines=31) @@
243
244
        resp.text = json.dumps(meta_result)
245
246
    @staticmethod
247
    @user_logger
248
    def on_delete(req, resp, id_):
249
        admin_control(req)
250
        if not id_.isdigit() or int(id_) <= 0:
251
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
252
                                   description='API.INVALID_DISTRIBUTION_CIRCUIT_ID')
253
        cnx = mysql.connector.connect(**config.myems_system_db)
254
        cursor = cnx.cursor()
255
256
        cursor.execute(" SELECT name "
257
                       " FROM tbl_distribution_circuits "
258
                       " WHERE id = %s ", (id_,))
259
        if cursor.fetchone() is None:
260
            cursor.close()
261
            cnx.close()
262
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
263
                                   description='API.DISTRIBUTION_CIRCUIT_NOT_FOUND')
264
265
        # delete relation with points
266
        cursor.execute(" DELETE FROM tbl_distribution_circuits_points "
267
                       " WHERE distribution_circuit_id = %s ", (id_,))
268
        # delete distribution circuit itself
269
        cursor.execute(" DELETE FROM tbl_distribution_circuits "
270
                       " WHERE id = %s ", (id_,))
271
        cnx.commit()
272
273
        cursor.close()
274
        cnx.close()
275
276
        resp.status = falcon.HTTP_204
277
278
    @staticmethod
279
    @user_logger

myems-api/core/distributionsystem.py 1 location

@@ 198-226 (lines=29) @@
195
196
        resp.text = json.dumps(meta_result)
197
198
    @staticmethod
199
    @user_logger
200
    def on_delete(req, resp, id_):
201
        admin_control(req)
202
        if not id_.isdigit() or int(id_) <= 0:
203
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
204
                                   description='API.INVALID_DISTRIBUTION_SYSTEM_ID')
205
        cnx = mysql.connector.connect(**config.myems_system_db)
206
        cursor = cnx.cursor()
207
208
        cursor.execute(" SELECT name "
209
                       " FROM tbl_distribution_systems "
210
                       " WHERE id = %s ", (id_,))
211
        if cursor.fetchone() is None:
212
            cursor.close()
213
            cnx.close()
214
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
215
                                   description='API.DISTRIBUTION_SYSTEM_NOT_FOUND')
216
217
        cursor.execute(" DELETE FROM tbl_distribution_circuits_points WHERE distribution_circuit_id "
218
                       "IN (SELECT id FROM tbl_distribution_circuits WHERE distribution_system_id = %s) ", (id_,))
219
        cursor.execute(" DELETE FROM tbl_distribution_circuits WHERE distribution_system_id = %s ", (id_,))
220
        cursor.execute(" DELETE FROM tbl_distribution_systems WHERE id = %s ", (id_,))
221
        cnx.commit()
222
223
        cursor.close()
224
        cnx.close()
225
226
        resp.status = falcon.HTTP_204
227
228
    @staticmethod
229
    @user_logger