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