| Conditions | 10 |
| Total Lines | 62 |
| Code Lines | 41 |
| 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.widgets.serviceswidget.ServicesView.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 -*- |
||
| 119 | def folderitem(self, obj, item, index): |
||
| 120 | """Service triggered each time an item is iterated in folderitems. |
||
| 121 | |||
| 122 | The use of this service prevents the extra-loops in child objects. |
||
| 123 | |||
| 124 | :obj: the instance of the class to be foldered |
||
| 125 | :item: dict containing the properties of the object to be used by |
||
| 126 | the template |
||
| 127 | :index: current index of the item |
||
| 128 | """ |
||
| 129 | # ensure we have an object and not a brain |
||
| 130 | obj = api.get_object(obj) |
||
| 131 | uid = api.get_uid(obj) |
||
| 132 | url = api.get_url(obj) |
||
| 133 | title = api.get_title(obj) |
||
| 134 | |||
| 135 | # get the category |
||
| 136 | if self.show_categories_enabled(): |
||
| 137 | category = obj.getCategoryTitle() |
||
| 138 | if category not in self.categories: |
||
| 139 | self.categories.append(category) |
||
| 140 | item["category"] = category |
||
| 141 | |||
| 142 | item["replace"]["Title"] = get_link(url, value=title) |
||
| 143 | item["selected"] = False |
||
| 144 | item["selected"] = uid in self.selected_services_uids |
||
| 145 | |||
| 146 | # Add methods |
||
| 147 | methods = obj.getMethods() |
||
| 148 | if methods: |
||
| 149 | links = map( |
||
| 150 | lambda m: get_link( |
||
| 151 | m.absolute_url(), value=m.Title(), css_class="link"), |
||
| 152 | methods) |
||
| 153 | item["replace"]["Methods"] = ", ".join(links) |
||
| 154 | else: |
||
| 155 | item["methods"] = "" |
||
| 156 | |||
| 157 | calculation = obj.getCalculation() |
||
| 158 | if calculation: |
||
| 159 | title = calculation.Title() |
||
| 160 | url = calculation.absolute_url() |
||
| 161 | item["Calculation"] = title |
||
| 162 | item["replace"]["Calculation"] = get_link(url, value=title) |
||
| 163 | else: |
||
| 164 | item["Calculation"] = "" |
||
| 165 | |||
| 166 | # Icons |
||
| 167 | after_icons = "" |
||
| 168 | if obj.getAccredited(): |
||
| 169 | after_icons += get_image( |
||
| 170 | "accredited.png", title=_("Accredited")) |
||
| 171 | if obj.getAttachmentOption() == "r": |
||
| 172 | after_icons += get_image( |
||
| 173 | "attach_reqd.png", title=_("Attachment required")) |
||
| 174 | if obj.getAttachmentOption() == "n": |
||
| 175 | after_icons += get_image( |
||
| 176 | "attach_no.png", title=_("Attachment not permitted")) |
||
| 177 | if after_icons: |
||
| 178 | item["after"]["Title"] = after_icons |
||
| 179 | |||
| 180 | return item |
||
| 181 | |||
| 221 |