Conditions | 26 |
Total Lines | 105 |
Code Lines | 81 |
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 emailserver.EmailServerItem.on_put() 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 |
||
201 | @staticmethod |
||
202 | def on_put(req, resp, id_): |
||
203 | """Handles PUT requests""" |
||
204 | try: |
||
205 | raw_json = req.stream.read().decode('utf-8') |
||
206 | except Exception as ex: |
||
207 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
208 | |||
209 | if not id_.isdigit() or int(id_) <= 0: |
||
210 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
211 | description='API.INVALID_EMAIL_SERVER_ID') |
||
212 | |||
213 | new_values = json.loads(raw_json, encoding='utf-8') |
||
214 | if 'host' not in new_values['data'].keys() or \ |
||
215 | not isinstance(new_values['data']['host'], str) or \ |
||
216 | len(str.strip(new_values['data']['host'])) == 0: |
||
217 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
218 | description='API.INVALID_EMAIL_SERVER_HOST') |
||
219 | |||
220 | host = str.strip(new_values['data']['host']) |
||
221 | |||
222 | if 'port' not in new_values['data'].keys() or \ |
||
223 | not isinstance(new_values['data']['port'], int) or \ |
||
224 | new_values['data']['port'] <= 0: |
||
225 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
226 | description='API.INVALID_PORT') |
||
227 | port = float(new_values['data']['port']) |
||
228 | |||
229 | if 'requires_authentication' not in new_values['data'].keys() or \ |
||
230 | not isinstance(new_values['data']['requires_authentication'], bool): |
||
231 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
232 | description='API.INVALID_REQUIRES_AUTHENTICATION') |
||
233 | requires_authentication = new_values['data']['requires_authentication'] |
||
234 | |||
235 | if requires_authentication: |
||
236 | if 'user_name' not in new_values['data'].keys() or \ |
||
237 | not isinstance(new_values['data']['user_name'], str) or \ |
||
238 | len(str.strip(new_values['data']['user_name'])) == 0: |
||
239 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
240 | description='API.INVALID_USER_NAME') |
||
241 | user_name = new_values['data']['user_name'] |
||
242 | else: |
||
243 | user_name = None |
||
244 | |||
245 | if requires_authentication: |
||
246 | if 'password' not in new_values['data'].keys() or \ |
||
247 | not isinstance(new_values['data']['password'], str) or \ |
||
248 | len(str.strip(new_values['data']['password'])) == 0: |
||
249 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
250 | description='API.INVALID_PASSWORD') |
||
251 | password = base64.b64encode(bytearray(new_values['data']['password'], 'utf-8')) |
||
252 | else: |
||
253 | password = None |
||
254 | |||
255 | if 'from_addr' not in new_values['data'].keys() or \ |
||
256 | not isinstance(new_values['data']['from_addr'], str) or \ |
||
257 | len(str.strip(new_values['data']['from_addr'])) == 0: |
||
258 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
259 | description='API.INVALID_FROM_ADDR') |
||
260 | from_addr = new_values['data']['from_addr'] |
||
261 | |||
262 | match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', from_addr) |
||
263 | if match is None: |
||
264 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
265 | description='API.INVALID_FROM_ADDR') |
||
266 | |||
267 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
268 | cursor = cnx.cursor() |
||
269 | |||
270 | cursor.execute(" SELECT id " |
||
271 | " FROM tbl_email_servers " |
||
272 | " WHERE id = %s ", |
||
273 | (id_,)) |
||
274 | if cursor.fetchone() is None: |
||
275 | cursor.close() |
||
276 | cnx.disconnect() |
||
277 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
278 | description='API.EMAIL_SERVER_NOT_FOUND') |
||
279 | |||
280 | cursor.execute(" SELECT host " |
||
281 | " FROM tbl_email_servers " |
||
282 | " WHERE host = %s AND id != %s ", (host, id_)) |
||
283 | if cursor.fetchone() is not None: |
||
284 | cursor.close() |
||
285 | cnx.disconnect() |
||
286 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
287 | description='API.EMAIL_SERVER_HOST_IS_ALREADY_IN_USE') |
||
288 | |||
289 | update_row = (" UPDATE tbl_email_servers " |
||
290 | " SET host = %s, port = %s, requires_authentication = %s, " |
||
291 | " user_name = %s, password = %s, from_addr = %s " |
||
292 | " WHERE id = %s ") |
||
293 | cursor.execute(update_row, (host, |
||
294 | port, |
||
295 | requires_authentication, |
||
296 | user_name, |
||
297 | password, |
||
298 | from_addr, |
||
299 | id_,)) |
||
300 | cnx.commit() |
||
301 | |||
302 | cursor.close() |
||
303 | cnx.disconnect() |
||
304 | |||
305 | resp.status = falcon.HTTP_200 |
||
306 |