Code Duplication    Length = 79-85 lines in 2 locations

core/equipment.py 1 location

@@ 200-284 (lines=85) @@
197
198
        resp.body = json.dumps(meta_result)
199
200
    @staticmethod
201
    def on_delete(req, resp, id_):
202
        if not id_.isdigit() or int(id_) <= 0:
203
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
204
                                   description='API.INVALID_EQUIPMENT_ID')
205
206
        cnx = mysql.connector.connect(**config.myems_system_db)
207
        cursor = cnx.cursor()
208
209
        # check relation with space
210
        cursor.execute(" SELECT space_id "
211
                       " FROM tbl_spaces_equipments "
212
                       " WHERE equipment_id = %s ",
213
                       (id_,))
214
        rows_equipments = cursor.fetchall()
215
        if rows_equipments is not None and len(rows_equipments) > 0:
216
            cursor.close()
217
            cnx.disconnect()
218
            raise falcon.HTTPError(falcon.HTTP_400,
219
                                   title='API.BAD_REQUEST',
220
                                   description='API.THERE_IS_RELATION_WITH_SPACE')
221
222
        # check relation with combined equipments
223
        cursor.execute(" SELECT combined_equipment_id "
224
                       " FROM tbl_combined_equipments_equipments "
225
                       " WHERE equipment_id = %s ",
226
                       (id_,))
227
        rows_combined_equipments = cursor.fetchall()
228
        if rows_combined_equipments is not None and len(rows_combined_equipments) > 0:
229
            cursor.close()
230
            cnx.disconnect()
231
            raise falcon.HTTPError(falcon.HTTP_400,
232
                                   title='API.BAD_REQUEST',
233
                                   description='API.THERE_IS_RELATION_WITH_COMBINED_EQUIPMENTS')
234
235
        # check relation with meter
236
        cursor.execute(" SELECT meter_id "
237
                       " FROM tbl_equipments_meters "
238
                       " WHERE equipment_id = %s ",
239
                       (id_,))
240
        rows_meters = cursor.fetchall()
241
        if rows_meters is not None and len(rows_meters) > 0:
242
            cursor.close()
243
            cnx.disconnect()
244
            raise falcon.HTTPError(falcon.HTTP_400,
245
                                   title='API.BAD_REQUEST',
246
                                   description='API.THERE_IS_RELATION_WITH_METER')
247
248
        # check relation with offline meter
249
        cursor.execute(" SELECT offline_meter_id "
250
                       " FROM tbl_equipments_offline_meters "
251
                       " WHERE equipment_id = %s ",
252
                       (id_,))
253
        rows_offline_meters = cursor.fetchall()
254
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
255
            cursor.close()
256
            cnx.disconnect()
257
            raise falcon.HTTPError(falcon.HTTP_400,
258
                                   title='API.BAD_REQUEST',
259
                                   description='API.THERE_IS_RELATION_WITH_OFFLINE_METER')
260
261
        # check relation with virtual meter
262
        cursor.execute(" SELECT virtual_meter_id "
263
                       " FROM tbl_equipments_virtual_meters "
264
                       " WHERE equipment_id = %s ",
265
                       (id_,))
266
        rows_virtual_meters = cursor.fetchall()
267
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
268
            cursor.close()
269
            cnx.disconnect()
270
            raise falcon.HTTPError(falcon.HTTP_400,
271
                                   title='API.BAD_REQUEST',
272
                                   description='API.THERE_IS_RELATION_WITH_VIRTUAL_METER')
273
274
        # delete all associated parameters
275
        cursor.execute(" DELETE FROM tbl_equipments_parameters WHERE equipment_id = %s ", (id_,))
276
        cnx.commit()
277
278
        cursor.execute(" DELETE FROM tbl_equipments WHERE id = %s ", (id_,))
279
        cnx.commit()
280
281
        cursor.close()
282
        cnx.disconnect()
283
284
        resp.status = falcon.HTTP_204
285
286
    @staticmethod
287
    def on_put(req, resp, id_):

core/energycategory.py 1 location

@@ 145-223 (lines=79) @@
142
                  "kgco2e": row[5]}
143
        resp.body = json.dumps(result)
144
145
    @staticmethod
146
    def on_delete(req, resp, id_):
147
        if not id_.isdigit() or int(id_) <= 0:
148
            raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',
149
                                   description='API.INVALID_ENERGY_CATEGORY_ID')
150
151
        cnx = mysql.connector.connect(**config.myems_system_db)
152
        cursor = cnx.cursor()
153
154
        cursor.execute(" SELECT name "
155
                       " FROM tbl_energy_categories "
156
                       " WHERE id = %s ", (id_,))
157
        if cursor.fetchone() is None:
158
            cursor.close()
159
            cnx.disconnect()
160
            raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',
161
                                   description='API.ENERGY_CATEGORY_NOT_FOUND')
162
163
        cursor.execute(" SELECT id "
164
                       " FROM tbl_meters "
165
                       " WHERE energy_category_id = %s ", (id_,))
166
        rows_meters = cursor.fetchall()
167
        if rows_meters is not None and len(rows_meters) > 0:
168
            cursor.close()
169
            cnx.disconnect()
170
            raise falcon.HTTPError(falcon.HTTP_400,
171
                                   title='API.BAD_REQUEST',
172
                                   description='API.ENERGY_CATEGORY_USED_IN_METER')
173
174
        cursor.execute(" SELECT id "
175
                       " FROM tbl_virtual_meters "
176
                       " WHERE energy_category_id = %s ", (id_,))
177
        rows_virtual_meters = cursor.fetchall()
178
        if rows_virtual_meters is not None and len(rows_virtual_meters) > 0:
179
            cursor.close()
180
            cnx.disconnect()
181
            raise falcon.HTTPError(falcon.HTTP_400,
182
                                   title='API.BAD_REQUEST',
183
                                   description='API.ENERGY_CATEGORY_USED_IN_VIRTUAL_METER')
184
185
        cursor.execute(" SELECT id "
186
                       " FROM tbl_offline_meters "
187
                       " WHERE energy_category_id = %s ", (id_,))
188
        rows_offline_meters = cursor.fetchall()
189
        if rows_offline_meters is not None and len(rows_offline_meters) > 0:
190
            cursor.close()
191
            cnx.disconnect()
192
            raise falcon.HTTPError(falcon.HTTP_400,
193
                                   title='API.BAD_REQUEST',
194
                                   description='API.ENERGY_CATEGORY_USED_IN_OFFLINE_METER')
195
196
        cursor.execute(" SELECT id "
197
                       " FROM tbl_tariffs "
198
                       " WHERE energy_category_id = %s ", (id_,))
199
        rows_tariffs = cursor.fetchall()
200
        if rows_tariffs is not None and len(rows_tariffs) > 0:
201
            cursor.close()
202
            cnx.disconnect()
203
            raise falcon.HTTPError(falcon.HTTP_400,
204
                                   title='API.BAD_REQUEST',
205
                                   description='API.ENERGY_CATEGORY_USED_IN_TARIFFS')
206
207
        cursor.execute(" SELECT id "
208
                       " FROM tbl_energy_items "
209
                       " WHERE energy_category_id = %s ", (id_,))
210
        rows_energy_items = cursor.fetchall()
211
        if rows_energy_items is not None and len(rows_energy_items) > 0:
212
            cursor.close()
213
            cnx.disconnect()
214
            raise falcon.HTTPError(falcon.HTTP_400,
215
                                   title='API.BAD_REQUEST',
216
                                   description='API.ENERGY_CATEGORY_USED_IN_ENERGY_ITEMS')
217
218
        cursor.execute(" DELETE FROM tbl_energy_categories WHERE id = %s ", (id_,))
219
        cnx.commit()
220
221
        cursor.close()
222
        cnx.disconnect()
223
        resp.status = falcon.HTTP_204
224
225
    @staticmethod
226
    def on_put(req, resp, id_):