| Conditions | 31 |
| Total Lines | 145 |
| Code Lines | 116 |
| Lines | 35 |
| Ratio | 24.14 % |
| 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.emailmessage.EmailMessageCollection.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 |
||
| 96 | @staticmethod |
||
| 97 | @user_logger |
||
| 98 | def on_post(req, resp): |
||
| 99 | """Handles POST requests""" |
||
| 100 | access_control(req) |
||
| 101 | try: |
||
| 102 | upload = req.get_param('file') |
||
| 103 | # Read upload file as binary |
||
| 104 | attachment_file_object = upload.file.read() |
||
| 105 | # Retrieve filename |
||
| 106 | attachment_file_name = upload.filename |
||
| 107 | except Exception as ex: |
||
| 108 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', |
||
| 109 | description='API.FAILED_TO_UPLOAD_ATTACHMENT_FILE') |
||
| 110 | try: |
||
| 111 | raw_json = req.get_param('req') |
||
| 112 | except Exception as ex: |
||
| 113 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 114 | |||
| 115 | new_values = json.loads(raw_json) |
||
| 116 | |||
| 117 | if 'rule_id' in new_values['data'].keys(): |
||
| 118 | if new_values['data']['rule_id'] <= 0: |
||
| 119 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 120 | description='API.INVALID_RULE_ID') |
||
| 121 | rule_id = new_values['data']['rule_id'] |
||
| 122 | else: |
||
| 123 | rule_id = None |
||
| 124 | |||
| 125 | if 'recipient_name' not in new_values['data'].keys() or \ |
||
| 126 | not isinstance(new_values['data']['recipient_name'], str) or \ |
||
| 127 | len(str.strip(new_values['data']['recipient_name'])) == 0: |
||
| 128 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 129 | description='API.INVALID_RECIPIENT_NAME') |
||
| 130 | recipient_name = str.strip(new_values['data']['recipient_name']) |
||
| 131 | |||
| 132 | if 'recipient_email' not in new_values['data'].keys() or \ |
||
| 133 | not isinstance(new_values['data']['recipient_email'], str) or \ |
||
| 134 | len(str.strip(new_values['data']['recipient_email'])) == 0: |
||
| 135 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 136 | description='API.INVALID_RECIPIENT_EMAIL') |
||
| 137 | recipient_email = str.strip(new_values['data']['recipient_email']) |
||
| 138 | match = re.match(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', recipient_email) |
||
| 139 | if match is None: |
||
| 140 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 141 | description='API.INVALID_EMAIL') |
||
| 142 | |||
| 143 | if 'subject' not in new_values['data'].keys() or \ |
||
| 144 | not isinstance(new_values['data']['subject'], str) or \ |
||
| 145 | len(str.strip(new_values['data']['subject'])) == 0: |
||
| 146 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 147 | description='API.INVALID_SUBJECT_VALUE') |
||
| 148 | subject = str.strip(new_values['data']['subject']) |
||
| 149 | |||
| 150 | if 'message' not in new_values['data'].keys() or \ |
||
| 151 | not isinstance(new_values['data']['message'], str) or \ |
||
| 152 | len(str.strip(new_values['data']['message'])) == 0: |
||
| 153 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 154 | description='API.INVALID_MESSAGE_VALUE') |
||
| 155 | message = str.strip(new_values['data']['message']) |
||
| 156 | |||
| 157 | if 'created_datetime' not in new_values['data'].keys() or \ |
||
| 158 | not isinstance(new_values['data']['created_datetime'], str) or \ |
||
| 159 | len(str.strip(new_values['data']['created_datetime'])) == 0: |
||
| 160 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 161 | description='API.INVALID_CREATED_DATETIME') |
||
| 162 | created_datetime_local = str.strip(new_values['data']['created_datetime']) |
||
| 163 | |||
| 164 | if 'scheduled_datetime' not in new_values['data'].keys() or \ |
||
| 165 | not isinstance(new_values['data']['scheduled_datetime'], str) or \ |
||
| 166 | len(str.strip(new_values['data']['scheduled_datetime'])) == 0: |
||
| 167 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 168 | description='API.INVALID_SCHEDULED_DATETIME') |
||
| 169 | scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime']) |
||
| 170 | |||
| 171 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 172 | if config.utc_offset[0] == '-': |
||
| 173 | timezone_offset = -timezone_offset |
||
| 174 | |||
| 175 | View Code Duplication | if created_datetime_local is None: |
|
| 176 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 177 | description="API.INVALID_CREATED_DATETIME") |
||
| 178 | else: |
||
| 179 | created_datetime_local = str.strip(created_datetime_local) |
||
| 180 | try: |
||
| 181 | created_datetime_utc = datetime.strptime(created_datetime_local, |
||
| 182 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 183 | timedelta(minutes=timezone_offset) |
||
| 184 | except ValueError: |
||
| 185 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 186 | description="API.INVALID_CREATED_DATETIME") |
||
| 187 | |||
| 188 | View Code Duplication | if scheduled_datetime_local is None: |
|
| 189 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 190 | description="API.INVALID_SCHEDULED_DATETIME") |
||
| 191 | else: |
||
| 192 | scheduled_datetime_local = str.strip(scheduled_datetime_local) |
||
| 193 | try: |
||
| 194 | scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local, |
||
| 195 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 196 | timedelta(minutes=timezone_offset) |
||
| 197 | except ValueError: |
||
| 198 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 199 | description="API.INVALID_SCHEDULED_DATETIME") |
||
| 200 | |||
| 201 | status = 'new' |
||
| 202 | |||
| 203 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 204 | cursor = cnx.cursor() |
||
| 205 | |||
| 206 | View Code Duplication | if rule_id is not None: |
|
| 207 | cursor.execute(" SELECT name " |
||
| 208 | " FROM tbl_rules " |
||
| 209 | " WHERE id = %s ", |
||
| 210 | (new_values['data']['rule_id'],)) |
||
| 211 | row = cursor.fetchone() |
||
| 212 | if row is None: |
||
| 213 | cursor.close() |
||
| 214 | cnx.disconnect() |
||
| 215 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 216 | description='API.RULE_NOT_FOUND') |
||
| 217 | |||
| 218 | add_row = (" INSERT INTO tbl_email_messages " |
||
| 219 | " (rule_id, recipient_name, recipient_email, subject, message, " |
||
| 220 | " attachment_file_name, attachment_file_object, created_datetime_utc," |
||
| 221 | " scheduled_datetime_utc, status) " |
||
| 222 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 223 | |||
| 224 | cursor.execute(add_row, (rule_id, |
||
| 225 | recipient_name, |
||
| 226 | recipient_email, |
||
| 227 | subject, |
||
| 228 | message, |
||
| 229 | attachment_file_name, |
||
| 230 | attachment_file_object, |
||
| 231 | created_datetime_utc, |
||
| 232 | scheduled_datetime_utc, |
||
| 233 | status)) |
||
| 234 | new_id = cursor.lastrowid |
||
| 235 | cnx.commit() |
||
| 236 | cursor.close() |
||
| 237 | cnx.disconnect() |
||
| 238 | |||
| 239 | resp.status = falcon.HTTP_201 |
||
| 240 | resp.location = '/emailmessages/' + str(new_id) |
||
| 241 | |||
| 325 |