Conditions | 14 |
Total Lines | 92 |
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 |
||
98 | def _post(self, payload, requester_user, show_secrets=False, match_multiple=False): |
||
99 | action_alias_name = payload.name if payload else None |
||
100 | |||
101 | if not action_alias_name: |
||
102 | abort(http_client.BAD_REQUEST, 'Alias execution "name" is required') |
||
103 | return |
||
104 | |||
105 | if not requester_user: |
||
106 | requester_user = UserDB(cfg.CONF.system_user.user) |
||
107 | |||
108 | format_str = payload.format or '' |
||
109 | command = payload.command or '' |
||
110 | |||
111 | try: |
||
112 | action_alias_db = ActionAlias.get_by_name(action_alias_name) |
||
113 | except ValueError: |
||
114 | action_alias_db = None |
||
115 | |||
116 | if not action_alias_db: |
||
117 | msg = 'Unable to identify action alias with name "%s".' % (action_alias_name) |
||
118 | abort(http_client.NOT_FOUND, msg) |
||
119 | return |
||
120 | |||
121 | if not action_alias_db.enabled: |
||
122 | msg = 'Action alias with name "%s" is disabled.' % (action_alias_name) |
||
123 | abort(http_client.BAD_REQUEST, msg) |
||
124 | return |
||
125 | |||
126 | if match_multiple: |
||
127 | multiple_execution_parameters = extract_parameters_for_action_alias_db( |
||
128 | action_alias_db=action_alias_db, |
||
129 | format_str=format_str, |
||
130 | param_stream=command, |
||
131 | match_multiple=match_multiple) |
||
132 | else: |
||
133 | multiple_execution_parameters = [ |
||
134 | extract_parameters_for_action_alias_db( |
||
135 | action_alias_db=action_alias_db, |
||
136 | format_str=format_str, |
||
137 | param_stream=command, |
||
138 | match_multiple=match_multiple) |
||
139 | ] |
||
140 | |||
141 | notify = self._get_notify_field(payload) |
||
142 | |||
143 | context = { |
||
144 | 'action_alias_ref': reference.get_ref_from_model(action_alias_db), |
||
145 | 'api_user': payload.user, |
||
146 | 'user': requester_user.name, |
||
147 | 'source_channel': payload.source_channel |
||
148 | } |
||
149 | |||
150 | results = [] |
||
151 | for execution_parameters in multiple_execution_parameters: |
||
152 | execution = self._schedule_execution(action_alias_db=action_alias_db, |
||
153 | params=execution_parameters, |
||
154 | notify=notify, |
||
155 | context=context, |
||
156 | show_secrets=show_secrets, |
||
157 | requester_user=requester_user) |
||
158 | |||
159 | result = { |
||
160 | 'execution': execution, |
||
161 | 'actionalias': ActionAliasAPI.from_model(action_alias_db) |
||
162 | } |
||
163 | |||
164 | if action_alias_db.ack: |
||
165 | try: |
||
166 | if 'format' in action_alias_db.ack: |
||
167 | message = render({'alias': action_alias_db.ack['format']}, result)['alias'] |
||
168 | |||
169 | result.update({ |
||
170 | 'message': message |
||
171 | }) |
||
172 | except UndefinedError as e: |
||
173 | result.update({ |
||
174 | 'message': 'Cannot render "format" in field "ack" for alias. ' + e.message |
||
175 | }) |
||
176 | |||
177 | try: |
||
178 | if 'extra' in action_alias_db.ack: |
||
179 | result.update({ |
||
180 | 'extra': render(action_alias_db.ack['extra'], result) |
||
181 | }) |
||
182 | except UndefinedError as e: |
||
183 | result.update({ |
||
184 | 'extra': 'Cannot render "extra" in field "ack" for alias. ' + e.message |
||
185 | }) |
||
186 | |||
187 | results.append(result) |
||
188 | |||
189 | return results |
||
190 | |||
250 |