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