| 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 |
||
| 196 | @staticmethod |
||
| 197 | @user_logger |
||
| 198 | def on_delete(req, resp, id_): |
||
| 199 | admin_control(req) |
||
| 200 | if not id_.isdigit() or int(id_) <= 0: |
||
| 201 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 202 | description='API.INVALID_CONTACT_ID') |
||
| 203 | |||
| 204 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 205 | cursor = cnx.cursor() |
||
| 206 | |||
| 207 | cursor.execute(" SELECT name " |
||
| 208 | " FROM tbl_contacts " |
||
| 209 | " WHERE id = %s ", (id_,)) |
||
| 210 | if cursor.fetchone() is None: |
||
| 211 | cursor.close() |
||
| 212 | cnx.close() |
||
| 213 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 214 | description='API.CONTACT_NOT_FOUND') |
||
| 215 | |||
| 216 | # check relation with shopfloors |
||
| 217 | cursor.execute(" SELECT id " |
||
| 218 | " FROM tbl_shopfloors " |
||
| 219 | " WHERE contact_id = %s ", (id_,)) |
||
| 220 | rows_shopfloors = cursor.fetchall() |
||
| 221 | if rows_shopfloors is not None and len(rows_shopfloors) > 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_SHOPFLOORS') |
||
| 227 | |||
| 228 | # check relation with spaces |
||
| 229 | cursor.execute(" SELECT id " |
||
| 230 | " FROM tbl_spaces " |
||
| 231 | " WHERE contact_id = %s ", (id_,)) |
||
| 232 | rows_spaces = cursor.fetchall() |
||
| 233 | if rows_spaces is not None and len(rows_spaces) > 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_SPACES') |
||
| 239 | |||
| 240 | # check relation with stores |
||
| 241 | cursor.execute(" SELECT id " |
||
| 242 | " FROM tbl_stores " |
||
| 243 | " WHERE contact_id = %s ", (id_,)) |
||
| 244 | rows_stores = cursor.fetchall() |
||
| 245 | if rows_stores is not None and len(rows_stores) > 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_STORES') |
||
| 251 | |||
| 252 | # check relation with tenants |
||
| 253 | cursor.execute(" SELECT id " |
||
| 254 | " FROM tbl_tenants " |
||
| 255 | " WHERE contact_id = %s ", (id_,)) |
||
| 256 | rows_tenants = cursor.fetchall() |
||
| 257 | if rows_tenants is not None and len(rows_tenants) > 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_TENANTS') |
||
| 263 | |||
| 264 | # check relation with charging_stations |
||
| 265 | cursor.execute(" SELECT id " |
||
| 266 | " FROM tbl_charging_stations " |
||
| 267 | " WHERE contact_id = %s ", (id_,)) |
||
| 268 | rows_charging_stations = cursor.fetchall() |
||
| 269 | if rows_charging_stations is not None and len(rows_charging_stations) > 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_CHARGING_STATIONS') |
||
| 275 | |||
| 276 | # check relation with energy_storage_containers |
||
| 277 | cursor.execute(" SELECT id " |
||
| 278 | " FROM tbl_energy_storage_containers " |
||
| 279 | " WHERE contact_id = %s ", (id_,)) |
||
| 280 | rows_energy_storage_containers = cursor.fetchall() |
||
| 281 | if rows_energy_storage_containers is not None and len(rows_energy_storage_containers) > 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_ENERGY_STORAGE_CONTAINERS') |
||
| 287 | |||
| 288 | |||
| 289 | # check relation with energy_storage_power_stations |
||
| 290 | cursor.execute(" SELECT id " |
||
| 291 | " FROM tbl_energy_storage_power_stations " |
||
| 292 | " WHERE contact_id = %s ", (id_,)) |
||
| 293 | rows_energy_storage_power_stations = cursor.fetchall() |
||
| 294 | if rows_energy_storage_power_stations is not None and len(rows_energy_storage_power_stations) > 0: |
||
| 295 | cursor.close() |
||
| 296 | cnx.close() |
||
| 297 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 298 | title='API.BAD_REQUEST', |
||
| 299 | description='API.THERE_IS_RELATION_WITH_ENERGY_STORAGE_POWER_STATIONS') |
||
| 300 | |||
| 301 | # check relation with microgrids |
||
| 302 | cursor.execute(" SELECT id " |
||
| 303 | " FROM tbl_microgrids " |
||
| 304 | " WHERE contact_id = %s ", (id_,)) |
||
| 305 | rows_microgrids = cursor.fetchall() |
||
| 306 | if rows_microgrids is not None and len(rows_microgrids) > 0: |
||
| 307 | cursor.close() |
||
| 308 | cnx.close() |
||
| 309 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 310 | title='API.BAD_REQUEST', |
||
| 311 | description='API.THERE_IS_RELATION_WITH_MICROGRIDS') |
||
| 312 | |||
| 313 | # check relation with photovoltaic_power_stations |
||
| 314 | cursor.execute(" SELECT id " |
||
| 315 | " FROM tbl_photovoltaic_power_stations " |
||
| 316 | " WHERE contact_id = %s ", (id_,)) |
||
| 317 | rows_photovoltaic_power_stations = cursor.fetchall() |
||
| 318 | if rows_photovoltaic_power_stations is not None and len(rows_photovoltaic_power_stations) > 0: |
||
| 319 | cursor.close() |
||
| 320 | cnx.close() |
||
| 321 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 322 | title='API.BAD_REQUEST', |
||
| 323 | description='API.THERE_IS_RELATION_WITH_PHOTOVOLTAIC_POWER_STATIONS') |
||
| 324 | |||
| 325 | #check relation with wind_farms |
||
| 326 | cursor.execute(" SELECT id " |
||
| 327 | " FROM tbl_wind_farms " |
||
| 328 | " WHERE contact_id = %s ", (id_,)) |
||
| 329 | rows_wind_farms = cursor.fetchall() |
||
| 330 | if rows_wind_farms is not None and len(rows_wind_farms) > 0: |
||
| 331 | cursor.close() |
||
| 332 | cnx.close() |
||
| 333 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 334 | title='API.BAD_REQUEST', |
||
| 335 | description='API.THERE_IS_RELATION_WITH_WIND_FARMS') |
||
| 336 | |||
| 337 | |||
| 338 | cursor.execute(" DELETE FROM tbl_contacts WHERE id = %s ", (id_,)) |
||
| 339 | cnx.commit() |
||
| 340 | |||
| 341 | cursor.close() |
||
| 342 | cnx.close() |
||
| 343 | |||
| 344 | resp.status = falcon.HTTP_204 |
||
| 345 | |||
| 435 |