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