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