Conditions | 24 |
Total Lines | 122 |
Code Lines | 93 |
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 | ars = self.context.getAnalysisRequests(is_active=True) |
||
138 | self.total = len(ars) |
||
139 | |||
140 | self.categories = [] |
||
141 | analyses = {} |
||
142 | items = [] |
||
143 | distinct = [] # distinct analyses (each one a different service) |
||
144 | keywords = [] |
||
145 | for ar in ars: |
||
146 | analyses[ar.id] = [] |
||
147 | for analysis in ar.getAnalyses(full_objects=True): |
||
148 | analyses[ar.id].append(analysis) |
||
149 | if analysis.getKeyword() not in keywords: |
||
150 | # we use a keyword check, because versioned services are !=. |
||
151 | keywords.append(analysis.getKeyword()) |
||
152 | distinct.append(analysis) |
||
153 | |||
154 | batchlink = "" |
||
155 | batch = ar.getBatch() |
||
156 | if batch: |
||
157 | batchlink = "<a href='%s'>%s</a>" % ( |
||
158 | batch.absolute_url(), batch.Title()) |
||
159 | |||
160 | arlink = "<a href='%s'>%s</a>" % ( |
||
161 | ar.absolute_url(), ar.Title()) |
||
162 | |||
163 | subgroup = ar.Schema()["SubGroup"].get(ar) |
||
164 | sub_title = subgroup.Title() if subgroup else t(_("No Subgroup")) |
||
165 | sub_sort = subgroup.getSortKey() if subgroup else "1" |
||
166 | sub_class = re.sub(r"[^A-Za-z\w\d\-\_]", "", sub_title) |
||
167 | |||
168 | if [sub_sort, sub_title] not in self.categories: |
||
169 | self.categories.append([sub_sort, sub_title]) |
||
170 | |||
171 | wf = api.get_tool("portal_workflow") |
||
172 | review_state = api.get_review_status(ar) |
||
173 | state_title = wf.getTitleForStateOnType( |
||
174 | review_state, "AnalysisRequest") |
||
175 | |||
176 | item = { |
||
177 | "obj": ar, |
||
178 | "id": ar.id, |
||
179 | "uid": ar.UID(), |
||
180 | "category": sub_title, |
||
181 | "title": ar.Title(), |
||
182 | "type_class": "contenttype-AnalysisRequest", |
||
183 | "url": ar.absolute_url(), |
||
184 | "relative_url": ar.absolute_url(), |
||
185 | "view_url": ar.absolute_url(), |
||
186 | "created": self.ulocalized_time(ar.created(), long_format=1), |
||
187 | "sort_key": ar.created(), |
||
188 | "replace": { |
||
189 | "Batch": batchlink, |
||
190 | "AnalysisRequest": arlink, |
||
191 | }, |
||
192 | "before": {}, |
||
193 | "after": {}, |
||
194 | "choices": {}, |
||
195 | "class": {"Batch": "Title"}, |
||
196 | "state_class": "state-active subgroup_{0}".format(sub_class) if sub_class else "state-active", |
||
197 | "allow_edit": [], |
||
198 | "Batch": "", |
||
199 | "SamplePoint": ar.getSamplePoint().Title() if ar.getSamplePoint() else "", |
||
200 | "SampleType": ar.getSampleType().Title() if ar.getSampleType() else "", |
||
201 | "ClientOrderNumber": ar.getClientOrderNumber(), |
||
202 | "AnalysisRequest": "", |
||
203 | "state_title": state_title, |
||
204 | } |
||
205 | items.append(item) |
||
206 | |||
207 | unitstr = '<em class="discreet" style="white-space:nowrap;">%s</em>' |
||
208 | |||
209 | # Insert columns for analyses |
||
210 | for d_a in distinct: |
||
211 | keyword = d_a.getKeyword() |
||
212 | short = d_a.getShortTitle() |
||
213 | title = d_a.Title() |
||
214 | |||
215 | self.columns[keyword] = { |
||
216 | "title": short if short else title, |
||
217 | "sortable": False, |
||
218 | } |
||
219 | self.review_states[0]["columns"].insert( |
||
220 | len(self.review_states[0]["columns"]) - 1, keyword) |
||
221 | |||
222 | # Insert values for analyses |
||
223 | for i, item in enumerate(items): |
||
224 | for analysis in analyses[item["id"]]: |
||
225 | if keyword not in items[i]: |
||
226 | items[i][keyword] = "" |
||
227 | if analysis.getKeyword() != keyword: |
||
228 | continue |
||
229 | |||
230 | # check if the user can edit the analysis result |
||
231 | can_edit_result = self.can_edit_analysis_result(analysis) |
||
232 | |||
233 | calculation = analysis.getCalculation() |
||
234 | if self.allow_edit and can_edit_result and not calculation: |
||
235 | items[i]["allow_edit"].append(keyword) |
||
236 | self.columns[keyword]["ajax"] = True |
||
237 | |||
238 | value = analysis.getResult() |
||
239 | items[i][keyword] = value |
||
240 | items[i]["class"][keyword] = "" |
||
241 | |||
242 | if value or (can_edit_result and not calculation): |
||
243 | unit = unitstr % d_a.getUnit() |
||
244 | items[i]["after"][keyword] = unit |
||
245 | |||
246 | if keyword not in items[i]["class"]: |
||
247 | items[i]["class"][keyword] = "empty" |
||
248 | |||
249 | self.categories.sort() |
||
250 | self.categories = [x[1] for x in self.categories] |
||
251 | |||
252 | items = sorted(items, key=itemgetter("sort_key")) |
||
253 | |||
254 | return items |
||
255 |