| Conditions | 14 |
| Total Lines | 70 |
| Code Lines | 56 |
| Lines | 70 |
| Ratio | 100 % |
| 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 emailmessage.EmailMessageCollection.on_get() 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 |
||
| 17 | @staticmethod |
||
| 18 | def on_get(req, resp, startdate, enddate): |
||
| 19 | try: |
||
| 20 | start_datetime_local = datetime.strptime(startdate, '%Y-%m-%d') |
||
| 21 | except Exception: |
||
| 22 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 23 | title='API.BAD_REQUEST', |
||
| 24 | description='API.INVALID_START_DATE_FORMAT') |
||
| 25 | try: |
||
| 26 | end_datetime_local = datetime.strptime(enddate, '%Y-%m-%d') |
||
| 27 | except Exception: |
||
| 28 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 29 | title='API.BAD_REQUEST', |
||
| 30 | description='API.INVALID_END_DATE_FORMAT') |
||
| 31 | |||
| 32 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 33 | if config.utc_offset[0] == '-': |
||
| 34 | timezone_offset = -timezone_offset |
||
| 35 | |||
| 36 | start_datetime_utc = start_datetime_local.replace(tzinfo=timezone.utc) |
||
| 37 | start_datetime_utc -= timedelta(minutes=timezone_offset) |
||
| 38 | |||
| 39 | end_datetime_utc = end_datetime_local.replace(tzinfo=timezone.utc) |
||
| 40 | end_datetime_utc -= timedelta(minutes=timezone_offset) |
||
| 41 | end_datetime_utc += timedelta(days=1) |
||
| 42 | |||
| 43 | if start_datetime_utc >= end_datetime_utc: |
||
| 44 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 45 | title='API.BAD_REQUEST', |
||
| 46 | description='API.START_DATETIME_SHOULD_BE_EARLY_THAN_END_DATETIME') |
||
| 47 | |||
| 48 | try: |
||
| 49 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 50 | cursor = cnx.cursor() |
||
| 51 | except Exception as e: |
||
| 52 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 53 | |||
| 54 | try: |
||
| 55 | query = (" SELECT id, recipient_name, recipient_email, " |
||
| 56 | " subject, message, attachment_file_name, " |
||
| 57 | " created_datetime_utc, scheduled_datetime_utc, status " |
||
| 58 | " FROM tbl_email_messages " |
||
| 59 | " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
||
| 60 | " ORDER BY created_datetime_utc ") |
||
| 61 | cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
||
| 62 | rows = cursor.fetchall() |
||
| 63 | |||
| 64 | if cursor: |
||
| 65 | cursor.close() |
||
| 66 | if cnx: |
||
| 67 | cnx.disconnect() |
||
| 68 | except Exception as e: |
||
| 69 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 70 | |||
| 71 | result = list() |
||
| 72 | if rows is not None and len(rows) > 0: |
||
| 73 | for row in rows: |
||
| 74 | meta_result = {"id": row[0], |
||
| 75 | "recipient_name": row[1], |
||
| 76 | "recipient_email": row[2], |
||
| 77 | "subject": row[3], |
||
| 78 | "message": row[4].replace("<br>", ""), |
||
| 79 | "attachment_file_name": row[5], |
||
| 80 | "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None, |
||
| 81 | "scheduled_datetime": |
||
| 82 | row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None, |
||
| 83 | "status": row[8]} |
||
| 84 | result.append(meta_result) |
||
| 85 | |||
| 86 | resp.body = json.dumps(result) |
||
| 87 | |||
| 198 |