@@ 8-86 (lines=79) @@ | ||
5 | from datetime import datetime, timedelta, timezone |
|
6 | ||
7 | ||
8 | class EmailMessageCollection: |
|
9 | @staticmethod |
|
10 | def __init__(): |
|
11 | pass |
|
12 | ||
13 | @staticmethod |
|
14 | def on_options(req, resp, startdate, enddate): |
|
15 | resp.status = falcon.HTTP_200 |
|
16 | ||
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 | ||
48 | try: |
|
49 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
50 | cursor = cnx.cursor() |
|
51 | except Exception as e: |
|
52 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
|
53 | ||
54 | try: |
|
55 | query = (" SELECT id, recipient_name, recipient_email, " |
|
56 | " subject, message, attachment_file_name, " |
|
57 | " created_datetime_utc, scheduled_datetime_utc, status " |
|
58 | " FROM tbl_email_messages " |
|
59 | " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
|
60 | " ORDER BY created_datetime_utc ") |
|
61 | cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
|
62 | rows = cursor.fetchall() |
|
63 | ||
64 | if cursor: |
|
65 | cursor.close() |
|
66 | if cnx: |
|
67 | cnx.disconnect() |
|
68 | except Exception as e: |
|
69 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
|
70 | ||
71 | result = list() |
|
72 | if rows is not None and len(rows) > 0: |
|
73 | for row in rows: |
|
74 | meta_result = {"id": row[0], |
|
75 | "recipient_name": row[1], |
|
76 | "recipient_email": row[2], |
|
77 | "subject": row[3], |
|
78 | "message": row[4].replace("<br>", ""), |
|
79 | "attachment_file_name": row[5], |
|
80 | "created_datetime": row[6].timestamp() * 1000 if isinstance(row[6], datetime) else None, |
|
81 | "scheduled_datetime": |
|
82 | row[7].timestamp() * 1000 if isinstance(row[7], datetime) else None, |
|
83 | "status": row[8]} |
|
84 | result.append(meta_result) |
|
85 | ||
86 | resp.body = json.dumps(result) |
|
87 | ||
88 | ||
89 | class EmailMessageItem: |
@@ 8-83 (lines=76) @@ | ||
5 | from datetime import datetime, timedelta, timezone |
|
6 | ||
7 | ||
8 | class TextMessageCollection: |
|
9 | @staticmethod |
|
10 | def __init__(): |
|
11 | pass |
|
12 | ||
13 | @staticmethod |
|
14 | def on_options(req, resp, startdate, enddate): |
|
15 | resp.status = falcon.HTTP_200 |
|
16 | ||
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 | try: |
|
48 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
|
49 | cursor = cnx.cursor() |
|
50 | except Exception as e: |
|
51 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
|
52 | ||
53 | try: |
|
54 | query = (" SELECT id, recipient_name, recipient_mobile, " |
|
55 | " message, created_datetime_utc, scheduled_datetime_utc, acknowledge_code, status " |
|
56 | " FROM tbl_text_messages_outbox " |
|
57 | " WHERE created_datetime_utc >= %s AND created_datetime_utc < %s " |
|
58 | " ORDER BY created_datetime_utc ") |
|
59 | cursor.execute(query, (start_datetime_utc, end_datetime_utc)) |
|
60 | rows = cursor.fetchall() |
|
61 | ||
62 | if cursor: |
|
63 | cursor.close() |
|
64 | if cnx: |
|
65 | cnx.disconnect() |
|
66 | except Exception as e: |
|
67 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
|
68 | ||
69 | result = list() |
|
70 | if rows is not None and len(rows) > 0: |
|
71 | for row in rows: |
|
72 | meta_result = {"id": row[0], |
|
73 | "recipient_name": row[1], |
|
74 | "recipient_mobile": row[2], |
|
75 | "message": row[3], |
|
76 | "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None, |
|
77 | "scheduled_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) |
|
78 | else None, |
|
79 | "acknowledge_code": row[6], |
|
80 | "status": row[7]} |
|
81 | result.append(meta_result) |
|
82 | ||
83 | resp.body = json.dumps(result) |
|
84 | ||
85 | ||
86 | class TextMessageItem: |