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