Conditions | 32 |
Total Lines | 120 |
Code Lines | 98 |
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.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 |
||
219 | @staticmethod |
||
220 | def on_put(req, resp, id_): |
||
221 | """Handles PUT requests""" |
||
222 | try: |
||
223 | raw_json = req.stream.read().decode('utf-8') |
||
224 | except Exception as ex: |
||
225 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
226 | |||
227 | if not id_.isdigit() or int(id_) <= 0: |
||
228 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
229 | description='API.INVALID_RULE_ID') |
||
230 | |||
231 | new_values = json.loads(raw_json) |
||
232 | if 'name' not in new_values['data'].keys() or \ |
||
233 | not isinstance(new_values['data']['name'], str) or \ |
||
234 | len(str.strip(new_values['data']['name'])) == 0: |
||
235 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
236 | description='API.INVALID_RULE_NAME') |
||
237 | name = str.strip(new_values['data']['name']) |
||
238 | |||
239 | if 'fdd_code' not in new_values['data'].keys() or \ |
||
240 | not isinstance(new_values['data']['fdd_code'], str) or \ |
||
241 | len(str.strip(new_values['data']['fdd_code'])) == 0: |
||
242 | raise falcon.HTTPError(falcon.HTTP_400, |
||
243 | title='API.BAD_REQUEST', |
||
244 | description='API.INVALID_FDD_CODE') |
||
245 | fdd_code = str.strip(new_values['data']['fdd_code']) |
||
246 | |||
247 | if 'category' not in new_values['data'].keys() or \ |
||
248 | not isinstance(new_values['data']['category'], str) or \ |
||
249 | len(str.strip(new_values['data']['category'])) == 0 or \ |
||
250 | str.strip(new_values['data']['category']) not in \ |
||
251 | ('SYSTEM', 'SPACE', 'METER', 'TENANT', 'STORE', 'SHOPFLOOR', 'EQUIPMENT', 'COMBINEDEQUIPMENT'): |
||
252 | raise falcon.HTTPError(falcon.HTTP_400, |
||
253 | title='API.BAD_REQUEST', |
||
254 | description='API.INVALID_CATEGORY') |
||
255 | category = str.strip(new_values['data']['category']) |
||
256 | |||
257 | if 'priority' not in new_values['data'].keys() or \ |
||
258 | not isinstance(new_values['data']['priority'], str) or \ |
||
259 | len(str.strip(new_values['data']['priority'])) == 0 or \ |
||
260 | str.strip(new_values['data']['priority']) not in \ |
||
261 | ('CRITICAL', 'HIGH', 'MEDIUM', 'LOW'): |
||
262 | raise falcon.HTTPError(falcon.HTTP_400, |
||
263 | title='API.BAD_REQUEST', |
||
264 | description='API.INVALID_PRIORITY') |
||
265 | priority = str.strip(new_values['data']['priority']) |
||
266 | |||
267 | if 'channel' not in new_values['data'].keys() or \ |
||
268 | not isinstance(new_values['data']['channel'], str) or \ |
||
269 | len(str.strip(new_values['data']['channel'])) == 0 or \ |
||
270 | str.strip(new_values['data']['channel']) not in ('WEB', 'EMAIL', 'SMS', 'WECHAT', 'CALL'): |
||
271 | raise falcon.HTTPError(falcon.HTTP_400, |
||
272 | title='API.BAD_REQUEST', |
||
273 | description='API.INVALID_CHANNEL') |
||
274 | channel = str.strip(new_values['data']['channel']) |
||
275 | |||
276 | if 'expression' not in new_values['data'].keys() or \ |
||
277 | not isinstance(new_values['data']['expression'], str) or \ |
||
278 | len(str.strip(new_values['data']['expression'])) == 0: |
||
279 | raise falcon.HTTPError(falcon.HTTP_400, |
||
280 | title='API.BAD_REQUEST', |
||
281 | description='API.INVALID_EXPRESSION') |
||
282 | expression = str.strip(new_values['data']['expression']) |
||
283 | |||
284 | if 'message_template' not in new_values['data'].keys() or \ |
||
285 | not isinstance(new_values['data']['message_template'], str) or \ |
||
286 | len(str.strip(new_values['data']['message_template'])) == 0: |
||
287 | raise falcon.HTTPError(falcon.HTTP_400, |
||
288 | title='API.BAD_REQUEST', |
||
289 | description='API.INVALID_MESSAGE_TEMPLATE') |
||
290 | message_template = str.strip(new_values['data']['message_template']) |
||
291 | |||
292 | if 'is_enabled' not in new_values['data'].keys() or \ |
||
293 | not isinstance(new_values['data']['is_enabled'], bool): |
||
294 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
295 | description='API.INVALID_IS_ENABLED') |
||
296 | is_enabled = new_values['data']['is_enabled'] |
||
297 | |||
298 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
299 | cursor = cnx.cursor() |
||
300 | |||
301 | cursor.execute(" SELECT id " |
||
302 | " FROM tbl_rules " |
||
303 | " WHERE id = %s ", (id_,)) |
||
304 | if cursor.fetchone() is None: |
||
305 | cursor.close() |
||
306 | cnx.disconnect() |
||
307 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
308 | description='API.RULE_NOT_FOUND') |
||
309 | |||
310 | cursor.execute(" SELECT name " |
||
311 | " FROM tbl_rules " |
||
312 | " WHERE name = %s AND id != %s ", (name, id_)) |
||
313 | if cursor.fetchone() is not None: |
||
314 | cursor.close() |
||
315 | cnx.disconnect() |
||
316 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
317 | description='API.RULE_NAME_IS_ALREADY_IN_USE') |
||
318 | |||
319 | update_row = (" UPDATE tbl_rules " |
||
320 | " SET name = %s, fdd_code = %s, category = %s, priority = %s, " |
||
321 | " channel = %s, expression = %s, message_template = %s, " |
||
322 | " is_enabled = %s " |
||
323 | " WHERE id = %s ") |
||
324 | cursor.execute(update_row, (name, |
||
325 | fdd_code, |
||
326 | category, |
||
327 | priority, |
||
328 | channel, |
||
329 | expression, |
||
330 | message_template, |
||
331 | is_enabled, |
||
332 | id_,)) |
||
333 | cnx.commit() |
||
334 | |||
335 | cursor.close() |
||
336 | cnx.disconnect() |
||
337 | |||
338 | resp.status = falcon.HTTP_200 |
||
339 |