| @@ 10-141 (lines=132) @@ | ||
| 7 | import os |
|
| 8 | ||
| 9 | ||
| 10 | class OfflineCostFileCollection: |
|
| 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_cost_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 | @staticmethod |
|
| 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_COST_FILE') |
|
| 72 | ||
| 73 | # Verify User Session |
|
| 74 | token = req.headers.get('TOKEN') |
|
| 75 | user_uuid = req.headers.get('USER-UUID') |
|
| 76 | if token is None: |
|
| 77 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 78 | description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
|
| 79 | if user_uuid is None: |
|
| 80 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 81 | description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
|
| 82 | ||
| 83 | cnx = mysql.connector.connect(**config.myems_user_db) |
|
| 84 | cursor = cnx.cursor() |
|
| 85 | ||
| 86 | query = (" SELECT utc_expires " |
|
| 87 | " FROM tbl_sessions " |
|
| 88 | " WHERE user_uuid = %s AND token = %s") |
|
| 89 | cursor.execute(query, (user_uuid, token,)) |
|
| 90 | row = cursor.fetchone() |
|
| 91 | ||
| 92 | if row is None: |
|
| 93 | if cursor: |
|
| 94 | cursor.close() |
|
| 95 | if cnx: |
|
| 96 | cnx.disconnect() |
|
| 97 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 98 | description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
|
| 99 | else: |
|
| 100 | utc_expires = row[0] |
|
| 101 | if datetime.utcnow() > utc_expires: |
|
| 102 | if cursor: |
|
| 103 | cursor.close() |
|
| 104 | if cnx: |
|
| 105 | cnx.disconnect() |
|
| 106 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 107 | description='API.USER_SESSION_TIMEOUT') |
|
| 108 | ||
| 109 | cursor.execute(" SELECT id " |
|
| 110 | " FROM tbl_users " |
|
| 111 | " WHERE uuid = %s ", |
|
| 112 | (user_uuid,)) |
|
| 113 | row = cursor.fetchone() |
|
| 114 | if row is None: |
|
| 115 | if cursor: |
|
| 116 | cursor.close() |
|
| 117 | if cnx: |
|
| 118 | cnx.disconnect() |
|
| 119 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 120 | description='API.INVALID_USER_PLEASE_RE_LOGIN') |
|
| 121 | else: |
|
| 122 | user_id = row[0] |
|
| 123 | ||
| 124 | cnx = mysql.connector.connect(**config.myems_historical_db) |
|
| 125 | cursor = cnx.cursor() |
|
| 126 | ||
| 127 | add_values = (" INSERT INTO tbl_offline_cost_files " |
|
| 128 | " (file_name, uuid, upload_datetime_utc, status, file_object ) " |
|
| 129 | " VALUES (%s, %s, %s, %s, %s) ") |
|
| 130 | cursor.execute(add_values, (filename, |
|
| 131 | file_uuid, |
|
| 132 | datetime.utcnow(), |
|
| 133 | 'new', |
|
| 134 | raw_blob)) |
|
| 135 | new_id = cursor.lastrowid |
|
| 136 | cnx.commit() |
|
| 137 | cursor.close() |
|
| 138 | cnx.disconnect() |
|
| 139 | ||
| 140 | resp.status = falcon.HTTP_201 |
|
| 141 | resp.location = '/offlinecostfiles/' + str(new_id) |
|
| 142 | ||
| 143 | ||
| 144 | class OfflineCostFileItem: |
|
| @@ 10-141 (lines=132) @@ | ||
| 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 | @staticmethod |
|
| 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 | token = req.headers.get('TOKEN') |
|
| 75 | user_uuid = req.headers.get('USER-UUID') |
|
| 76 | if token is None: |
|
| 77 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 78 | description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
|
| 79 | if user_uuid is None: |
|
| 80 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 81 | description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
|
| 82 | ||
| 83 | cnx = mysql.connector.connect(**config.myems_user_db) |
|
| 84 | cursor = cnx.cursor() |
|
| 85 | ||
| 86 | query = (" SELECT utc_expires " |
|
| 87 | " FROM tbl_sessions " |
|
| 88 | " WHERE user_uuid = %s AND token = %s") |
|
| 89 | cursor.execute(query, (user_uuid, token,)) |
|
| 90 | row = cursor.fetchone() |
|
| 91 | ||
| 92 | if row is None: |
|
| 93 | if cursor: |
|
| 94 | cursor.close() |
|
| 95 | if cnx: |
|
| 96 | cnx.disconnect() |
|
| 97 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 98 | description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
|
| 99 | else: |
|
| 100 | utc_expires = row[0] |
|
| 101 | if datetime.utcnow() > utc_expires: |
|
| 102 | if cursor: |
|
| 103 | cursor.close() |
|
| 104 | if cnx: |
|
| 105 | cnx.disconnect() |
|
| 106 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 107 | description='API.USER_SESSION_TIMEOUT') |
|
| 108 | ||
| 109 | cursor.execute(" SELECT id " |
|
| 110 | " FROM tbl_users " |
|
| 111 | " WHERE uuid = %s ", |
|
| 112 | (user_uuid,)) |
|
| 113 | row = cursor.fetchone() |
|
| 114 | if row is None: |
|
| 115 | if cursor: |
|
| 116 | cursor.close() |
|
| 117 | if cnx: |
|
| 118 | cnx.disconnect() |
|
| 119 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
| 120 | description='API.INVALID_USER_PLEASE_RE_LOGIN') |
|
| 121 | else: |
|
| 122 | user_id = row[0] |
|
| 123 | ||
| 124 | cnx = mysql.connector.connect(**config.myems_historical_db) |
|
| 125 | cursor = cnx.cursor() |
|
| 126 | ||
| 127 | add_values = (" INSERT INTO tbl_offline_meter_files " |
|
| 128 | " (file_name, uuid, upload_datetime_utc, status, file_object ) " |
|
| 129 | " VALUES (%s, %s, %s, %s, %s) ") |
|
| 130 | cursor.execute(add_values, (filename, |
|
| 131 | file_uuid, |
|
| 132 | datetime.utcnow(), |
|
| 133 | 'new', |
|
| 134 | raw_blob)) |
|
| 135 | new_id = cursor.lastrowid |
|
| 136 | cnx.commit() |
|
| 137 | cursor.close() |
|
| 138 | cnx.disconnect() |
|
| 139 | ||
| 140 | resp.status = falcon.HTTP_201 |
|
| 141 | resp.location = '/offlinemeterfiles/' + str(new_id) |
|
| 142 | ||
| 143 | ||
| 144 | class OfflineMeterFileItem: |
|