| Conditions | 13 | 
| Total Lines | 98 | 
| Code Lines | 68 | 
| Lines | 0 | 
| Ratio | 0 % | 
| 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.knowledgefile.KnowledgeFileCollection.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  | 
            ||
| 78 | @staticmethod  | 
            ||
| 79 | @user_logger  | 
            ||
| 80 | def on_post(req, resp):  | 
            ||
| 81 | """Handles POST requests"""  | 
            ||
| 82 | |||
| 83 | try:  | 
            ||
| 84 |             upload = req.get_param('file') | 
            ||
| 85 | # Read upload file as binary  | 
            ||
| 86 | raw_blob = upload.file.read()  | 
            ||
| 87 | # Retrieve filename  | 
            ||
| 88 | filename = upload.filename  | 
            ||
| 89 | file_uuid = str(uuid.uuid4())  | 
            ||
| 90 | |||
| 91 | # Define file_path  | 
            ||
| 92 | file_path = os.path.join(config.upload_path, file_uuid)  | 
            ||
| 93 | |||
| 94 | # Write to a temporary file to prevent incomplete files from  | 
            ||
| 95 | # being used.  | 
            ||
| 96 | temp_file_path = file_path + '~'  | 
            ||
| 97 | |||
| 98 | open(temp_file_path, 'wb').write(raw_blob)  | 
            ||
| 99 | |||
| 100 | # Now that we know the file has been fully saved to disk  | 
            ||
| 101 | # move it into place.  | 
            ||
| 102 | os.rename(temp_file_path, file_path)  | 
            ||
| 103 | except Exception as ex:  | 
            ||
| 104 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR',  | 
            ||
| 105 | description='API.FAILED_TO_UPLOAD_KNOWLEDGE_FILE')  | 
            ||
| 106 | |||
| 107 | # Verify User Session  | 
            ||
| 108 |         token = req.headers.get('TOKEN') | 
            ||
| 109 |         user_uuid = req.headers.get('USER-UUID') | 
            ||
| 110 | if token is None:  | 
            ||
| 111 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 112 | description='API.TOKEN_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')  | 
            ||
| 113 | if user_uuid is None:  | 
            ||
| 114 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 115 | description='API.USER_UUID_NOT_FOUND_IN_HEADERS_PLEASE_LOGIN')  | 
            ||
| 116 | |||
| 117 | cnx = mysql.connector.connect(**config.myems_user_db)  | 
            ||
| 118 | cursor = cnx.cursor()  | 
            ||
| 119 | |||
| 120 |         query = (" SELECT utc_expires " | 
            ||
| 121 | " FROM tbl_sessions "  | 
            ||
| 122 | " WHERE user_uuid = %s AND token = %s")  | 
            ||
| 123 | cursor.execute(query, (user_uuid, token,))  | 
            ||
| 124 | row = cursor.fetchone()  | 
            ||
| 125 | |||
| 126 | if row is None:  | 
            ||
| 127 | if cursor:  | 
            ||
| 128 | cursor.close()  | 
            ||
| 129 | if cnx:  | 
            ||
| 130 | cnx.disconnect()  | 
            ||
| 131 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 132 | description='API.INVALID_SESSION_PLEASE_RE_LOGIN')  | 
            ||
| 133 | else:  | 
            ||
| 134 | utc_expires = row[0]  | 
            ||
| 135 | if datetime.utcnow() > utc_expires:  | 
            ||
| 136 | if cursor:  | 
            ||
| 137 | cursor.close()  | 
            ||
| 138 | if cnx:  | 
            ||
| 139 | cnx.disconnect()  | 
            ||
| 140 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 141 | description='API.USER_SESSION_TIMEOUT')  | 
            ||
| 142 | |||
| 143 |         cursor.execute(" SELECT id " | 
            ||
| 144 | " FROM tbl_users "  | 
            ||
| 145 | " WHERE uuid = %s ",  | 
            ||
| 146 | (user_uuid,))  | 
            ||
| 147 | row = cursor.fetchone()  | 
            ||
| 148 | if row is None:  | 
            ||
| 149 | if cursor:  | 
            ||
| 150 | cursor.close()  | 
            ||
| 151 | if cnx:  | 
            ||
| 152 | cnx.disconnect()  | 
            ||
| 153 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 154 | description='API.INVALID_USER_PLEASE_RE_LOGIN')  | 
            ||
| 155 | else:  | 
            ||
| 156 | user_id = row[0]  | 
            ||
| 157 | |||
| 158 | cnx = mysql.connector.connect(**config.myems_system_db)  | 
            ||
| 159 | cursor = cnx.cursor()  | 
            ||
| 160 | |||
| 161 |         add_values = (" INSERT INTO tbl_knowledge_files " | 
            ||
| 162 | " (file_name, uuid, upload_datetime_utc, upload_user_uuid, file_object ) "  | 
            ||
| 163 | " VALUES (%s, %s, %s, %s, %s) ")  | 
            ||
| 164 | cursor.execute(add_values, (filename,  | 
            ||
| 165 | file_uuid,  | 
            ||
| 166 | datetime.utcnow(),  | 
            ||
| 167 | user_uuid,  | 
            ||
| 168 | raw_blob))  | 
            ||
| 169 | new_id = cursor.lastrowid  | 
            ||
| 170 | cnx.commit()  | 
            ||
| 171 | cursor.close()  | 
            ||
| 172 | cnx.disconnect()  | 
            ||
| 173 | |||
| 174 | resp.status = falcon.HTTP_201  | 
            ||
| 175 | resp.location = '/knowledgefiles/' + str(new_id)  | 
            ||
| 176 | |||
| 330 |