| Conditions | 15 |
| Total Lines | 98 |
| Code Lines | 73 |
| Lines | 98 |
| 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:
Complex classes like core.costfile.CostFileCollection.on_post() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import os |
||
| 61 | @staticmethod |
||
| 62 | @user_logger |
||
| 63 | def on_post(req, resp): |
||
| 64 | """Handles POST requests""" |
||
| 65 | admin_control(req) |
||
| 66 | try: |
||
| 67 | upload = req.get_param('file') |
||
| 68 | # Read upload file as binary |
||
| 69 | raw_blob = upload.file.read() |
||
| 70 | # Retrieve filename |
||
| 71 | filename = upload.filename |
||
| 72 | file_uuid = str(uuid.uuid4()) |
||
| 73 | |||
| 74 | # Define file_path |
||
| 75 | file_path = os.path.join(config.upload_path, file_uuid) |
||
| 76 | |||
| 77 | # Write to a temporary file to prevent incomplete files from being used. |
||
| 78 | with open(file_path + '~', 'wb') as f: |
||
| 79 | f.write(raw_blob) |
||
| 80 | |||
| 81 | # Now that we know the file has been fully saved to disk move it into place. |
||
| 82 | os.rename(file_path + '~', file_path) |
||
| 83 | except OSError as ex: |
||
| 84 | print("Failed to stream request") |
||
| 85 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 86 | description='API.FAILED_TO_UPLOAD_COST_FILE') |
||
| 87 | except Exception as ex: |
||
| 88 | print("Unexpected error reading request stream") |
||
| 89 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.ERROR', |
||
| 90 | description='API.FAILED_TO_UPLOAD_COST_FILE') |
||
| 91 | |||
| 92 | # Verify User Session |
||
| 93 | token = req.headers.get('TOKEN') |
||
| 94 | user_uuid = req.headers.get('USER-UUID') |
||
| 95 | if token is None: |
||
| 96 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 97 | description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
||
| 98 | if user_uuid is None: |
||
| 99 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 100 | description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN') |
||
| 101 | |||
| 102 | cnx_user_db = mysql.connector.connect(**config.myems_user_db) |
||
| 103 | cursor_user_db = cnx_user_db.cursor() |
||
| 104 | |||
| 105 | query = (" SELECT utc_expires " |
||
| 106 | " FROM tbl_sessions " |
||
| 107 | " WHERE user_uuid = %s AND token = %s") |
||
| 108 | cursor_user_db.execute(query, (user_uuid, token,)) |
||
| 109 | row = cursor_user_db.fetchone() |
||
| 110 | |||
| 111 | if row is None: |
||
| 112 | if cursor_user_db: |
||
| 113 | cursor_user_db.close() |
||
| 114 | if cnx_user_db: |
||
| 115 | cnx_user_db.close() |
||
| 116 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 117 | description='API.INVALID_SESSION_PLEASE_RE_LOGIN') |
||
| 118 | else: |
||
| 119 | utc_expires = row[0] |
||
| 120 | if datetime.utcnow() > utc_expires: |
||
| 121 | if cursor_user_db: |
||
| 122 | cursor_user_db.close() |
||
| 123 | if cnx_user_db: |
||
| 124 | cnx_user_db.close() |
||
| 125 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 126 | description='API.USER_SESSION_TIMEOUT') |
||
| 127 | |||
| 128 | cursor_user_db.execute(" SELECT id " |
||
| 129 | " FROM tbl_users " |
||
| 130 | " WHERE uuid = %s ", |
||
| 131 | (user_uuid,)) |
||
| 132 | row = cursor_user_db.fetchone() |
||
| 133 | if row is None: |
||
| 134 | if cursor_user_db: |
||
| 135 | cursor_user_db.close() |
||
| 136 | if cnx_user_db: |
||
| 137 | cnx_user_db.close() |
||
| 138 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 139 | description='API.INVALID_USER_PLEASE_RE_LOGIN') |
||
| 140 | |||
| 141 | cnx_historical_db = mysql.connector.connect(**config.myems_historical_db) |
||
| 142 | cursor_historical_db = cnx_historical_db.cursor() |
||
| 143 | |||
| 144 | add_values = (" INSERT INTO tbl_cost_files " |
||
| 145 | " (file_name, uuid, upload_datetime_utc, status, file_object ) " |
||
| 146 | " VALUES (%s, %s, %s, %s, %s) ") |
||
| 147 | cursor_historical_db.execute(add_values, (filename, |
||
| 148 | file_uuid, |
||
| 149 | datetime.utcnow(), |
||
| 150 | 'new', |
||
| 151 | raw_blob)) |
||
| 152 | new_id = cursor_historical_db.lastrowid |
||
| 153 | cnx_historical_db.commit() |
||
| 154 | cursor_historical_db.close() |
||
| 155 | cnx_historical_db.close() |
||
| 156 | |||
| 157 | resp.status = falcon.HTTP_201 |
||
| 158 | resp.location = '/costfiles/' + str(new_id) |
||
| 159 | |||
| 313 |