myems /
myems-api
| 1 | import falcon |
||
| 2 | import json |
||
| 3 | import mysql.connector |
||
| 4 | import config |
||
| 5 | import uuid |
||
| 6 | from datetime import datetime, timezone |
||
| 7 | import os |
||
| 8 | |||
| 9 | |||
| 10 | class OfflineMeterFileCollection: |
||
| 11 | @staticmethod |
||
| 12 | def __init__(): |
||
| 13 | pass |
||
| 14 | |||
| 15 | @staticmethod |
||
| 16 | def on_options(req, resp): |
||
| 17 | resp.status = falcon.HTTP_200 |
||
| 18 | |||
| 19 | @staticmethod |
||
| 20 | def on_get(req, resp): |
||
| 21 | cnx = mysql.connector.connect(**config.myems_historical_db) |
||
| 22 | cursor = cnx.cursor() |
||
| 23 | |||
| 24 | query = (" SELECT id, file_name, uuid, upload_datetime_utc, status " |
||
| 25 | " FROM tbl_offline_meter_files " |
||
| 26 | " ORDER BY upload_datetime_utc desc ") |
||
| 27 | cursor.execute(query) |
||
| 28 | rows = cursor.fetchall() |
||
| 29 | cursor.close() |
||
| 30 | cnx.disconnect() |
||
| 31 | |||
| 32 | result = list() |
||
| 33 | if rows is not None and len(rows) > 0: |
||
| 34 | for row in rows: |
||
| 35 | upload_datetime = row[3] |
||
| 36 | upload_datetime = upload_datetime.replace(tzinfo=timezone.utc) |
||
| 37 | meta_result = {"id": row[0], |
||
| 38 | "file_name": row[1], |
||
| 39 | "uuid": row[2], |
||
| 40 | "upload_datetime": upload_datetime.timestamp() * 1000, |
||
| 41 | "status": row[4]} |
||
| 42 | result.append(meta_result) |
||
| 43 | |||
| 44 | resp.body = json.dumps(result) |
||
| 45 | |||
| 46 | View Code Duplication | @staticmethod |
|
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
| 47 | def on_post(req, resp): |
||
| 48 | """Handles POST requests""" |
||
| 49 | try: |
||
| 50 | upload = req.get_param('file') |
||
| 51 | # Read upload file as binary |
||
| 52 | raw_blob = upload.file.read() |
||
| 53 | # Retrieve filename |
||
| 54 | filename = upload.filename |
||
| 55 | file_uuid = str(uuid.uuid4()) |
||
| 56 | |||
| 57 | # Define file_path |
||
| 58 | file_path = os.path.join(config.upload_path, file_uuid) |
||
| 59 | |||
| 60 | # Write to a temporary file to prevent incomplete files from |
||
| 61 | # being used. |
||
| 62 | temp_file_path = file_path + '~' |
||
| 63 | |||
| 64 | open(temp_file_path, 'wb').write(raw_blob) |
||
| 65 | |||
| 66 | # Now that we know the file has been fully saved to disk |
||
| 67 | # move it into place. |
||
| 68 | os.rename(temp_file_path, file_path) |
||
| 69 | except Exception as ex: |
||
| 70 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 71 | description='API.FAILED_TO_UPLOAD_OFFLINE_METER_FILE') |
||
| 72 | |||
| 73 | # Verify User Session |
||
| 74 | cookies = req.headers['SET-COOKIE'].split('=') |
||
| 75 | if 'user_uuid' not in cookies or 'token' not in cookies: |
||
| 76 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 77 | description='API.INVALID_COOKIES_PLEASE_RE_LOGIN') |
||
| 78 | |||
| 79 | cnx = mysql.connector.connect(**config.myems_user_db) |
||
| 80 | cursor = cnx.cursor() |
||
| 81 | |||
| 82 | query = (" SELECT utc_expires " |
||
| 83 | " FROM tbl_sessions " |
||
| 84 | " WHERE user_uuid = %s AND token = %s") |
||
| 85 | cursor.execute(query, (cookies[1], cookies[3],)) |
||
| 86 | row = cursor.fetchone() |
||
| 87 | |||
| 88 | if row is None: |
||
| 89 | cursor.close() |
||
| 90 | cnx.disconnect() |
||
| 91 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 92 | description='API.INVALID_COOKIES_PLEASE_RE_LOGIN') |
||
| 93 | else: |
||
| 94 | utc_expires = row[0] |
||
| 95 | if datetime.utcnow() > utc_expires: |
||
| 96 | cursor.close() |
||
| 97 | cnx.disconnect() |
||
| 98 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 99 | description='API.USER_SESSION_TIMEOUT') |
||
| 100 | |||
| 101 | cursor.execute(" SELECT id " |
||
| 102 | " FROM tbl_users " |
||
| 103 | " WHERE uuid = %s ", |
||
| 104 | (cookies[1],)) |
||
| 105 | if cursor.fetchone() is None: |
||
| 106 | cursor.close() |
||
| 107 | cnx.disconnect() |
||
| 108 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 109 | description='API.INVALID_COOKIES_PLEASE_RE_LOGIN') |
||
| 110 | |||
| 111 | cnx = mysql.connector.connect(**config.myems_historical_db) |
||
| 112 | cursor = cnx.cursor() |
||
| 113 | |||
| 114 | add_values = (" INSERT INTO tbl_offline_meter_files " |
||
| 115 | " (file_name, uuid, upload_datetime_utc, status, file_object ) " |
||
| 116 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 117 | cursor.execute(add_values, (filename, |
||
| 118 | file_uuid, |
||
| 119 | datetime.utcnow(), |
||
| 120 | 'new', |
||
| 121 | raw_blob)) |
||
| 122 | new_id = cursor.lastrowid |
||
| 123 | cnx.commit() |
||
| 124 | cursor.close() |
||
| 125 | cnx.disconnect() |
||
| 126 | |||
| 127 | resp.status = falcon.HTTP_201 |
||
| 128 | resp.location = '/offlinemeterfiles/' + str(new_id) |
||
| 129 | |||
| 130 | |||
| 131 | class OfflineMeterFileItem: |
||
| 132 | @staticmethod |
||
| 133 | def __init__(): |
||
| 134 | pass |
||
| 135 | |||
| 136 | @staticmethod |
||
| 137 | def on_options(req, resp, id_): |
||
| 138 | resp.status = falcon.HTTP_200 |
||
| 139 | |||
| 140 | View Code Duplication | @staticmethod |
|
|
0 ignored issues
–
show
|
|||
| 141 | def on_get(req, resp, id_): |
||
| 142 | if not id_.isdigit() or int(id_) <= 0: |
||
| 143 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 144 | title='API.BAD_REQUEST', |
||
| 145 | description='API.INVALID_OFFLINE_METER_FILE_ID') |
||
| 146 | |||
| 147 | cnx = mysql.connector.connect(**config.myems_historical_db) |
||
| 148 | cursor = cnx.cursor() |
||
| 149 | |||
| 150 | query = (" SELECT id, file_name, uuid, upload_datetime_utc, status " |
||
| 151 | " FROM tbl_offline_meter_files " |
||
| 152 | " WHERE id = %s ") |
||
| 153 | cursor.execute(query, (id_,)) |
||
| 154 | row = cursor.fetchone() |
||
| 155 | cursor.close() |
||
| 156 | cnx.disconnect() |
||
| 157 | if row is None: |
||
| 158 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 159 | description='API.OFFLINE_METER_FILE_NOT_FOUND') |
||
| 160 | |||
| 161 | upload_datetime = row[3] |
||
| 162 | upload_datetime = upload_datetime.replace(tzinfo=timezone.utc) |
||
| 163 | |||
| 164 | result = {"id": row[0], |
||
| 165 | "file_name": row[1], |
||
| 166 | "uuid": row[2], |
||
| 167 | "upload_datetime": upload_datetime.timestamp() * 1000, |
||
| 168 | "status": row[4]} |
||
| 169 | resp.body = json.dumps(result) |
||
| 170 | |||
| 171 | View Code Duplication | @staticmethod |
|
|
0 ignored issues
–
show
|
|||
| 172 | def on_delete(req, resp, id_): |
||
| 173 | if not id_.isdigit() or int(id_) <= 0: |
||
| 174 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 175 | description='API.INVALID_OFFLINE_METER_FILE_ID') |
||
| 176 | |||
| 177 | cnx = mysql.connector.connect(**config.myems_historical_db) |
||
| 178 | cursor = cnx.cursor() |
||
| 179 | |||
| 180 | cursor.execute(" SELECT uuid " |
||
| 181 | " FROM tbl_offline_meter_files " |
||
| 182 | " WHERE id = %s ", (id_,)) |
||
| 183 | row = cursor.fetchone() |
||
| 184 | if row is None: |
||
| 185 | cursor.close() |
||
| 186 | cnx.disconnect() |
||
| 187 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 188 | description='API.OFFLINE_METER_FILE_NOT_FOUND') |
||
| 189 | |||
| 190 | try: |
||
| 191 | file_uuid = row[0] |
||
| 192 | # Define file_path |
||
| 193 | file_path = os.path.join(config.upload_path, file_uuid) |
||
| 194 | |||
| 195 | # remove the file from disk |
||
| 196 | os.remove(file_path) |
||
| 197 | except Exception as ex: |
||
| 198 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 199 | description='API.OFFLINE_METER_FILE_NOT_FOUND') |
||
| 200 | |||
| 201 | # Note: the energy data imported from the deleted file will not be deleted |
||
| 202 | cursor.execute(" DELETE FROM tbl_offline_meter_files WHERE id = %s ", (id_,)) |
||
| 203 | cnx.commit() |
||
| 204 | |||
| 205 | cursor.close() |
||
| 206 | cnx.disconnect() |
||
| 207 | |||
| 208 | resp.status = falcon.HTTP_204 |
||
| 209 |