| Conditions | 38 |
| Total Lines | 166 |
| Code Lines | 131 |
| Lines | 35 |
| Ratio | 21.08 % |
| 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.emailmessage.EmailMessageItem.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 |
||
| 292 | @staticmethod |
||
| 293 | @user_logger |
||
| 294 | def on_put(req, resp, id_): |
||
| 295 | """Handles PUT requests""" |
||
| 296 | access_control(req) |
||
| 297 | |||
| 298 | if not id_.isdigit() or int(id_) <= 0: |
||
| 299 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 300 | description='API.INVALID_EMAIL_MESSAGE_ID') |
||
| 301 | try: |
||
| 302 | upload = req.get_param('file') |
||
| 303 | # Read upload file as binary |
||
| 304 | attachment_file_object = upload.file.read() |
||
| 305 | # Retrieve filename |
||
| 306 | attachment_file_name = upload.filename |
||
| 307 | except Exception as ex: |
||
| 308 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 309 | description='API.FAILED_TO_UPLOAD_ATTACHMENT_FILE') |
||
| 310 | |||
| 311 | try: |
||
| 312 | raw_json = req.get_param('req') |
||
| 313 | except Exception as ex: |
||
| 314 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 315 | |||
| 316 | new_values = json.loads(raw_json) |
||
| 317 | |||
| 318 | if 'rule_id' in new_values['data'].keys(): |
||
| 319 | if new_values['data']['rule_id'] <= 0: |
||
| 320 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 321 | description='API.INVALID_RULE_ID') |
||
| 322 | rule_id = new_values['data']['rule_id'] |
||
| 323 | else: |
||
| 324 | rule_id = None |
||
| 325 | |||
| 326 | if 'recipient_name' not in new_values['data'].keys() or \ |
||
| 327 | not isinstance(new_values['data']['recipient_name'], str) or \ |
||
| 328 | len(str.strip(new_values['data']['recipient_name'])) == 0: |
||
| 329 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 330 | description='API.INVALID_RECIPIENT_NAME') |
||
| 331 | recipient_name = str.strip(new_values['data']['recipient_name']) |
||
| 332 | |||
| 333 | if 'recipient_email' not in new_values['data'].keys() or \ |
||
| 334 | not isinstance(new_values['data']['recipient_email'], str) or \ |
||
| 335 | len(str.strip(new_values['data']['recipient_email'])) == 0: |
||
| 336 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 337 | description='API.INVALID_RECIPIENT_EMAIL') |
||
| 338 | recipient_email = str.strip(new_values['data']['recipient_email']) |
||
| 339 | match = re.match(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', recipient_email) |
||
| 340 | if match is None: |
||
| 341 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 342 | description='API.INVALID_EMAIL') |
||
| 343 | |||
| 344 | if 'subject' not in new_values['data'].keys() or \ |
||
| 345 | not isinstance(new_values['data']['subject'], str) or \ |
||
| 346 | len(str.strip(new_values['data']['subject'])) == 0: |
||
| 347 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 348 | description='API.INVALID_SUBJECT_VALUE') |
||
| 349 | subject = str.strip(new_values['data']['subject']) |
||
| 350 | |||
| 351 | if 'message' not in new_values['data'].keys() or \ |
||
| 352 | not isinstance(new_values['data']['message'], str) or \ |
||
| 353 | len(str.strip(new_values['data']['message'])) == 0: |
||
| 354 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 355 | description='API.INVALID_MESSAGE_VALUE') |
||
| 356 | message = str.strip(new_values['data']['message']) |
||
| 357 | |||
| 358 | if 'status' not in new_values['data'].keys() or \ |
||
| 359 | not isinstance(new_values['data']['status'], str) or \ |
||
| 360 | len(str.strip(new_values['data']['status'])) == 0 or \ |
||
| 361 | str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'timeout'): |
||
| 362 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 363 | description='API.INVALID_STATUS') |
||
| 364 | status = str.strip(new_values['data']['status']) |
||
| 365 | |||
| 366 | if 'created_datetime' not in new_values['data'].keys() or \ |
||
| 367 | not isinstance(new_values['data']['created_datetime'], str) or \ |
||
| 368 | len(str.strip(new_values['data']['created_datetime'])) == 0: |
||
| 369 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 370 | description='API.INVALID_CREATED_DATETIME') |
||
| 371 | created_datetime_local = str.strip(new_values['data']['created_datetime']) |
||
| 372 | |||
| 373 | if 'scheduled_datetime' not in new_values['data'].keys() or \ |
||
| 374 | not isinstance(new_values['data']['scheduled_datetime'], str) or \ |
||
| 375 | len(str.strip(new_values['data']['scheduled_datetime'])) == 0: |
||
| 376 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 377 | description='API.INVALID_SCHEDULED_DATETIME') |
||
| 378 | scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime']) |
||
| 379 | |||
| 380 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 381 | if config.utc_offset[0] == '-': |
||
| 382 | timezone_offset = -timezone_offset |
||
| 383 | |||
| 384 | View Code Duplication | if created_datetime_local is None: |
|
| 385 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 386 | description="API.INVALID_CREATED_DATETIME") |
||
| 387 | else: |
||
| 388 | created_datetime_local = str.strip(created_datetime_local) |
||
| 389 | try: |
||
| 390 | created_datetime_utc = datetime.strptime(created_datetime_local, |
||
| 391 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 392 | timedelta(minutes=timezone_offset) |
||
| 393 | except ValueError: |
||
| 394 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 395 | description="API.INVALID_CREATED_DATETIME") |
||
| 396 | |||
| 397 | View Code Duplication | if scheduled_datetime_local is None: |
|
| 398 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 399 | description="API.INVALID_SCHEDULED_DATETIME") |
||
| 400 | else: |
||
| 401 | scheduled_datetime_local = str.strip(scheduled_datetime_local) |
||
| 402 | try: |
||
| 403 | scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local, |
||
| 404 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 405 | timedelta(minutes=timezone_offset) |
||
| 406 | except ValueError: |
||
| 407 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 408 | description="API.INVALID_SCHEDULED_DATETIME") |
||
| 409 | |||
| 410 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 411 | cursor = cnx.cursor() |
||
| 412 | |||
| 413 | cursor.execute(" SELECT recipient_name " |
||
| 414 | " FROM tbl_email_messages " |
||
| 415 | " WHERE id = %s ", (id_,)) |
||
| 416 | |||
| 417 | if cursor.fetchone() is None: |
||
| 418 | cursor.close() |
||
| 419 | cnx.disconnect() |
||
| 420 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 421 | description='API.EMAIL_MESSAGE_NOT_FOUND') |
||
| 422 | |||
| 423 | View Code Duplication | if rule_id is not None: |
|
| 424 | cursor.execute(" SELECT name " |
||
| 425 | " FROM tbl_rules " |
||
| 426 | " WHERE id = %s ", |
||
| 427 | (new_values['data']['rule_id'],)) |
||
| 428 | row = cursor.fetchone() |
||
| 429 | if row is None: |
||
| 430 | cursor.close() |
||
| 431 | cnx.disconnect() |
||
| 432 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 433 | description='API.RULE_NOT_FOUND') |
||
| 434 | |||
| 435 | update_row = (" UPDATE tbl_email_messages " |
||
| 436 | " SET rule_id = %s, recipient_name = %s, recipient_email = %s, subject = %s, message = %s," |
||
| 437 | " attachment_file_name = %s, attachment_file_object = %s, created_datetime_utc = %s," |
||
| 438 | " scheduled_datetime_utc = %s, status = %s" |
||
| 439 | " WHERE id = %s ") |
||
| 440 | |||
| 441 | cursor.execute(update_row, (rule_id, |
||
| 442 | recipient_name, |
||
| 443 | recipient_email, |
||
| 444 | subject, |
||
| 445 | message, |
||
| 446 | attachment_file_name, |
||
| 447 | attachment_file_object, |
||
| 448 | created_datetime_utc, |
||
| 449 | scheduled_datetime_utc, |
||
| 450 | status, |
||
| 451 | id_)) |
||
| 452 | |||
| 453 | cnx.commit() |
||
| 454 | cursor.close() |
||
| 455 | cnx.disconnect() |
||
| 456 | |||
| 457 | resp.status = falcon.HTTP_200 |
||
| 458 | |||
| 492 |