| Conditions | 10 |
| Total Lines | 71 |
| Code Lines | 45 |
| 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 senaite.core.browser.widgets.analysisprofileswidget.AnalysisProfilesWidget.folderitem() 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 -*- |
||
| 199 | def folderitem(self, obj, item, index): |
||
| 200 | """Service triggered each time an item is iterated in folderitems. |
||
| 201 | |||
| 202 | The use of this service prevents the extra-loops in child objects. |
||
| 203 | |||
| 204 | :obj: the instance of the class to be foldered |
||
| 205 | :item: dict containing the properties of the object to be used by |
||
| 206 | the template |
||
| 207 | :index: current index of the item |
||
| 208 | """ |
||
| 209 | item = super(AnalysisProfilesWidget, self).folderitem(obj, item, index) |
||
| 210 | |||
| 211 | # ensure we have an object and not a brain |
||
| 212 | obj = api.get_object(obj) |
||
| 213 | uid = api.get_uid(obj) |
||
| 214 | url = api.get_url(obj) |
||
| 215 | title = api.get_title(obj) |
||
| 216 | |||
| 217 | # get the category |
||
| 218 | if self.show_categories_enabled(): |
||
| 219 | category = obj.getCategoryTitle() |
||
| 220 | if category not in self.categories: |
||
| 221 | self.categories.append(category) |
||
| 222 | item["category"] = category |
||
| 223 | |||
| 224 | hidden = False |
||
| 225 | # get the hidden setting from the records |
||
| 226 | if self.records.get(uid): |
||
| 227 | record = self.records.get(uid, {}) or {} |
||
| 228 | hidden = record.get("hidden", False) |
||
| 229 | else: |
||
| 230 | # get the default value from the service |
||
| 231 | hidden = obj.getHidden() |
||
| 232 | |||
| 233 | item["replace"]["Title"] = get_link(url, value=title) |
||
| 234 | item["Price"] = self.format_price(obj.Price) |
||
| 235 | item["allow_edit"] = self.get_editable_columns() |
||
| 236 | item["selected"] = False |
||
| 237 | item["Hidden"] = hidden |
||
| 238 | item["replace"]["Hidden"] = _("Yes") if hidden else _("No") |
||
| 239 | item["selected"] = uid in self.records |
||
| 240 | item["Keyword"] = obj.getKeyword() |
||
| 241 | |||
| 242 | # Add methods |
||
| 243 | methods = obj.getMethods() |
||
| 244 | if methods: |
||
| 245 | links = map( |
||
| 246 | lambda m: get_link( |
||
| 247 | m.absolute_url(), value=m.Title(), css_class="link"), |
||
| 248 | methods) |
||
| 249 | item["replace"]["Methods"] = ", ".join(links) |
||
| 250 | else: |
||
| 251 | item["methods"] = "" |
||
| 252 | |||
| 253 | # Unit |
||
| 254 | unit = obj.getUnit() |
||
| 255 | item["Unit"] = unit or "" |
||
| 256 | item["replace"]["Unit"] = unit and format_supsub(unit) or "" |
||
| 257 | |||
| 258 | # Icons |
||
| 259 | after_icons = "" |
||
| 260 | if obj.getAccredited(): |
||
| 261 | after_icons += get_image( |
||
| 262 | "accredited.png", title=_("Accredited")) |
||
| 263 | if obj.getAttachmentRequired(): |
||
| 264 | after_icons += get_image( |
||
| 265 | "attach_reqd.png", title=_("Attachment required")) |
||
| 266 | if after_icons: |
||
| 267 | item["after"]["Title"] = after_icons |
||
| 268 | |||
| 269 | return item |
||
| 270 |