| Conditions | 29 |
| Total Lines | 131 |
| Code Lines | 103 |
| Lines | 35 |
| Ratio | 26.72 % |
| 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.textmessage.TextMessageCollection.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 |
||
| 91 | @staticmethod |
||
| 92 | @user_logger |
||
| 93 | def on_post(req, resp): |
||
| 94 | """Handles POST requests""" |
||
| 95 | access_control(req) |
||
| 96 | |||
| 97 | try: |
||
| 98 | raw_json = req.stream.read().decode('utf-8') |
||
| 99 | except Exception as ex: |
||
| 100 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
| 101 | |||
| 102 | new_values = json.loads(raw_json) |
||
| 103 | |||
| 104 | if 'rule_id' in new_values['data'].keys(): |
||
| 105 | if new_values['data']['rule_id'] <= 0: |
||
| 106 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 107 | description='API.INVALID_RULE_ID') |
||
| 108 | rule_id = new_values['data']['rule_id'] |
||
| 109 | else: |
||
| 110 | rule_id = None |
||
| 111 | |||
| 112 | if 'recipient_name' not in new_values['data'].keys() or \ |
||
| 113 | not isinstance(new_values['data']['recipient_name'], str) or \ |
||
| 114 | len(str.strip(new_values['data']['recipient_name'])) == 0: |
||
| 115 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 116 | description='API.INVALID_RECIPIENT_NAME') |
||
| 117 | recipient_name = str.strip(new_values['data']['recipient_name']) |
||
| 118 | |||
| 119 | if 'recipient_mobile' not in new_values['data'].keys() or \ |
||
| 120 | not isinstance(new_values['data']['recipient_mobile'], str) or \ |
||
| 121 | len(str.strip(new_values['data']['recipient_mobile'])) == 0: |
||
| 122 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 123 | description='API.INVALID_RECIPIENT_MOBILE') |
||
| 124 | recipient_mobile = str.strip(new_values['data']['recipient_mobile']) |
||
| 125 | |||
| 126 | if 'message' not in new_values['data'].keys() or \ |
||
| 127 | not isinstance(new_values['data']['message'], str) or \ |
||
| 128 | len(str.strip(new_values['data']['message'])) == 0: |
||
| 129 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 130 | description='API.INVALID_MESSAGE_VALUE') |
||
| 131 | message = str.strip(new_values['data']['message']) |
||
| 132 | |||
| 133 | if 'acknowledge_code' not in new_values['data'].keys() or \ |
||
| 134 | not isinstance(new_values['data']['acknowledge_code'], str) or \ |
||
| 135 | len(str.strip(new_values['data']['acknowledge_code'])) == 0: |
||
| 136 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 137 | description='API.INVALID_ACKNOWLEDGE_CODE') |
||
| 138 | acknowledge_code = str.strip(new_values['data']['acknowledge_code']) |
||
| 139 | |||
| 140 | if 'created_datetime' not in new_values['data'].keys() or \ |
||
| 141 | not isinstance(new_values['data']['created_datetime'], str) or \ |
||
| 142 | len(str.strip(new_values['data']['created_datetime'])) == 0: |
||
| 143 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 144 | description='API.INVALID_CREATED_DATETIME') |
||
| 145 | created_datetime_local = str.strip(new_values['data']['created_datetime']) |
||
| 146 | |||
| 147 | if 'scheduled_datetime' not in new_values['data'].keys() or \ |
||
| 148 | not isinstance(new_values['data']['scheduled_datetime'], str) or \ |
||
| 149 | len(str.strip(new_values['data']['scheduled_datetime'])) == 0: |
||
| 150 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 151 | description='API.INVALID_SCHEDULED_DATETIME') |
||
| 152 | scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime']) |
||
| 153 | |||
| 154 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 155 | if config.utc_offset[0] == '-': |
||
| 156 | timezone_offset = -timezone_offset |
||
| 157 | |||
| 158 | View Code Duplication | if created_datetime_local is None: |
|
|
|
|||
| 159 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 160 | description="API.INVALID_CREATED_DATETIME") |
||
| 161 | else: |
||
| 162 | created_datetime_local = str.strip(created_datetime_local) |
||
| 163 | try: |
||
| 164 | created_datetime_utc = datetime.strptime(created_datetime_local, |
||
| 165 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 166 | timedelta(minutes=timezone_offset) |
||
| 167 | except ValueError: |
||
| 168 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 169 | description="API.INVALID_CREATED_DATETIME") |
||
| 170 | |||
| 171 | View Code Duplication | if scheduled_datetime_local is None: |
|
| 172 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 173 | description="API.INVALID_SCHEDULED_DATETIME") |
||
| 174 | else: |
||
| 175 | scheduled_datetime_local = str.strip(scheduled_datetime_local) |
||
| 176 | try: |
||
| 177 | scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local, |
||
| 178 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 179 | timedelta(minutes=timezone_offset) |
||
| 180 | except ValueError: |
||
| 181 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 182 | description="API.INVALID_SCHEDULED_DATETIME") |
||
| 183 | |||
| 184 | status = 'new' |
||
| 185 | |||
| 186 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 187 | cursor = cnx.cursor() |
||
| 188 | |||
| 189 | View Code Duplication | if rule_id is not None: |
|
| 190 | cursor.execute(" SELECT name " |
||
| 191 | " FROM tbl_rules " |
||
| 192 | " WHERE id = %s ", |
||
| 193 | (new_values['data']['rule_id'],)) |
||
| 194 | row = cursor.fetchone() |
||
| 195 | if row is None: |
||
| 196 | cursor.close() |
||
| 197 | cnx.disconnect() |
||
| 198 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 199 | description='API.RULE_NOT_FOUND') |
||
| 200 | |||
| 201 | add_row = (" INSERT INTO tbl_text_messages_outbox" |
||
| 202 | " (rule_id, recipient_name, recipient_mobile, message, " |
||
| 203 | " acknowledge_code, created_datetime_utc," |
||
| 204 | " scheduled_datetime_utc, status) " |
||
| 205 | " VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ") |
||
| 206 | |||
| 207 | cursor.execute(add_row, (rule_id, |
||
| 208 | recipient_name, |
||
| 209 | recipient_mobile, |
||
| 210 | message, |
||
| 211 | acknowledge_code, |
||
| 212 | created_datetime_utc, |
||
| 213 | scheduled_datetime_utc, |
||
| 214 | status)) |
||
| 215 | new_id = cursor.lastrowid |
||
| 216 | cnx.commit() |
||
| 217 | cursor.close() |
||
| 218 | cnx.disconnect() |
||
| 219 | |||
| 220 | resp.status = falcon.HTTP_201 |
||
| 221 | resp.location = '/textmessages/' + str(new_id) |
||
| 222 | |||
| 302 |