| Conditions | 21 |
| Total Lines | 87 |
| Code Lines | 70 |
| 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 rule.RuleItem.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 |
||
| 179 | @staticmethod |
||
| 180 | def on_put(req, resp, id_): |
||
| 181 | """Handles PUT requests""" |
||
| 182 | try: |
||
| 183 | raw_json = req.stream.read().decode('utf-8') |
||
| 184 | except Exception as ex: |
||
| 185 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
| 186 | |||
| 187 | if not id_.isdigit() or int(id_) <= 0: |
||
| 188 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 189 | description='API.INVALID_RULE_ID') |
||
| 190 | |||
| 191 | new_values = json.loads(raw_json, encoding='utf-8') |
||
| 192 | if 'name' not in new_values['data'].keys() or \ |
||
| 193 | not isinstance(new_values['data']['name'], str) or \ |
||
| 194 | len(str.strip(new_values['data']['name'])) == 0: |
||
| 195 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 196 | description='API.INVALID_RULE_NAME') |
||
| 197 | name = str.strip(new_values['data']['name']) |
||
| 198 | |||
| 199 | if 'channel' not in new_values['data'].keys() or \ |
||
| 200 | not isinstance(new_values['data']['channel'], str) or \ |
||
| 201 | len(str.strip(new_values['data']['channel'])) == 0 or \ |
||
| 202 | str.strip(new_values['data']['channel']) not in ('call', 'sms', 'email', 'wechat', 'web'): |
||
| 203 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 204 | title='API.BAD_REQUEST', |
||
| 205 | description='API.INVALID_CHANNEL') |
||
| 206 | channel = str.strip(new_values['data']['channel']) |
||
| 207 | |||
| 208 | if 'expression' not in new_values['data'].keys() or \ |
||
| 209 | not isinstance(new_values['data']['expression'], str) or \ |
||
| 210 | len(str.strip(new_values['data']['expression'])) == 0: |
||
| 211 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 212 | title='API.BAD_REQUEST', |
||
| 213 | description='API.INVALID_EXPRESSION') |
||
| 214 | expression = str.strip(new_values['data']['expression']) |
||
| 215 | |||
| 216 | if 'message' not in new_values['data'].keys() or \ |
||
| 217 | not isinstance(new_values['data']['message'], str) or \ |
||
| 218 | len(str.strip(new_values['data']['message'])) == 0: |
||
| 219 | raise falcon.HTTPError(falcon.HTTP_400, |
||
| 220 | title='API.BAD_REQUEST', |
||
| 221 | description='API.INVALID_MESSAGE') |
||
| 222 | message = str.strip(new_values['data']['message']) |
||
| 223 | |||
| 224 | if 'is_enabled' not in new_values['data'].keys() or \ |
||
| 225 | not isinstance(new_values['data']['is_enabled'], bool): |
||
| 226 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 227 | description='API.INVALID_IS_ENABLED') |
||
| 228 | is_enabled = new_values['data']['is_enabled'] |
||
| 229 | |||
| 230 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
| 231 | cursor = cnx.cursor() |
||
| 232 | |||
| 233 | cursor.execute(" SELECT id " |
||
| 234 | " FROM tbl_rules " |
||
| 235 | " WHERE id = %s ", (id_,)) |
||
| 236 | if cursor.fetchone() is None: |
||
| 237 | cursor.close() |
||
| 238 | cnx.disconnect() |
||
| 239 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
| 240 | description='API.RULE_NOT_FOUND') |
||
| 241 | |||
| 242 | cursor.execute(" SELECT name " |
||
| 243 | " FROM tbl_rules " |
||
| 244 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
| 245 | if cursor.fetchone() is not None: |
||
| 246 | cursor.close() |
||
| 247 | cnx.disconnect() |
||
| 248 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
| 249 | description='API.RULE_NAME_IS_ALREADY_IN_USE') |
||
| 250 | |||
| 251 | update_row = (" UPDATE tbl_rules " |
||
| 252 | " SET name = %s, channel = %s, expression = %s, message = %s, is_enabled = %s " |
||
| 253 | " WHERE id = %s ") |
||
| 254 | cursor.execute(update_row, (name, |
||
| 255 | channel, |
||
| 256 | expression, |
||
| 257 | message, |
||
| 258 | is_enabled, |
||
| 259 | id_,)) |
||
| 260 | cnx.commit() |
||
| 261 | |||
| 262 | cursor.close() |
||
| 263 | cnx.disconnect() |
||
| 264 | |||
| 265 | resp.status = falcon.HTTP_200 |
||
| 266 |