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