| Conditions | 15 |
| Total Lines | 77 |
| Code Lines | 61 |
| 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.health.browser.patient.historicresults.get_historicresults() 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 -*- |
||
| 85 | def get_historicresults(patient): |
||
| 86 | if not patient: |
||
| 87 | return [], {} |
||
| 88 | |||
| 89 | rows = {} |
||
| 90 | dates = [] |
||
| 91 | uid = patient.UID() |
||
| 92 | states = ['verified', 'published'] |
||
| 93 | |||
| 94 | # Retrieve the AR IDs for the current patient |
||
| 95 | bc = getToolByName(patient, 'bika_catalog') |
||
| 96 | ars = [ar.id for ar |
||
| 97 | in bc(portal_type='AnalysisRequest', review_state=states) |
||
| 98 | if 'Patient' in ar.getObject().Schema() |
||
| 99 | and ar.getObject().Schema().getField('Patient').get(ar.getObject()) |
||
| 100 | and ar.getObject().Schema().getField('Patient').get(ar.getObject()).UID() == uid] |
||
| 101 | |||
| 102 | # Retrieve all the analyses, sorted by ResultCaptureDate DESC |
||
| 103 | bc = getToolByName(patient, 'bika_analysis_catalog') |
||
| 104 | analyses = [an.getObject() for an |
||
| 105 | in bc(portal_type='Analysis', |
||
| 106 | getRequestID=ars, |
||
| 107 | sort_on='getResultCaptureDate', |
||
| 108 | sort_order='reverse')] |
||
| 109 | |||
| 110 | # Build the dictionary of rows |
||
| 111 | for analysis in analyses: |
||
| 112 | ar = analysis.aq_parent |
||
| 113 | sampletype = ar.getSampleType() |
||
| 114 | row = rows.get(sampletype.UID()) if sampletype.UID() in rows.keys() \ |
||
| 115 | else {'object': sampletype, 'analyses': {}} |
||
| 116 | anrow = row.get('analyses') |
||
| 117 | service_uid = analysis.getServiceUID() |
||
| 118 | asdict = anrow.get(service_uid, {'object': analysis, |
||
| 119 | 'title': analysis.Title(), |
||
| 120 | 'keyword': analysis.getKeyword(), |
||
| 121 | 'units': analysis.getUnit()}) |
||
| 122 | date = analysis.getResultCaptureDate() or analysis.created() |
||
| 123 | date = ulocalized_time(date, 1, None, patient, 'bika') |
||
| 124 | # If more than one analysis of the same type has been |
||
| 125 | # performed in the same datetime, get only the last one |
||
| 126 | if date not in asdict.keys(): |
||
| 127 | asdict[date] = {'object': analysis, |
||
| 128 | 'result': analysis.getResult(), |
||
| 129 | 'formattedresult': analysis.getFormattedResult()} |
||
| 130 | # Get the specs |
||
| 131 | # Only the specs applied to the last analysis for that |
||
| 132 | # sample type will be taken into consideration. |
||
| 133 | # We assume specs from previous analyses are obsolete. |
||
| 134 | if 'specs' not in asdict.keys(): |
||
| 135 | spec = analysis.getAnalysisSpecs() |
||
| 136 | spec = spec.getResultsRangeDict() if spec else {} |
||
| 137 | specs = spec.get(analysis.getKeyword(), {}) |
||
| 138 | if not specs.get('rangecomment', ''): |
||
| 139 | if specs.get('min', '') and specs.get('max', ''): |
||
| 140 | specs['rangecomment'] = '%s - %s' % \ |
||
| 141 | (specs.get('min'), specs.get('max')) |
||
| 142 | elif specs.get('min', ''): |
||
| 143 | specs['rangecomment'] = '> %s' % specs.get('min') |
||
| 144 | elif specs.get('max', ''): |
||
| 145 | specs['rangecomment'] = '< %s' % specs.get('max') |
||
| 146 | |||
| 147 | if specs.get('error', '0') != '0' \ |
||
| 148 | and specs.get('rangecomment', ''): |
||
| 149 | specs['rangecomment'] = ('%s (%s' % |
||
| 150 | (specs.get('rangecomment'), |
||
| 151 | specs.get('error'))) + '%)' |
||
| 152 | asdict['specs'] = specs |
||
| 153 | |||
| 154 | if date not in dates: |
||
| 155 | dates.append(date) |
||
| 156 | anrow[service_uid] = asdict |
||
| 157 | row['analyses'] = anrow |
||
| 158 | rows[sampletype.UID()] = row |
||
| 159 | dates.sort(reverse=False) |
||
| 160 | |||
| 161 | return dates, rows |
||
| 162 | |||
| 185 |