| Conditions | 14 |
| Total Lines | 67 |
| Code Lines | 55 |
| Lines | 67 |
| 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 textmessage.TextMessageCollection.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 | try: |
||
| 48 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 49 | cursor = cnx.cursor() |
||
| 50 | except Exception as e: |
||
| 51 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 52 | |||
| 53 | try: |
||
| 54 | query = (" SELECT id, recipient_name, recipient_mobile, " |
||
| 55 | " message, created_datetime_utc, scheduled_datetime_utc, acknowledge_code, status " |
||
| 56 | " FROM tbl_text_messages_outbox " |
||
| 57 | " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
||
| 58 | " ORDER BY created_datetime_utc ") |
||
| 59 | cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
||
| 60 | rows = cursor.fetchall() |
||
| 61 | |||
| 62 | if cursor: |
||
| 63 | cursor.close() |
||
| 64 | if cnx: |
||
| 65 | cnx.disconnect() |
||
| 66 | except Exception as e: |
||
| 67 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 68 | |||
| 69 | result = list() |
||
| 70 | if rows is not None and len(rows) > 0: |
||
| 71 | for row in rows: |
||
| 72 | meta_result = {"id": row[0], |
||
| 73 | "recipient_name": row[1], |
||
| 74 | "recipient_mobile": row[2], |
||
| 75 | "message": row[3], |
||
| 76 | "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None, |
||
| 77 | "scheduled_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) |
||
| 78 | else None, |
||
| 79 | "acknowledge_code": row[6], |
||
| 80 | "status": row[7]} |
||
| 81 | result.append(meta_result) |
||
| 82 | |||
| 83 | resp.body = json.dumps(result) |
||
| 84 | |||
| 195 |