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