Conditions | 14 |
Total Lines | 75 |
Code Lines | 59 |
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.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 |
||
19 | @staticmethod |
||
20 | def on_get(req, resp): |
||
21 | access_control(req) |
||
22 | print(req.params) |
||
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 | |||
61 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
62 | cursor = cnx.cursor() |
||
63 | |||
64 | query = (" SELECT id, recipient_name, recipient_email, " |
||
65 | " subject, message, attachment_file_name, " |
||
66 | " created_datetime_utc, scheduled_datetime_utc, status " |
||
67 | " FROM tbl_email_messages " |
||
68 | " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
||
69 | " ORDER BY created_datetime_utc ") |
||
70 | cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
||
71 | rows = cursor.fetchall() |
||
72 | |||
73 | if cursor: |
||
74 | cursor.close() |
||
75 | if cnx: |
||
76 | cnx.disconnect() |
||
77 | |||
78 | result = list() |
||
79 | if rows is not None and len(rows) > 0: |
||
80 | for row in rows: |
||
81 | meta_result = {"id": row[0], |
||
82 | "recipient_name": row[1], |
||
83 | "recipient_email": row[2], |
||
84 | "subject": row[3], |
||
85 | "message": row[4].replace("<br>", ""), |
||
86 | "attachment_file_name": row[5], |
||
87 | "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None, |
||
88 | "scheduled_datetime": |
||
89 | row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None, |
||
90 | "status": row[8]} |
||
91 | result.append(meta_result) |
||
92 | |||
93 | resp.text = json.dumps(result) |
||
94 | |||
179 |