Code Duplication    Length = 138-146 lines in 2 locations

myems-api/core/command.py 1 location

@@ 186-331 (lines=146) @@
183
                  "description": row[6]}
184
        resp.text = json.dumps(result)
185
186
    @staticmethod
187
    @user_logger
188
    def on_delete(req, resp, id_):
189
        admin_control(req)
190
        if not id_.isdigit() or int(id_) <= 0:
191
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
192
                                   description='API.INVALID_COMMAND_ID')
193
194
        cnx = mysql.connector.connect(**config.myems_system_db)
195
        cursor = cnx.cursor()
196
197
        cursor.execute(" SELECT name "
198
                       " FROM tbl_commands "
199
                       " WHERE id = %s ", (id_,))
200
        if cursor.fetchone() is None:
201
            cursor.close()
202
            cnx.close()
203
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
204
                                   description='API.COMMAND_NOT_FOUND')
205
206
        # check relation with meter
207
        cursor.execute(" SELECT meter_id "
208
                       " FROM tbl_meters_commands "
209
                       " WHERE command_id = %s ",
210
                       (id_,))
211
        rows = cursor.fetchall()
212
        if rows is not None and len(rows) > 0:
213
            cursor.close()
214
            cnx.close()
215
            raise falcon.HTTPError(status=falcon.HTTP_400,
216
                                   title='API.BAD_REQUEST',
217
                                   description='API.THERE_IS_RELATION_WITH_METERS')
218
219
        # check relation with space
220
        cursor.execute(" SELECT space_id "
221
                       " FROM tbl_spaces_commands "
222
                       " WHERE command_id = %s ",
223
                       (id_,))
224
        rows_spaces = cursor.fetchall()
225
        if rows_spaces is not None and len(rows_spaces) > 0:
226
            cursor.close()
227
            cnx.close()
228
            raise falcon.HTTPError(status=falcon.HTTP_400,
229
                                   title='API.BAD_REQUEST',
230
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
231
232
        # check relation with equipment
233
        cursor.execute(" SELECT equipment_id "
234
                       " FROM tbl_equipments_commands "
235
                       " WHERE command_id = %s ",
236
                       (id_,))
237
        rows = cursor.fetchall()
238
        if rows is not None and len(rows) > 0:
239
            cursor.close()
240
            cnx.close()
241
            raise falcon.HTTPError(status=falcon.HTTP_400,
242
                                   title='API.BAD_REQUEST',
243
                                   description='API.THERE_IS_RELATION_WITH_EQUIPMENTS')
244
245
        # check relation with combined equipment
246
        cursor.execute(" SELECT combined_equipment_id "
247
                       " FROM tbl_combined_equipments_commands "
248
                       " WHERE command_id = %s ",
249
                       (id_,))
250
        rows = cursor.fetchall()
251
        if rows is not None and len(rows) > 0:
252
            cursor.close()
253
            cnx.close()
254
            raise falcon.HTTPError(status=falcon.HTTP_400,
255
                                   title='API.BAD_REQUEST',
256
                                   description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS')
257
258
        # check relation with tenant
259
        cursor.execute(" SELECT tenant_id "
260
                       " FROM tbl_tenants_commands "
261
                       " WHERE command_id = %s ",
262
                       (id_,))
263
        rows = cursor.fetchall()
264
        if rows is not None and len(rows) > 0:
265
            cursor.close()
266
            cnx.close()
267
            raise falcon.HTTPError(status=falcon.HTTP_400,
268
                                   title='API.BAD_REQUEST',
269
                                   description='API.THERE_IS_RELATION_WITH_TENANTS')
270
271
        # check relation with store
272
        cursor.execute(" SELECT store_id "
273
                       " FROM tbl_stores_commands "
274
                       " WHERE command_id = %s ",
275
                       (id_,))
276
        rows = cursor.fetchall()
277
        if rows is not None and len(rows) > 0:
278
            cursor.close()
279
            cnx.close()
280
            raise falcon.HTTPError(status=falcon.HTTP_400,
281
                                   title='API.BAD_REQUEST',
282
                                   description='API.THERE_IS_RELATION_WITH_STORES')
283
284
        # check relation with shopfloor
285
        cursor.execute(" SELECT shopfloor_id "
286
                       " FROM tbl_shopfloors_commands "
287
                       " WHERE command_id = %s ",
288
                       (id_,))
289
        rows = cursor.fetchall()
290
        if rows is not None and len(rows) > 0:
291
            cursor.close()
292
            cnx.close()
293
            raise falcon.HTTPError(status=falcon.HTTP_400,
294
                                   title='API.BAD_REQUEST',
295
                                   description='API.THERE_IS_RELATION_WITH_SHOPFLOORS')
296
297
        # check relation with energy storage container
298
        cursor.execute(" SELECT energy_storage_container_id "
299
                       " FROM tbl_energy_storage_containers_commands "
300
                       " WHERE command_id = %s ",
301
                       (id_,))
302
        rows = cursor.fetchall()
303
        if rows is not None and len(rows) > 0:
304
            cursor.close()
305
            cnx.close()
306
            raise falcon.HTTPError(status=falcon.HTTP_400,
307
                                   title='API.BAD_REQUEST',
308
                                   description='API.THERE_IS_RELATION_WITH_ENERGY_STORAGE_CONTAINERS')
309
310
        # check relation with microgrid
311
        cursor.execute(" SELECT microgrid_id "
312
                       " FROM tbl_microgrids_commands "
313
                       " WHERE command_id = %s ",
314
                       (id_,))
315
        rows = cursor.fetchall()
316
        if rows is not None and len(rows) > 0:
317
            cursor.close()
318
            cnx.close()
319
            raise falcon.HTTPError(status=falcon.HTTP_400,
320
                                   title='API.BAD_REQUEST',
321
                                   description='API.THERE_IS_RELATION_WITH_MICROGRIDS')
322
323
        # todo: check relation with points
324
325
        cursor.execute(" DELETE FROM tbl_commands WHERE id = %s ", (id_,))
326
        cnx.commit()
327
328
        cursor.close()
329
        cnx.close()
330
331
        resp.status = falcon.HTTP_204
332
333
    @staticmethod
334
    @user_logger

myems-api/core/costcenter.py 1 location

@@ 159-296 (lines=138) @@
156
                  "external_id": row[3]}
157
        resp.text = json.dumps(result)
158
159
    @staticmethod
160
    @user_logger
161
    def on_delete(req, resp, id_):
162
        """Handles DELETE requests"""
163
        admin_control(req)
164
        if not id_.isdigit() or int(id_) <= 0:
165
            raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
166
                                   description='API.INVALID_COST_CENTER_ID')
167
168
        cnx = mysql.connector.connect(**config.myems_system_db)
169
        cursor = cnx.cursor()
170
171
        cursor.execute(" SELECT name "
172
                       " FROM tbl_cost_centers "
173
                       " WHERE id = %s ", (id_,))
174
        if cursor.fetchone() is None:
175
            cursor.close()
176
            cnx.close()
177
            raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
178
                                   description='API.COST_CENTER_NOT_FOUND')
179
180
        # check relation with equipments
181
        cursor.execute(" SELECT id "
182
                       " FROM tbl_equipments "
183
                       " WHERE cost_center_id = %s ", (id_,))
184
        rows_equipments = cursor.fetchall()
185
        if rows_equipments is not None and len(rows_equipments) > 0:
186
            cursor.close()
187
            cnx.close()
188
            raise falcon.HTTPError(status=falcon.HTTP_400,
189
                                   title='API.BAD_REQUEST',
190
                                   description='API.THERE_IS_RELATION_WITH_EQUIPMENTS')
191
192
        # check relation with combined equipments
193
        cursor.execute(" SELECT id "
194
                       " FROM tbl_combined_equipments "
195
                       " WHERE cost_center_id = %s ", (id_,))
196
        rows_combined_equipments = cursor.fetchall()
197
        if rows_combined_equipments is not None and len(rows_combined_equipments) > 0:
198
            cursor.close()
199
            cnx.close()
200
            raise falcon.HTTPError(status=falcon.HTTP_400,
201
                                   title='API.BAD_REQUEST',
202
                                   description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS')
203
204
        # check relation with meters
205
        cursor.execute(" SELECT id "
206
                       " FROM tbl_meters "
207
                       " WHERE cost_center_id = %s ", (id_,))
208
        rows_meters = cursor.fetchall()
209
        if rows_meters is not None and len(rows_meters) > 0:
210
            cursor.close()
211
            cnx.close()
212
            raise falcon.HTTPError(status=falcon.HTTP_400,
213
                                   title='API.BAD_REQUEST',
214
                                   description='API.THERE_IS_RELATION_WITH_METERS')
215
216
        # check relation with offline meters
217
        cursor.execute(" SELECT id "
218
                       " FROM tbl_offline_meters "
219
                       " WHERE cost_center_id = %s ", (id_,))
220
        rows_offline_meters = cursor.fetchall()
221
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
222
            cursor.close()
223
            cnx.close()
224
            raise falcon.HTTPError(status=falcon.HTTP_400,
225
                                   title='API.BAD_REQUEST',
226
                                   description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS')
227
228
        # check relation with virtual meters
229
        cursor.execute(" SELECT id "
230
                       " FROM tbl_virtual_meters "
231
                       " WHERE cost_center_id = %s ", (id_,))
232
        rows_virtual_meters = cursor.fetchall()
233
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
234
            cursor.close()
235
            cnx.close()
236
            raise falcon.HTTPError(status=falcon.HTTP_400,
237
                                   title='API.BAD_REQUEST',
238
                                   description='API.THERE_IS_RELATION_WITH_OFFLINE_METERS')
239
240
        # check relation with tenants
241
        cursor.execute(" SELECT id "
242
                       " FROM tbl_tenants "
243
                       " WHERE cost_center_id = %s ", (id_,))
244
        rows_tenants = cursor.fetchall()
245
        if rows_tenants is not None and len(rows_tenants) > 0:
246
            cursor.close()
247
            cnx.close()
248
            raise falcon.HTTPError(status=falcon.HTTP_400,
249
                                   title='API.BAD_REQUEST',
250
                                   description='API.THERE_IS_RELATION_WITH_TENANTS')
251
252
        # check relation with stores
253
        cursor.execute(" SELECT id "
254
                       " FROM tbl_stores "
255
                       " WHERE cost_center_id = %s ", (id_,))
256
        rows_stores = cursor.fetchall()
257
        if rows_stores is not None and len(rows_stores) > 0:
258
            cursor.close()
259
            cnx.close()
260
            raise falcon.HTTPError(status=falcon.HTTP_400,
261
                                   title='API.BAD_REQUEST',
262
                                   description='API.THERE_IS_RELATION_WITH_STORES')
263
264
        # check relation with spaces
265
        cursor.execute(" SELECT id "
266
                       " FROM tbl_spaces "
267
                       " WHERE cost_center_id = %s ", (id_,))
268
        rows_factories = cursor.fetchall()
269
        if rows_factories is not None and len(rows_factories) > 0:
270
            cursor.close()
271
            cnx.close()
272
            raise falcon.HTTPError(status=falcon.HTTP_400,
273
                                   title='API.BAD_REQUEST',
274
                                   description='API.THERE_IS_RELATION_WITH_SPACES')
275
276
        # check relation with shopfloors
277
        cursor.execute(" SELECT id "
278
                       " FROM tbl_shopfloors "
279
                       " WHERE cost_center_id = %s ", (id_,))
280
        rows_shopfloors = cursor.fetchall()
281
        if rows_shopfloors is not None and len(rows_shopfloors) > 0:
282
            cursor.close()
283
            cnx.close()
284
            raise falcon.HTTPError(status=falcon.HTTP_400,
285
                                   title='API.BAD_REQUEST',
286
                                   description='API.THERE_IS_RELATION_WITH_SHOPFLOORS')
287
288
        # delete relation with tariffs
289
        cursor.execute(" DELETE FROM tbl_cost_centers_tariffs WHERE cost_center_id = %s ", (id_,))
290
291
        cursor.execute(" DELETE FROM tbl_cost_centers WHERE id = %s ", (id_,))
292
        cnx.commit()
293
294
        cursor.close()
295
        cnx.close()
296
        resp.status = falcon.HTTP_204
297
298
    @staticmethod
299
    @user_logger