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