Conditions | 24 |
Total Lines | 149 |
Code Lines | 111 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like core.contact.ContactItem.on_delete() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import re |
||
177 | @staticmethod |
||
178 | @user_logger |
||
179 | def on_delete(req, resp, id_): |
||
180 | admin_control(req) |
||
181 | if not id_.isdigit() or int(id_) <= 0: |
||
182 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
183 | description='API.INVALID_CONTACT_ID') |
||
184 | |||
185 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
186 | cursor = cnx.cursor() |
||
187 | |||
188 | cursor.execute(" SELECT name " |
||
189 | " FROM tbl_contacts " |
||
190 | " WHERE id = %s ", (id_,)) |
||
191 | if cursor.fetchone() is None: |
||
192 | cursor.close() |
||
193 | cnx.close() |
||
194 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
195 | description='API.CONTACT_NOT_FOUND') |
||
196 | |||
197 | # check relation with shopfloors |
||
198 | cursor.execute(" SELECT id " |
||
199 | " FROM tbl_shopfloors " |
||
200 | " WHERE contact_id = %s ", (id_,)) |
||
201 | rows_shopfloors = cursor.fetchall() |
||
202 | if rows_shopfloors is not None and len(rows_shopfloors) > 0: |
||
203 | cursor.close() |
||
204 | cnx.close() |
||
205 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
206 | title='API.BAD_REQUEST', |
||
207 | description='API.THERE_IS_RELATION_WITH_SHOPFLOORS') |
||
208 | |||
209 | # check relation with spaces |
||
210 | cursor.execute(" SELECT id " |
||
211 | " FROM tbl_spaces " |
||
212 | " WHERE contact_id = %s ", (id_,)) |
||
213 | rows_spaces = cursor.fetchall() |
||
214 | if rows_spaces is not None and len(rows_spaces) > 0: |
||
215 | cursor.close() |
||
216 | cnx.close() |
||
217 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
218 | title='API.BAD_REQUEST', |
||
219 | description='API.THERE_IS_RELATION_WITH_SPACES') |
||
220 | |||
221 | # check relation with stores |
||
222 | cursor.execute(" SELECT id " |
||
223 | " FROM tbl_stores " |
||
224 | " WHERE contact_id = %s ", (id_,)) |
||
225 | rows_stores = cursor.fetchall() |
||
226 | if rows_stores is not None and len(rows_stores) > 0: |
||
227 | cursor.close() |
||
228 | cnx.close() |
||
229 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
230 | title='API.BAD_REQUEST', |
||
231 | description='API.THERE_IS_RELATION_WITH_STORES') |
||
232 | |||
233 | # check relation with tenants |
||
234 | cursor.execute(" SELECT id " |
||
235 | " FROM tbl_tenants " |
||
236 | " WHERE contact_id = %s ", (id_,)) |
||
237 | rows_tenants = cursor.fetchall() |
||
238 | if rows_tenants is not None and len(rows_tenants) > 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_TENANTS') |
||
244 | |||
245 | # check relation with charging_stations |
||
246 | cursor.execute(" SELECT id " |
||
247 | " FROM tbl_charging_stations " |
||
248 | " WHERE contact_id = %s ", (id_,)) |
||
249 | rows_charging_stations = cursor.fetchall() |
||
250 | if rows_charging_stations is not None and len(rows_charging_stations) > 0: |
||
251 | cursor.close() |
||
252 | cnx.close() |
||
253 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
254 | title='API.BAD_REQUEST', |
||
255 | description='API.THERE_IS_RELATION_WITH_CHARGING_STATIONS') |
||
256 | |||
257 | # check relation with energy_storage_containers |
||
258 | cursor.execute(" SELECT id " |
||
259 | " FROM tbl_energy_storage_containers " |
||
260 | " WHERE contact_id = %s ", (id_,)) |
||
261 | rows_energy_storage_containers = cursor.fetchall() |
||
262 | if rows_energy_storage_containers is not None and len(rows_energy_storage_containers) > 0: |
||
263 | cursor.close() |
||
264 | cnx.close() |
||
265 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
266 | title='API.BAD_REQUEST', |
||
267 | description='API.THERE_IS_RELATION_WITH_ENERGY_STORAGE_CONTAINERS') |
||
268 | |||
269 | |||
270 | # check relation with energy_storage_power_stations |
||
271 | cursor.execute(" SELECT id " |
||
272 | " FROM tbl_energy_storage_power_stations " |
||
273 | " WHERE contact_id = %s ", (id_,)) |
||
274 | rows_energy_storage_power_stations = cursor.fetchall() |
||
275 | if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
||
276 | cursor.close() |
||
277 | cnx.close() |
||
278 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
279 | title='API.BAD_REQUEST', |
||
280 | description='API.THERE_IS_RELATION_WITH_ENERGY_STORAGE_POWER_STATIONS') |
||
281 | |||
282 | # check relation with microgrids |
||
283 | cursor.execute(" SELECT id " |
||
284 | " FROM tbl_microgrids " |
||
285 | " WHERE contact_id = %s ", (id_,)) |
||
286 | rows_microgrids = cursor.fetchall() |
||
287 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
288 | cursor.close() |
||
289 | cnx.close() |
||
290 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
291 | title='API.BAD_REQUEST', |
||
292 | description='API.THERE_IS_RELATION_WITH_MICROGRIDS') |
||
293 | |||
294 | # check relation with photovoltaic_power_stations |
||
295 | cursor.execute(" SELECT id " |
||
296 | " FROM tbl_photovoltaic_power_stations " |
||
297 | " WHERE contact_id = %s ", (id_,)) |
||
298 | rows_photovoltaic_power_stations = cursor.fetchall() |
||
299 | if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0: |
||
300 | cursor.close() |
||
301 | cnx.close() |
||
302 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
303 | title='API.BAD_REQUEST', |
||
304 | description='API.THERE_IS_RELATION_WITH_PHOTOVOLTAIC_POWER_STATIONS') |
||
305 | |||
306 | #check relation with wind_farms |
||
307 | cursor.execute(" SELECT id " |
||
308 | " FROM tbl_wind_farms " |
||
309 | " WHERE contact_id = %s ", (id_,)) |
||
310 | rows_wind_farms = cursor.fetchall() |
||
311 | if rows_wind_farms is not None and len(rows_wind_farms) > 0: |
||
312 | cursor.close() |
||
313 | cnx.close() |
||
314 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
315 | title='API.BAD_REQUEST', |
||
316 | description='API.THERE_IS_RELATION_WITH_WIND_FARMS') |
||
317 | |||
318 | |||
319 | cursor.execute(" DELETE FROM tbl_contacts WHERE id = %s ", (id_,)) |
||
320 | cnx.commit() |
||
321 | |||
322 | cursor.close() |
||
323 | cnx.close() |
||
324 | |||
325 | resp.status = falcon.HTTP_204 |
||
326 | |||
416 |