| Conditions | 26 |
| Total Lines | 111 |
| Code Lines | 87 |
| 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.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 base64 |
||
| 223 | @staticmethod |
||
| 224 | @user_logger |
||
| 225 | def on_put(req, resp, id_): |
||
| 226 | """Handles PUT requests""" |
||
| 227 | admin_control(req) |
||
| 228 | try: |
||
| 229 | raw_json = req.stream.read().decode('utf-8') |
||
| 230 | except Exception as ex: |
||
| 231 | print(str(ex)) |
||
| 232 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 233 | title='API.BAD_REQUEST', |
||
| 234 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 235 | |||
| 236 | if not id_.isdigit() or int(id_) <= 0: |
||
| 237 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 238 | description='API.INVALID_EMAIL_SERVER_ID') |
||
| 239 | |||
| 240 | new_values = json.loads(raw_json) |
||
| 241 | if 'host' not in new_values['data'].keys() or \ |
||
| 242 | not isinstance(new_values['data']['host'], str) or \ |
||
| 243 | len(str.strip(new_values['data']['host'])) == 0: |
||
| 244 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 245 | description='API.INVALID_EMAIL_SERVER_HOST') |
||
| 246 | |||
| 247 | host = str.strip(new_values['data']['host']) |
||
| 248 | |||
| 249 | if 'port' not in new_values['data'].keys() or \ |
||
| 250 | not isinstance(new_values['data']['port'], int) or \ |
||
| 251 | new_values['data']['port'] <= 0: |
||
| 252 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 253 | description='API.INVALID_PORT') |
||
| 254 | port = float(new_values['data']['port']) |
||
| 255 | |||
| 256 | if 'requires_authentication' not in new_values['data'].keys() or \ |
||
| 257 | not isinstance(new_values['data']['requires_authentication'], bool): |
||
| 258 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 259 | description='API.INVALID_REQUIRES_AUTHENTICATION') |
||
| 260 | requires_authentication = new_values['data']['requires_authentication'] |
||
| 261 | |||
| 262 | if requires_authentication: |
||
| 263 | if 'user_name' not in new_values['data'].keys() or \ |
||
| 264 | not isinstance(new_values['data']['user_name'], str) or \ |
||
| 265 | len(str.strip(new_values['data']['user_name'])) == 0: |
||
| 266 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 267 | description='API.INVALID_USER_NAME') |
||
| 268 | user_name = new_values['data']['user_name'] |
||
| 269 | else: |
||
| 270 | user_name = None |
||
| 271 | |||
| 272 | if requires_authentication: |
||
| 273 | if 'password' not in new_values['data'].keys() or \ |
||
| 274 | not isinstance(new_values['data']['password'], str) or \ |
||
| 275 | len(str.strip(new_values['data']['password'])) == 0: |
||
| 276 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 277 | description='API.INVALID_PASSWORD') |
||
| 278 | password = base64.b64encode(bytearray(new_values['data']['password'], 'utf-8')) |
||
| 279 | else: |
||
| 280 | password = None |
||
| 281 | |||
| 282 | if 'from_addr' not in new_values['data'].keys() or \ |
||
| 283 | not isinstance(new_values['data']['from_addr'], str) or \ |
||
| 284 | len(str.strip(new_values['data']['from_addr'])) == 0: |
||
| 285 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 286 | description='API.INVALID_FROM_ADDR') |
||
| 287 | from_addr = new_values['data']['from_addr'] |
||
| 288 | |||
| 289 | match = re.match(r'^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,4})$', |
||
| 290 | from_addr) |
||
| 291 | if match is None: |
||
| 292 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 293 | description='API.INVALID_FROM_ADDR') |
||
| 294 | |||
| 295 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 296 | cursor = cnx.cursor() |
||
| 297 | |||
| 298 | cursor.execute(" SELECT id " |
||
| 299 | " FROM tbl_email_servers " |
||
| 300 | " WHERE id = %s ", |
||
| 301 | (id_,)) |
||
| 302 | if cursor.fetchone() is None: |
||
| 303 | cursor.close() |
||
| 304 | cnx.close() |
||
| 305 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 306 | description='API.EMAIL_SERVER_NOT_FOUND') |
||
| 307 | |||
| 308 | cursor.execute(" SELECT host " |
||
| 309 | " FROM tbl_email_servers " |
||
| 310 | " WHERE host = %s AND id != %s ", (host, id_)) |
||
| 311 | if cursor.fetchone() is not None: |
||
| 312 | cursor.close() |
||
| 313 | cnx.close() |
||
| 314 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 315 | description='API.EMAIL_SERVER_HOST_IS_ALREADY_IN_USE') |
||
| 316 | |||
| 317 | update_row = (" UPDATE tbl_email_servers " |
||
| 318 | " SET host = %s, port = %s, requires_authentication = %s, " |
||
| 319 | " user_name = %s, password = %s, from_addr = %s " |
||
| 320 | " WHERE id = %s ") |
||
| 321 | cursor.execute(update_row, (host, |
||
| 322 | port, |
||
| 323 | requires_authentication, |
||
| 324 | user_name, |
||
| 325 | password, |
||
| 326 | from_addr, |
||
| 327 | id_,)) |
||
| 328 | cnx.commit() |
||
| 329 | |||
| 330 | cursor.close() |
||
| 331 | cnx.close() |
||
| 332 | |||
| 333 | resp.status = falcon.HTTP_200 |
||
| 334 |