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