myems /
myems-api
| 1 | import falcon |
||
| 2 | import json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | from datetime import datetime, timedelta, timezone |
||
| 6 | |||
| 7 | |||
| 8 | View Code Duplication | class TextMessageCollection: |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 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 | View Code Duplication | class TextMessageItem: |
|
|
0 ignored issues
–
show
|
|||
| 87 | @staticmethod |
||
| 88 | def __init__(): |
||
| 89 | pass |
||
| 90 | |||
| 91 | @staticmethod |
||
| 92 | def on_options(req, resp, id_): |
||
| 93 | resp.status = falcon.HTTP_200 |
||
| 94 | |||
| 95 | @staticmethod |
||
| 96 | def on_get(req, resp, id_): |
||
| 97 | if not id_.isdigit() or int(id_) <= 0: |
||
| 98 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 99 | description='API.INVALID_TEXT_MESSAGE_ID') |
||
| 100 | |||
| 101 | try: |
||
| 102 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 103 | cursor = cnx.cursor() |
||
| 104 | except Exception as e: |
||
| 105 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 106 | |||
| 107 | try: |
||
| 108 | query = (" SELECT id, recipient_name, recipient_mobile, " |
||
| 109 | " message, created_datetime_utc, scheduled_datetime_utc, acknowledge_code, status " |
||
| 110 | " FROM tbl_text_messages_outbox " |
||
| 111 | " WHERE id = %s ") |
||
| 112 | cursor.execute(query, (id_,)) |
||
| 113 | row = cursor.fetchone() |
||
| 114 | |||
| 115 | if cursor: |
||
| 116 | cursor.close() |
||
| 117 | if cnx: |
||
| 118 | cnx.disconnect() |
||
| 119 | |||
| 120 | except Exception as e: |
||
| 121 | if cursor: |
||
| 122 | cursor.close() |
||
| 123 | if cnx: |
||
| 124 | cnx.disconnect() |
||
| 125 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 126 | |||
| 127 | if row is None: |
||
| 128 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 129 | description='API.TEXT_MESSAGE_NOT_FOUND') |
||
| 130 | |||
| 131 | result = {"id": row[0], |
||
| 132 | "recipient_name": row[1], |
||
| 133 | "recipient_mobile": row[2], |
||
| 134 | "message": row[3], |
||
| 135 | "created_datetime": row[4].timestamp() * 1000 if isinstance(row[4], datetime) else None, |
||
| 136 | "scheduled_datetime": row[5].timestamp() * 1000 if isinstance(row[5], datetime) else None, |
||
| 137 | "acknowledge_code": row[6], |
||
| 138 | "status": row[7]} |
||
| 139 | |||
| 140 | resp.body = json.dumps(result) |
||
| 141 | |||
| 142 | @staticmethod |
||
| 143 | def on_delete(req, resp, id_): |
||
| 144 | if not id_.isdigit() or int(id_) <= 0: |
||
| 145 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 146 | description='API.INVALID_TEXT_MESSAGE_ID') |
||
| 147 | |||
| 148 | cnx = None |
||
| 149 | cursor = None |
||
| 150 | try: |
||
| 151 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 152 | cursor = cnx.cursor() |
||
| 153 | except Exception as e: |
||
| 154 | if cursor: |
||
| 155 | cursor.close() |
||
| 156 | if cnx: |
||
| 157 | cnx.disconnect() |
||
| 158 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 159 | |||
| 160 | try: |
||
| 161 | cursor.execute(" SELECT id FROM tbl_text_messages_outbox WHERE id = %s ", (id_,)) |
||
| 162 | row = cursor.fetchone() |
||
| 163 | except Exception as e: |
||
| 164 | if cursor: |
||
| 165 | cursor.close() |
||
| 166 | if cnx: |
||
| 167 | cnx.disconnect() |
||
| 168 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 169 | |||
| 170 | if row is None: |
||
| 171 | if cursor: |
||
| 172 | cursor.close() |
||
| 173 | if cnx: |
||
| 174 | cnx.disconnect() |
||
| 175 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 176 | description='API.TEXT_MESSAGE_NOT_FOUND') |
||
| 177 | |||
| 178 | try: |
||
| 179 | cursor.execute(" DELETE FROM tbl_text_messages_outbox WHERE id = %s ", (id_,)) |
||
| 180 | cnx.commit() |
||
| 181 | except Exception as e: |
||
| 182 | if cursor: |
||
| 183 | cursor.close() |
||
| 184 | if cnx: |
||
| 185 | cnx.disconnect() |
||
| 186 | raise falcon.HTTPError(falcon.HTTP_500, title='API.DATABASE_ERROR', description=str(e)) |
||
| 187 | |||
| 188 | if cursor: |
||
| 189 | cursor.close() |
||
| 190 | if cnx: |
||
| 191 | cnx.disconnect() |
||
| 192 | |||
| 193 | resp.status = falcon.HTTP_204 |
||
| 194 | |||
| 195 |