| Conditions | 19 |
| Total Lines | 83 |
| Code Lines | 63 |
| 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 contact.ContactItem.on_put() 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 falcon |
||
| 230 | @staticmethod |
||
| 231 | def on_put(req, resp, id_): |
||
| 232 | """Handles PUT requests""" |
||
| 233 | try: |
||
| 234 | raw_json = req.stream.read().decode('utf-8') |
||
| 235 | except Exception as ex: |
||
| 236 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 237 | |||
| 238 | if not id_.isdigit() or int(id_) <= 0: |
||
| 239 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 240 | description='API.INVALID_') |
||
| 241 | |||
| 242 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 243 | |||
| 244 | if 'name' not in new_values['data'].keys() or \ |
||
| 245 | not isinstance(new_values['data']['name'], str) or \ |
||
| 246 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 247 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 248 | description='API.INVALID_CONTACT_NAME') |
||
| 249 | name = str.strip(new_values['data']['name']) |
||
| 250 | |||
| 251 | if 'email' not in new_values['data'].keys() or \ |
||
| 252 | not isinstance(new_values['data']['email'], str) or \ |
||
| 253 | len(str.strip(new_values['data']['email'])) == 0: |
||
| 254 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 255 | description='API.INVALID_EMAIL') |
||
| 256 | email = str.strip(new_values['data']['email']) |
||
| 257 | |||
| 258 | match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', email) |
||
| 259 | if match is None: |
||
| 260 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 261 | description='API.INVALID_EMAIL') |
||
| 262 | |||
| 263 | if 'phone' not in new_values['data'].keys() or \ |
||
| 264 | not isinstance(new_values['data']['phone'], str) or \ |
||
| 265 | len(str.strip(new_values['data']['phone'])) == 0: |
||
| 266 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 267 | description='API.INVALID_USER_PHONE') |
||
| 268 | phone = str.strip(new_values['data']['phone']) |
||
| 269 | |||
| 270 | if 'description' in new_values['data'].keys() and \ |
||
| 271 | new_values['data']['description'] is not None and \ |
||
| 272 | len(str(new_values['data']['description'])) > 0: |
||
| 273 | description = str.strip(new_values['data']['description']) |
||
| 274 | else: |
||
| 275 | description = None |
||
| 276 | |||
| 277 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
| 278 | cursor = cnx.cursor() |
||
| 279 | |||
| 280 | cursor.execute(" SELECT name " |
||
| 281 | " FROM tbl_contacts " |
||
| 282 | " WHERE id = %s ", (id_,)) |
||
| 283 | if cursor.fetchone() is None: |
||
| 284 | cursor.close() |
||
| 285 | cnx.disconnect() |
||
| 286 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 287 | description='API.CONTACT_NOT_FOUND') |
||
| 288 | |||
| 289 | cursor.execute(" SELECT name " |
||
| 290 | " FROM tbl_contacts " |
||
| 291 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 292 | if cursor.fetchone() is not None: |
||
| 293 | cursor.close() |
||
| 294 | cnx.disconnect() |
||
| 295 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 296 | description='API.CONTACT_NAME_IS_ALREADY_IN_USE') |
||
| 297 | |||
| 298 | update_row = (" UPDATE tbl_contacts " |
||
| 299 | " SET name = %s, email = %s, " |
||
| 300 | " phone = %s, description = %s " |
||
| 301 | " WHERE id = %s ") |
||
| 302 | cursor.execute(update_row, (name, |
||
| 303 | email, |
||
| 304 | phone, |
||
| 305 | description, |
||
| 306 | id_,)) |
||
| 307 | cnx.commit() |
||
| 308 | |||
| 309 | cursor.close() |
||
| 310 | cnx.disconnect() |
||
| 311 | |||
| 312 | resp.status = falcon.HTTP_200 |
||
| 313 | |||
| 314 |