| Conditions | 4 |
| Total Lines | 70 |
| Code Lines | 37 |
| Lines | 11 |
| Ratio | 15.71 % |
| 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 -*- |
||
| 159 | def folderitems(self): |
||
| 160 | """Generate folderitems for each version |
||
| 161 | """ |
||
| 162 | items = [] |
||
| 163 | # get the snapshots |
||
| 164 | snapshots = get_snapshots(self.context) |
||
| 165 | # reverse the order to get the most recent change first |
||
| 166 | snapshots = list(reversed(snapshots)) |
||
| 167 | # set the total number of items |
||
| 168 | self.total = len(snapshots) |
||
| 169 | # slice a batch |
||
| 170 | batch = snapshots[self.limit_from:self.limit_from+self.pagesize] |
||
| 171 | |||
| 172 | for snapshot in batch: |
||
| 173 | item = self.make_empty_item(**snapshot) |
||
| 174 | # get the version of the snapshot |
||
| 175 | version = get_snapshot_version(self.context, snapshot) |
||
| 176 | |||
| 177 | # Version |
||
| 178 | item["version"] = version |
||
| 179 | |||
| 180 | # get the metadata of the diff |
||
| 181 | metadata = get_snapshot_metadata(snapshot) |
||
| 182 | |||
| 183 | # Modification Date |
||
| 184 | m_date = metadata.get("modified") |
||
| 185 | item["modified"] = self.to_localized_time(m_date) |
||
| 186 | |||
| 187 | # Actor |
||
| 188 | actor = metadata.get("actor") |
||
| 189 | item["actor"] = actor |
||
| 190 | |||
| 191 | # Fullname |
||
| 192 | properties = api.get_user_properties(actor) |
||
| 193 | item["fullname"] = properties.get("fullname", actor) |
||
| 194 | |||
| 195 | # Roles |
||
| 196 | roles = metadata.get("roles", []) |
||
| 197 | item["roles"] = ", ".join(roles) |
||
| 198 | |||
| 199 | # Remote Address |
||
| 200 | remote_address = metadata.get("remote_address") |
||
| 201 | item["remote_address"] = remote_address |
||
| 202 | |||
| 203 | # Action |
||
| 204 | action = metadata.get("action") |
||
| 205 | item["action"] = self.translate_state(action) |
||
| 206 | |||
| 207 | # Review State |
||
| 208 | review_state = metadata.get("review_state") |
||
| 209 | item["review_state"] = self.translate_state(review_state) |
||
| 210 | |||
| 211 | # get the previous snapshot |
||
| 212 | prev_snapshot = get_snapshot_by_version(self.context, version-1) |
||
| 213 | View Code Duplication | if prev_snapshot: |
|
| 214 | prev_metadata = get_snapshot_metadata(prev_snapshot) |
||
| 215 | prev_review_state = prev_metadata.get("review_state") |
||
| 216 | if prev_review_state != review_state: |
||
| 217 | item["replace"]["review_state"] = "{} → {}".format( |
||
| 218 | self.translate_state(prev_review_state), |
||
| 219 | self.translate_state(review_state)) |
||
| 220 | |||
| 221 | # Rendered Diff |
||
| 222 | diff = compare_snapshots(snapshot, prev_snapshot) |
||
| 223 | item["diff"] = self.render_diff(diff) |
||
| 224 | |||
| 225 | # append the item |
||
| 226 | items.append(item) |
||
| 227 | |||
| 228 | return items |
||
| 229 |