| Conditions | 7 |
| Total Lines | 83 |
| Code Lines | 55 |
| Lines | 83 |
| Ratio | 100 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import falcon |
||
| 46 | View Code Duplication | @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 | 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 | |||
| 209 |