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