Conditions | 36 |
Total Lines | 151 |
Code Lines | 118 |
Lines | 35 |
Ratio | 23.18 % |
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.TextMessageItem.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 |
||
271 | @staticmethod |
||
272 | @user_logger |
||
273 | def on_put(req, resp, id_): |
||
274 | """Handles PUT requests""" |
||
275 | access_control(req) |
||
276 | |||
277 | if not id_.isdigit() or int(id_) <= 0: |
||
278 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
279 | description='API.INVALID_TEXT_MESSAGE_ID') |
||
280 | |||
281 | try: |
||
282 | raw_json = req.stream.read().decode('utf-8') |
||
283 | except Exception as ex: |
||
284 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
285 | |||
286 | new_values = json.loads(raw_json) |
||
287 | |||
288 | if 'rule_id' in new_values['data'].keys(): |
||
289 | if new_values['data']['rule_id'] <= 0: |
||
290 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
291 | description='API.INVALID_RULE_ID') |
||
292 | rule_id = new_values['data']['rule_id'] |
||
293 | else: |
||
294 | rule_id = None |
||
295 | |||
296 | if 'recipient_name' not in new_values['data'].keys() or \ |
||
297 | not isinstance(new_values['data']['recipient_name'], str) or \ |
||
298 | len(str.strip(new_values['data']['recipient_name'])) == 0: |
||
299 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
300 | description='API.INVALID_RECIPIENT_NAME') |
||
301 | recipient_name = str.strip(new_values['data']['recipient_name']) |
||
302 | |||
303 | if 'recipient_mobile' not in new_values['data'].keys() or \ |
||
304 | not isinstance(new_values['data']['recipient_mobile'], str) or \ |
||
305 | len(str.strip(new_values['data']['recipient_mobile'])) == 0: |
||
306 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
307 | description='API.INVALID_RECIPIENT_MOBILE') |
||
308 | recipient_mobile = str.strip(new_values['data']['recipient_mobile']) |
||
309 | |||
310 | if 'message' not in new_values['data'].keys() or \ |
||
311 | not isinstance(new_values['data']['message'], str) or \ |
||
312 | len(str.strip(new_values['data']['message'])) == 0: |
||
313 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
314 | description='API.INVALID_MESSAGE_VALUE') |
||
315 | message = str.strip(new_values['data']['message']) |
||
316 | |||
317 | if 'acknowledge_code' not in new_values['data'].keys() or \ |
||
318 | not isinstance(new_values['data']['acknowledge_code'], str) or \ |
||
319 | len(str.strip(new_values['data']['acknowledge_code'])) == 0: |
||
320 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
321 | description='API.INVALID_ACKNOWLEDGE_CODE') |
||
322 | acknowledge_code = str.strip(new_values['data']['acknowledge_code']) |
||
323 | |||
324 | if 'status' not in new_values['data'].keys() or \ |
||
325 | not isinstance(new_values['data']['status'], str) or \ |
||
326 | len(str.strip(new_values['data']['status'])) == 0 or \ |
||
327 | str.strip(new_values['data']['status']) not in ('new', 'acknowledged', 'timeout'): |
||
328 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
329 | description='API.INVALID_STATUS') |
||
330 | status = str.strip(new_values['data']['status']) |
||
331 | |||
332 | if 'created_datetime' not in new_values['data'].keys() or \ |
||
333 | not isinstance(new_values['data']['created_datetime'], str) or \ |
||
334 | len(str.strip(new_values['data']['created_datetime'])) == 0: |
||
335 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
336 | description='API.INVALID_CREATED_DATETIME') |
||
337 | created_datetime_local = str.strip(new_values['data']['created_datetime']) |
||
338 | |||
339 | if 'scheduled_datetime' not in new_values['data'].keys() or \ |
||
340 | not isinstance(new_values['data']['scheduled_datetime'], str) or \ |
||
341 | len(str.strip(new_values['data']['scheduled_datetime'])) == 0: |
||
342 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
343 | description='API.INVALID_SCHEDULED_DATETIME') |
||
344 | scheduled_datetime_local = str.strip(new_values['data']['scheduled_datetime']) |
||
345 | |||
346 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
347 | if config.utc_offset[0] == '-': |
||
348 | timezone_offset = -timezone_offset |
||
349 | |||
350 | View Code Duplication | if created_datetime_local is None: |
|
351 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
352 | description="API.INVALID_CREATED_DATETIME") |
||
353 | else: |
||
354 | created_datetime_local = str.strip(created_datetime_local) |
||
355 | try: |
||
356 | created_datetime_utc = datetime.strptime(created_datetime_local, |
||
357 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
358 | timedelta(minutes=timezone_offset) |
||
359 | except ValueError: |
||
360 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
361 | description="API.INVALID_CREATED_DATETIME") |
||
362 | |||
363 | View Code Duplication | if scheduled_datetime_local is None: |
|
364 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
365 | description="API.INVALID_SCHEDULED_DATETIME") |
||
366 | else: |
||
367 | scheduled_datetime_local = str.strip(scheduled_datetime_local) |
||
368 | try: |
||
369 | scheduled_datetime_utc = datetime.strptime(scheduled_datetime_local, |
||
370 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
371 | timedelta(minutes=timezone_offset) |
||
372 | except ValueError: |
||
373 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
374 | description="API.INVALID_SCHEDULED_DATETIME") |
||
375 | |||
376 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
377 | cursor = cnx.cursor() |
||
378 | |||
379 | cursor.execute(" SELECT recipient_name " |
||
380 | " FROM tbl_text_messages_outbox " |
||
381 | " WHERE id = %s ", (id_,)) |
||
382 | |||
383 | if cursor.fetchone() is None: |
||
384 | cursor.close() |
||
385 | cnx.disconnect() |
||
386 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
387 | description='API.TEXT_MESSAGE_NOT_FOUND') |
||
388 | |||
389 | View Code Duplication | if rule_id is not None: |
|
390 | cursor.execute(" SELECT name " |
||
391 | " FROM tbl_rules " |
||
392 | " WHERE id = %s ", |
||
393 | (new_values['data']['rule_id'],)) |
||
394 | row = cursor.fetchone() |
||
395 | if row is None: |
||
396 | cursor.close() |
||
397 | cnx.disconnect() |
||
398 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
||
399 | description='API.RULE_NOT_FOUND') |
||
400 | |||
401 | update_row = (" UPDATE tbl_text_messages_outbox " |
||
402 | " SET rule_id = %s, recipient_name = %s, recipient_mobile = %s, message = %s," |
||
403 | " acknowledge_code = %s, created_datetime_utc = %s," |
||
404 | " scheduled_datetime_utc = %s, status = %s" |
||
405 | " WHERE id = %s ") |
||
406 | |||
407 | cursor.execute(update_row, (rule_id, |
||
408 | recipient_name, |
||
409 | recipient_mobile, |
||
410 | message, |
||
411 | acknowledge_code, |
||
412 | created_datetime_utc, |
||
413 | scheduled_datetime_utc, |
||
414 | status, |
||
415 | id_)) |
||
416 | |||
417 | cnx.commit() |
||
418 | cursor.close() |
||
419 | cnx.disconnect() |
||
420 | |||
421 | resp.status = falcon.HTTP_200 |
||
422 | |||
454 |