Conditions | 13 |
Total Lines | 75 |
Code Lines | 59 |
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.header_table.HeaderTableView.render_field_view() 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 -*- |
||
70 | def render_field_view(self, field): |
||
71 | fieldname = field.getName() |
||
72 | field = self.context.Schema()[fieldname] |
||
73 | ret = {"fieldName": fieldname, "mode": "view"} |
||
74 | try: |
||
75 | adapter = getAdapter(self.context, |
||
76 | interface=IHeaderTableFieldRenderer, |
||
77 | name=fieldname) |
||
78 | |||
79 | except ComponentLookupError: |
||
80 | adapter = None |
||
81 | if adapter: |
||
82 | ret = {'fieldName': fieldname, |
||
83 | 'mode': 'structure', |
||
84 | 'html': adapter(field)} |
||
85 | else: |
||
86 | if field.getWidgetName() == "BooleanWidget": |
||
87 | value = field.get(self.context) |
||
88 | ret = { |
||
89 | "fieldName": fieldname, |
||
90 | "mode": "structure", |
||
91 | "html": t(_("Yes")) if value else t(_("No")) |
||
92 | } |
||
93 | elif field.getType().find("Reference") > -1: |
||
94 | # Prioritize method retrieval over schema"s field |
||
95 | targets = None |
||
96 | if hasattr(self.context, "get%s" % fieldname): |
||
97 | fieldaccessor = getattr(self.context, "get%s" % fieldname) |
||
98 | if callable(fieldaccessor): |
||
99 | targets = fieldaccessor() |
||
100 | if not targets: |
||
101 | targets = field.get(self.context) |
||
102 | |||
103 | if targets: |
||
104 | if not type(targets) == list: |
||
105 | targets = [targets, ] |
||
106 | sm = getSecurityManager() |
||
107 | if all([sm.checkPermission(view, ta) for ta in targets]): |
||
108 | elements = [ |
||
109 | "<div id='{id}' class='field reference'>" |
||
110 | " <a class='link' uid='{uid}' href='{url}'>" |
||
111 | " {title}" |
||
112 | " </a>" |
||
113 | "</div>" |
||
114 | .format(id=target.getId(), |
||
115 | uid=target.UID(), |
||
116 | url=target.absolute_url(), |
||
117 | title=target.Title()) |
||
118 | for target in targets] |
||
119 | |||
120 | ret = { |
||
121 | "fieldName": fieldname, |
||
122 | "mode": "structure", |
||
123 | "html": "".join(elements), |
||
124 | } |
||
125 | else: |
||
126 | ret = { |
||
127 | "fieldName": fieldname, |
||
128 | "mode": "structure", |
||
129 | "html": ", ".join([ta.Title() for ta in targets]), |
||
130 | } |
||
131 | else: |
||
132 | ret = { |
||
133 | "fieldName": fieldname, |
||
134 | "mode": "structure", |
||
135 | "html": "", |
||
136 | } |
||
137 | elif field.getType().lower().find("datetime") > -1: |
||
138 | value = field.get(self.context) |
||
139 | ret = { |
||
140 | "fieldName": fieldname, |
||
141 | "mode": "structure", |
||
142 | "html": self.ulocalized_time(value, long_format=True) |
||
143 | } |
||
144 | return ret |
||
145 | |||
206 |