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