| Conditions | 11 |
| Total Lines | 53 |
| Code Lines | 46 |
| 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.worksheet.ajax.AttachAnalyses.__call__() 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 -*- |
||
| 41 | def __call__(self): |
||
| 42 | plone.protect.CheckAuthenticator(self.request) |
||
| 43 | searchTerm = 'searchTerm' in self.request and self.request['searchTerm'].lower() or '' |
||
| 44 | page = self.request['page'] |
||
| 45 | nr_rows = self.request['rows'] |
||
| 46 | sord = self.request['sord'] |
||
| 47 | sidx = self.request['sidx'] |
||
| 48 | attachable_states = ('assigned', 'unassigned', 'to_be_verified') |
||
| 49 | analysis_to_slot = {} |
||
| 50 | for s in self.context.getLayout(): |
||
| 51 | analysis_to_slot[s['analysis_uid']] = int(s['position']) |
||
| 52 | analyses = list(self.context.getAnalyses(full_objects=True)) |
||
| 53 | # Duplicates belong to the worksheet, so we must add them individually |
||
| 54 | for i in self.context.objectValues(): |
||
| 55 | if i.portal_type == 'DuplicateAnalysis': |
||
| 56 | analyses.append(i) |
||
| 57 | rows = [] |
||
| 58 | for analysis in analyses: |
||
| 59 | review_state = getCurrentState(analysis) |
||
| 60 | if review_state not in attachable_states: |
||
| 61 | continue |
||
| 62 | parent = analysis.getParentTitle() |
||
| 63 | rows.append({'analysis_uid': analysis.UID(), |
||
| 64 | 'slot': analysis_to_slot[analysis.UID()], |
||
| 65 | 'service': analysis.Title(), |
||
| 66 | 'parent': parent, |
||
| 67 | 'type': analysis.portal_type}) |
||
| 68 | |||
| 69 | # if there's a searchTerm supplied, restrict rows to those |
||
| 70 | # who contain at least one field that starts with the chars from |
||
| 71 | # searchTerm. |
||
| 72 | if searchTerm: |
||
| 73 | orig_rows = rows |
||
| 74 | rows = [] |
||
| 75 | for row in orig_rows: |
||
| 76 | matches = [v for v in row.values() |
||
| 77 | if str(v).lower().startswith(searchTerm)] |
||
| 78 | if matches: |
||
| 79 | rows.append(row) |
||
| 80 | |||
| 81 | rows = sorted(rows, cmp=lambda x, y: cmp(x, y), key=itemgetter(sidx and sidx or 'slot')) |
||
| 82 | if sord == 'desc': |
||
| 83 | rows.reverse() |
||
| 84 | pages = len(rows) / int(nr_rows) |
||
| 85 | pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0 |
||
| 86 | start = (int(page)-1) * int(nr_rows) |
||
| 87 | end = int(page) * int(nr_rows) |
||
| 88 | ret = {'page': page, |
||
| 89 | 'total': pages, |
||
| 90 | 'records': len(rows), |
||
| 91 | 'rows': rows[start:end]} |
||
| 92 | |||
| 93 | return json.dumps(ret) |
||
| 94 | |||
| 136 |