| Conditions | 16 |
| Total Lines | 80 |
| Code Lines | 61 |
| Lines | 11 |
| Ratio | 13.75 % |
| 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 webmessage.WebMessageCollection.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 | # get user dict |
||
| 48 | cnx = mysql.connector.connect(**config.myems_user_db) |
||
| 49 | cursor = cnx.cursor(dictionary=True) |
||
| 50 | |||
| 51 | query = (" SELECT id, display_name " |
||
| 52 | " FROM tbl_users ") |
||
| 53 | cursor.execute(query) |
||
| 54 | rows_users = cursor.fetchall() |
||
| 55 | |||
| 56 | if cursor: |
||
| 57 | cursor.close() |
||
| 58 | if cnx: |
||
| 59 | cnx.disconnect() |
||
| 60 | |||
| 61 | user_dict = dict() |
||
| 62 | if rows_users is not None and len(rows_users) > 0: |
||
| 63 | for row in rows_users: |
||
| 64 | user_dict[row['id']] = row['display_name'] |
||
| 65 | |||
| 66 | # get web messages |
||
| 67 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 68 | cursor = cnx.cursor() |
||
| 69 | |||
| 70 | query = (" SELECT id, user_id, subject, message, " |
||
| 71 | " created_datetime_utc, status, reply " |
||
| 72 | " FROM tbl_web_messages " |
||
| 73 | " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
||
| 74 | " ORDER BY created_datetime_utc DESC ") |
||
| 75 | cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
||
| 76 | rows = cursor.fetchall() |
||
| 77 | |||
| 78 | if cursor: |
||
| 79 | cursor.close() |
||
| 80 | if cnx: |
||
| 81 | cnx.disconnect() |
||
| 82 | |||
| 83 | result = list() |
||
| 84 | View Code Duplication | if rows is not None and len(rows) > 0: |
|
|
|
|||
| 85 | for row in rows: |
||
| 86 | meta_result = {"id": row[0], |
||
| 87 | "user_id": row[1], |
||
| 88 | "user_display_name": user_dict.get(row[1], None), |
||
| 89 | "subject": row[2], |
||
| 90 | "message": row[3].replace("<br>", ""), |
||
| 91 | "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None, |
||
| 92 | "status": row[5], |
||
| 93 | "reply": row[6]} |
||
| 94 | result.append(meta_result) |
||
| 95 | |||
| 96 | resp.body = json.dumps(result) |
||
| 97 | |||
| 312 |