Conditions | 11 |
Total Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 ActionAliasExecutionController.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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
53 | @jsexpose(body_cls=AliasExecutionAPI, status_code=http_client.CREATED) |
||
54 | def post(self, payload): |
||
55 | action_alias_name = payload.name if payload else None |
||
56 | |||
57 | if not action_alias_name: |
||
58 | abort(http_client.BAD_REQUEST, 'Alias execution "name" is required') |
||
59 | |||
60 | format_str = payload.format or '' |
||
61 | command = payload.command or '' |
||
62 | |||
63 | try: |
||
64 | action_alias_db = ActionAlias.get_by_name(action_alias_name) |
||
65 | except ValueError: |
||
66 | action_alias_db = None |
||
67 | |||
68 | if not action_alias_db: |
||
69 | msg = 'Unable to identify action alias with name "%s".' % (action_alias_name) |
||
70 | abort(http_client.NOT_FOUND, msg) |
||
71 | return |
||
72 | |||
73 | if not action_alias_db.enabled: |
||
74 | msg = 'Action alias with name "%s" is disabled.' % (action_alias_name) |
||
75 | abort(http_client.BAD_REQUEST, msg) |
||
76 | return |
||
77 | |||
78 | execution_parameters = extract_parameters_for_action_alias_db( |
||
79 | action_alias_db=action_alias_db, |
||
80 | format_str=format_str, |
||
81 | param_stream=command) |
||
82 | notify = self._get_notify_field(payload) |
||
83 | |||
84 | context = { |
||
85 | 'action_alias_ref': reference.get_ref_from_model(action_alias_db), |
||
86 | 'api_user': payload.user, |
||
87 | 'user': get_requester(), |
||
88 | 'source_channel': payload.source_channel |
||
89 | } |
||
90 | |||
91 | execution = self._schedule_execution(action_alias_db=action_alias_db, |
||
92 | params=execution_parameters, |
||
93 | notify=notify, |
||
94 | context=context) |
||
95 | |||
96 | result = { |
||
97 | 'execution': execution, |
||
98 | 'actionalias': ActionAliasAPI.from_model(action_alias_db) |
||
99 | } |
||
100 | |||
101 | if action_alias_db.ack: |
||
102 | try: |
||
103 | if 'format' in action_alias_db.ack: |
||
104 | result.update({ |
||
105 | 'message': render({'alias': action_alias_db.ack['format']}, result)['alias'] |
||
106 | }) |
||
107 | except UndefinedError as e: |
||
108 | result.update({ |
||
109 | 'message': 'Cannot render "format" in field "ack" for alias. ' + e.message |
||
110 | }) |
||
111 | |||
112 | try: |
||
113 | if 'extra' in action_alias_db.ack: |
||
114 | result.update({ |
||
115 | 'extra': render(action_alias_db.ack['extra'], result) |
||
116 | }) |
||
117 | except UndefinedError as e: |
||
118 | result.update({ |
||
119 | 'extra': 'Cannot render "extra" in field "ack" for alias. ' + e.message |
||
120 | }) |
||
121 | |||
122 | return result |
||
123 | |||
174 |