Conditions | 34 |
Total Lines | 143 |
Code Lines | 112 |
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 bika.lims.browser.batch.batchbook.BatchBookView.folderitems() 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 | # -*- coding: utf-8 -*- |
||
133 | def folderitems(self): |
||
134 | """Accumulate a list of all AnalysisRequest objects contained in |
||
135 | this Batch, as well as those which are inherited. |
||
136 | """ |
||
137 | wf = api.get_tool('portal_workflow') |
||
138 | schema = self.context.Schema() |
||
139 | |||
140 | ars = [] |
||
141 | |||
142 | for o in schema.getField('InheritedObjects').get(self.context): |
||
143 | if o.portal_type == 'AnalysisRequest': |
||
144 | if o not in ars: |
||
145 | ars.append(o) |
||
146 | elif o.portal_type == 'Batch': |
||
147 | for ar in o.getAnalysisRequests(is_active=True): |
||
148 | if ar not in ars: |
||
149 | ars.append(ar) |
||
150 | |||
151 | for ar in self.context.getAnalysisRequests(is_active=True): |
||
152 | if ar not in ars: |
||
153 | ars.append(ar) |
||
154 | |||
155 | self.categories = [] |
||
156 | analyses = {} |
||
157 | items = [] |
||
158 | distinct = [] # distinct analyses (each one a different service) |
||
159 | keywords = [] |
||
160 | for ar in ars: |
||
161 | analyses[ar.id] = [] |
||
162 | for analysis in ar.getAnalyses(full_objects=True): |
||
163 | analyses[ar.id].append(analysis) |
||
164 | if analysis.getKeyword() not in keywords: |
||
165 | # we use a keyword check, because versioned services are !=. |
||
166 | keywords.append(analysis.getKeyword()) |
||
167 | distinct.append(analysis) |
||
168 | |||
169 | batchlink = "" |
||
170 | batch = ar.getBatch() |
||
171 | if batch: |
||
172 | batchlink = "<a href='%s'>%s</a>" % ( |
||
173 | batch.absolute_url(), batch.Title()) |
||
174 | |||
175 | arlink = "<a href='%s'>%s</a>" % ( |
||
176 | ar.absolute_url(), ar.Title()) |
||
177 | |||
178 | subgroup = ar.Schema()['SubGroup'].get(ar) |
||
179 | sub_title = subgroup.Title() if subgroup else 'No Subgroup' |
||
180 | sub_sort = subgroup.getSortKey() if subgroup else '1' |
||
181 | sub_class = re.sub(r"[^A-Za-z\w\d\-\_]", '', sub_title) |
||
182 | |||
183 | if [sub_sort, sub_title] not in self.categories: |
||
184 | self.categories.append([sub_sort, sub_title]) |
||
185 | |||
186 | review_state = wf.getInfoFor(ar, 'review_state') |
||
187 | state_title = wf.getTitleForStateOnType( |
||
188 | review_state, 'AnalysisRequest') |
||
189 | |||
190 | item = { |
||
191 | 'obj': ar, |
||
192 | 'id': ar.id, |
||
193 | 'uid': ar.UID(), |
||
194 | 'category': sub_title, |
||
195 | 'title': ar.Title(), |
||
196 | 'type_class': 'contenttype-AnalysisRequest', |
||
197 | 'url': ar.absolute_url(), |
||
198 | 'relative_url': ar.absolute_url(), |
||
199 | 'view_url': ar.absolute_url(), |
||
200 | 'created': self.ulocalized_time(ar.created(), long_format=1), |
||
201 | 'sort_key': ar.created(), |
||
202 | 'replace': { |
||
203 | 'Batch': batchlink, |
||
204 | 'AnalysisRequest': arlink, |
||
205 | }, |
||
206 | 'before': {}, |
||
207 | 'after': {}, |
||
208 | 'choices': {}, |
||
209 | 'class': {'Batch': 'Title'}, |
||
210 | 'state_class': 'state-active subgroup_{0}'.format(sub_class) if sub_class else 'state-active', |
||
211 | 'allow_edit': [], |
||
212 | 'Batch': '', |
||
213 | 'SamplePoint': ar.getSamplePoint().Title() if ar.getSamplePoint() else '', |
||
214 | 'SampleType': ar.getSampleType().Title() if ar.getSampleType() else '', |
||
215 | 'ClientOrderNumber': ar.getClientOrderNumber(), |
||
216 | 'AnalysisRequest': '', |
||
217 | 'state_title': state_title, |
||
218 | } |
||
219 | items.append(item) |
||
220 | |||
221 | unitstr = '<em class="discreet" style="white-space:nowrap;">%s</em>' |
||
222 | checkPermission = getSecurityManager().checkPermission |
||
223 | |||
224 | # Insert columns for analyses |
||
225 | for d_a in distinct: |
||
226 | keyword = d_a.getKeyword() |
||
227 | short = d_a.getShortTitle() |
||
228 | title = d_a.Title() |
||
229 | self.columns[keyword] = { |
||
230 | 'title': short if short else title, |
||
231 | 'sortable': True |
||
232 | } |
||
233 | self.review_states[0]['columns'].insert( |
||
234 | len(self.review_states[0]['columns']) - 1, keyword) |
||
235 | |||
236 | # Insert values for analyses |
||
237 | for i, item in enumerate(items): |
||
238 | for analysis in analyses[item['id']]: |
||
239 | if keyword not in items[i]: |
||
240 | items[i][keyword] = '' |
||
241 | if analysis.getKeyword() != keyword: |
||
242 | continue |
||
243 | |||
244 | edit = checkPermission(EditResults, analysis) |
||
245 | calculation = analysis.getCalculation() |
||
246 | if self.allow_edit and edit and not calculation: |
||
247 | items[i]['allow_edit'].append(keyword) |
||
248 | if not self.insert_submit_button: |
||
249 | self.insert_submit_button = True |
||
250 | |||
251 | value = analysis.getResult() |
||
252 | items[i][keyword] = value |
||
253 | items[i]['class'][keyword] = '' |
||
254 | |||
255 | if value or (edit and not calculation): |
||
256 | |||
257 | unit = unitstr % d_a.getUnit() |
||
258 | items[i]['after'][keyword] = unit |
||
259 | |||
260 | if keyword not in items[i]['class']: |
||
261 | items[i]['class'][keyword] = 'empty' |
||
262 | if self.insert_submit_button: |
||
263 | transitions = self.review_states[0].get('custom_transitions', []) |
||
264 | transitions.append({ |
||
265 | 'id': 'submit', |
||
266 | 'title': _('Submit') |
||
267 | }) |
||
268 | self.review_states[0]['custom_transitions'] = transitions |
||
269 | |||
270 | self.categories.sort() |
||
271 | self.categories = [x[1] for x in self.categories] |
||
272 | |||
273 | items = sorted(items, key=itemgetter("sort_key")) |
||
274 | |||
275 | return items |
||
276 |