Conditions | 3 |
Total Lines | 74 |
Code Lines | 37 |
Lines | 11 |
Ratio | 14.86 % |
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:
1 | # -*- coding: utf-8 -*- |
||
122 | def folderitem(self, obj, item, index): |
||
123 | """Service triggered each time an item is iterated in folderitems. |
||
124 | The use of this service prevents the extra-loops in child objects. |
||
125 | :obj: the instance of the class to be foldered |
||
126 | :item: dict containing the properties of the object to be used by |
||
127 | the template |
||
128 | :index: current index of the item |
||
129 | """ |
||
130 | obj = api.get_object(obj) |
||
131 | |||
132 | # We are using the existing logic from the auditview |
||
133 | logview = api.get_view("auditlog", context=obj, request=self.request) |
||
134 | |||
135 | # get the last snapshot |
||
136 | snapshot = get_last_snapshot(obj) |
||
137 | # get the metadata of the last snapshot |
||
138 | metadata = get_snapshot_metadata(snapshot) |
||
139 | |||
140 | title = obj.Title() |
||
141 | url = obj.absolute_url() |
||
142 | auditlog_url = "{}/@@auditlog".format(url) |
||
143 | |||
144 | # Title |
||
145 | item["title"] = title |
||
146 | # Link the title to the auditlog of the object |
||
147 | item["replace"]["title"] = get_link(auditlog_url, value=title) |
||
148 | |||
149 | # Version |
||
150 | version = get_snapshot_version(obj, snapshot) |
||
151 | item["version"] = version |
||
152 | |||
153 | # Modification Date |
||
154 | m_date = metadata.get("modified") |
||
155 | item["modified"] = logview.to_localized_time(m_date) |
||
156 | |||
157 | # Actor |
||
158 | actor = metadata.get("actor") |
||
159 | item["actor"] = actor |
||
160 | |||
161 | # Fullname |
||
162 | properties = api.get_user_properties(actor) |
||
163 | item["fullname"] = properties.get("fullname", actor) |
||
164 | |||
165 | # Roles |
||
166 | roles = metadata.get("roles", []) |
||
167 | item["roles"] = ", ".join(roles) |
||
168 | |||
169 | # Remote Address |
||
170 | remote_address = metadata.get("remote_address") |
||
171 | item["remote_address"] = remote_address |
||
172 | |||
173 | # Action |
||
174 | action = metadata.get("action") |
||
175 | item["action"] = logview.translate_state(action) |
||
176 | |||
177 | # Review State |
||
178 | review_state = metadata.get("review_state") |
||
179 | item["review_state"] = logview.translate_state(review_state) |
||
180 | |||
181 | # get the previous snapshot |
||
182 | prev_snapshot = get_snapshot_by_version(obj, version-1) |
||
183 | View Code Duplication | if prev_snapshot: |
|
184 | prev_metadata = get_snapshot_metadata(prev_snapshot) |
||
185 | prev_review_state = prev_metadata.get("review_state") |
||
186 | if prev_review_state != review_state: |
||
187 | item["replace"]["review_state"] = "{} → {}".format( |
||
188 | logview.translate_state(prev_review_state), |
||
189 | logview.translate_state(review_state)) |
||
190 | |||
191 | # Rendered Diff |
||
192 | diff = compare_snapshots(snapshot, prev_snapshot) |
||
193 | item["diff"] = logview.render_diff(diff) |
||
194 | |||
195 | return item |
||
196 | |||
209 |