| Conditions | 13 | 
| Total Lines | 95 | 
| Code Lines | 67 | 
| Lines | 95 | 
| 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 falcon  | 
            ||
| 51 | @staticmethod  | 
            ||
| 52 | @user_logger  | 
            ||
| 53 | def on_post(req, resp):  | 
            ||
| 54 | """Handles POST requests"""  | 
            ||
| 55 | try:  | 
            ||
| 56 |             upload = req.get_param('file') | 
            ||
| 57 | # Read upload file as binary  | 
            ||
| 58 | raw_blob = upload.file.read()  | 
            ||
| 59 | # Retrieve filename  | 
            ||
| 60 | filename = upload.filename  | 
            ||
| 61 | file_uuid = str(uuid.uuid4())  | 
            ||
| 62 | |||
| 63 | # Define file_path  | 
            ||
| 64 | file_path = os.path.join(config.upload_path, file_uuid)  | 
            ||
| 65 | |||
| 66 | # Write to a temporary file to prevent incomplete files from  | 
            ||
| 67 | # being used.  | 
            ||
| 68 | temp_file_path = file_path + '~'  | 
            ||
| 69 | |||
| 70 | open(temp_file_path, 'wb').write(raw_blob)  | 
            ||
| 71 | |||
| 72 | # Now that we know the file has been fully saved to disk  | 
            ||
| 73 | # move it into place.  | 
            ||
| 74 | os.rename(temp_file_path, file_path)  | 
            ||
| 75 | except Exception as ex:  | 
            ||
| 76 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',  | 
            ||
| 77 | description='API.FAILED_TO_UPLOAD_COST_FILE')  | 
            ||
| 78 | |||
| 79 | # Verify User Session  | 
            ||
| 80 |         token = req.headers.get('TOKEN') | 
            ||
| 81 |         user_uuid = req.headers.get('USER-UUID') | 
            ||
| 82 | if token is None:  | 
            ||
| 83 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 84 | description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')  | 
            ||
| 85 | if user_uuid is None:  | 
            ||
| 86 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 87 | description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')  | 
            ||
| 88 | |||
| 89 | cnx_user_db = mysql.connector.connect(**config.myems_user_db)  | 
            ||
| 90 | cursor_user_db = cnx_user_db.cursor()  | 
            ||
| 91 | |||
| 92 |         query = (" SELECT utc_expires " | 
            ||
| 93 | " FROM tbl_sessions "  | 
            ||
| 94 | " WHERE user_uuid = %s AND token = %s")  | 
            ||
| 95 | cursor_user_db.execute(query, (user_uuid, token,))  | 
            ||
| 96 | row = cursor_user_db.fetchone()  | 
            ||
| 97 | |||
| 98 | if row is None:  | 
            ||
| 99 | if cursor_user_db:  | 
            ||
| 100 | cursor_user_db.close()  | 
            ||
| 101 | if cnx_user_db:  | 
            ||
| 102 | cnx_user_db.disconnect()  | 
            ||
| 103 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 104 | description='API.INVALID_SESSION_PLEASE_RE_LOGIN')  | 
            ||
| 105 | else:  | 
            ||
| 106 | utc_expires = row[0]  | 
            ||
| 107 | if datetime.utcnow() > utc_expires:  | 
            ||
| 108 | if cursor_user_db:  | 
            ||
| 109 | cursor_user_db.close()  | 
            ||
| 110 | if cnx_user_db:  | 
            ||
| 111 | cnx_user_db.disconnect()  | 
            ||
| 112 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 113 | description='API.USER_SESSION_TIMEOUT')  | 
            ||
| 114 | |||
| 115 |         cursor_user_db.execute(" SELECT id " | 
            ||
| 116 | " FROM tbl_users "  | 
            ||
| 117 | " WHERE uuid = %s ",  | 
            ||
| 118 | (user_uuid,))  | 
            ||
| 119 | row = cursor_user_db.fetchone()  | 
            ||
| 120 | if row is None:  | 
            ||
| 121 | if cursor_user_db:  | 
            ||
| 122 | cursor_user_db.close()  | 
            ||
| 123 | if cnx_user_db:  | 
            ||
| 124 | cnx_user_db.disconnect()  | 
            ||
| 125 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 126 | description='API.INVALID_USER_PLEASE_RE_LOGIN')  | 
            ||
| 127 | |||
| 128 | cnx_historical_db = mysql.connector.connect(**config.myems_historical_db)  | 
            ||
| 129 | cursor_historical_db = cnx_historical_db.cursor()  | 
            ||
| 130 | |||
| 131 |         add_values = (" INSERT INTO tbl_cost_files " | 
            ||
| 132 | " (file_name, uuid, upload_datetime_utc, status, file_object ) "  | 
            ||
| 133 | " VALUES (%s, %s, %s, %s, %s) ")  | 
            ||
| 134 | cursor_historical_db.execute(add_values, (filename,  | 
            ||
| 135 | file_uuid,  | 
            ||
| 136 | datetime.utcnow(),  | 
            ||
| 137 | 'new',  | 
            ||
| 138 | raw_blob))  | 
            ||
| 139 | new_id = cursor_historical_db.lastrowid  | 
            ||
| 140 | cnx_historical_db.commit()  | 
            ||
| 141 | cursor_historical_db.close()  | 
            ||
| 142 | cnx_historical_db.disconnect()  | 
            ||
| 143 | |||
| 144 | resp.status = falcon.HTTP_201  | 
            ||
| 145 | resp.location = '/costfiles/' + str(new_id)  | 
            ||
| 146 | |||
| 285 |