| Conditions | 23 |
| Total Lines | 97 |
| Code Lines | 78 |
| 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.EmailServerCollection.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 base64 |
||
| 56 | @staticmethod |
||
| 57 | @user_logger |
||
| 58 | def on_post(req, resp): |
||
| 59 | """Handles POST requests""" |
||
| 60 | admin_control(req) |
||
| 61 | try: |
||
| 62 | raw_json = req.stream.read().decode('utf-8') |
||
| 63 | except Exception as ex: |
||
| 64 | print(ex) |
||
| 65 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 66 | title='API.BAD_REQUEST', |
||
| 67 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
| 68 | |||
| 69 | new_values = json.loads(raw_json) |
||
| 70 | |||
| 71 | if 'host' not in new_values['data'].keys() or \ |
||
| 72 | not isinstance(new_values['data']['host'], str) or \ |
||
| 73 | len(str.strip(new_values['data']['host'])) == 0: |
||
| 74 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 75 | description='API.INVALID_EMAIL_SERVER_HOST') |
||
| 76 | |||
| 77 | host = str.strip(new_values['data']['host']) |
||
| 78 | |||
| 79 | if 'port' not in new_values['data'].keys() or \ |
||
| 80 | not isinstance(new_values['data']['port'], int) or \ |
||
| 81 | new_values['data']['port'] <= 0: |
||
| 82 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 83 | description='API.INVALID_PORT') |
||
| 84 | port = float(new_values['data']['port']) |
||
| 85 | |||
| 86 | if 'requires_authentication' not in new_values['data'].keys() or \ |
||
| 87 | not isinstance(new_values['data']['requires_authentication'], bool): |
||
| 88 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 89 | description='API.INVALID_REQUIRES_AUTHENTICATION') |
||
| 90 | requires_authentication = new_values['data']['requires_authentication'] |
||
| 91 | |||
| 92 | if requires_authentication: |
||
| 93 | if 'user_name' not in new_values['data'].keys() or \ |
||
| 94 | not isinstance(new_values['data']['user_name'], str) or \ |
||
| 95 | len(str.strip(new_values['data']['user_name'])) == 0: |
||
| 96 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 97 | description='API.INVALID_USER_NAME') |
||
| 98 | user_name = new_values['data']['user_name'] |
||
| 99 | else: |
||
| 100 | user_name = None |
||
| 101 | |||
| 102 | if requires_authentication: |
||
| 103 | if 'password' not in new_values['data'].keys() or \ |
||
| 104 | not isinstance(new_values['data']['password'], str) or \ |
||
| 105 | len(str.strip(new_values['data']['password'])) == 0: |
||
| 106 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 107 | description='API.INVALID_PASSWORD') |
||
| 108 | password = base64.b64encode(bytearray(new_values['data']['password'], 'utf-8')) |
||
| 109 | else: |
||
| 110 | password = None |
||
| 111 | |||
| 112 | if 'from_addr' not in new_values['data'].keys() or \ |
||
| 113 | not isinstance(new_values['data']['from_addr'], str) or \ |
||
| 114 | len(str.strip(new_values['data']['from_addr'])) == 0: |
||
| 115 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 116 | description='API.INVALID_FROM_ADDR') |
||
| 117 | from_addr = new_values['data']['from_addr'] |
||
| 118 | |||
| 119 | 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})$', |
||
| 120 | from_addr) |
||
| 121 | if match is None: |
||
| 122 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 123 | description='API.INVALID_FROM_ADDR') |
||
| 124 | |||
| 125 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 126 | cursor = cnx.cursor() |
||
| 127 | |||
| 128 | cursor.execute(" SELECT host " |
||
| 129 | " FROM tbl_email_servers " |
||
| 130 | " WHERE host = %s ", (host,)) |
||
| 131 | if cursor.fetchone() is not None: |
||
| 132 | cursor.close() |
||
| 133 | cnx.close() |
||
| 134 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 135 | description='API.EMAIL_SERVER_HOST_IS_ALREADY_IN_USE') |
||
| 136 | |||
| 137 | add_value = (" INSERT INTO tbl_email_servers " |
||
| 138 | " (host, port, requires_authentication, user_name, password, from_addr) " |
||
| 139 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
| 140 | cursor.execute(add_value, (host, |
||
| 141 | port, |
||
| 142 | requires_authentication, |
||
| 143 | user_name, |
||
| 144 | password, |
||
| 145 | from_addr)) |
||
| 146 | new_id = cursor.lastrowid |
||
| 147 | cnx.commit() |
||
| 148 | cursor.close() |
||
| 149 | cnx.close() |
||
| 150 | |||
| 151 | resp.status = falcon.HTTP_201 |
||
| 152 | resp.location = '/emailservers/' + str(new_id) |
||
| 153 | |||
| 334 |