| 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 -*- |
||
| 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 | obj = api.get_object(obj) |
||
| 128 | |||
| 129 | # We are using the existing logic from the auditview |
||
| 130 | logview = api.get_view("auditlog", context=obj, request=self.request) |
||
| 131 | |||
| 132 | # get the last snapshot |
||
| 133 | snapshot = get_last_snapshot(obj) |
||
| 134 | # get the metadata of the last snapshot |
||
| 135 | metadata = get_snapshot_metadata(snapshot) |
||
| 136 | |||
| 137 | title = obj.Title() |
||
| 138 | url = obj.absolute_url() |
||
| 139 | auditlog_url = "{}/@@auditlog".format(url) |
||
| 140 | |||
| 141 | # Title |
||
| 142 | item["title"] = title |
||
| 143 | # Link the title to the auditlog of the object |
||
| 144 | item["replace"]["title"] = get_link(auditlog_url, value=title) |
||
| 145 | |||
| 146 | # Version |
||
| 147 | version = get_snapshot_version(obj, snapshot) |
||
| 148 | item["version"] = version |
||
| 149 | |||
| 150 | # Modification Date |
||
| 151 | m_date = metadata.get("modified") |
||
| 152 | item["modified"] = logview.to_localized_time(m_date) |
||
| 153 | |||
| 154 | # Actor |
||
| 155 | actor = metadata.get("actor") |
||
| 156 | item["actor"] = actor |
||
| 157 | |||
| 158 | # Fullname |
||
| 159 | properties = api.get_user_properties(actor) |
||
| 160 | item["fullname"] = properties.get("fullname", actor) |
||
| 161 | |||
| 162 | # Roles |
||
| 163 | roles = metadata.get("roles", []) |
||
| 164 | item["roles"] = ", ".join(roles) |
||
| 165 | |||
| 166 | # Remote Address |
||
| 167 | remote_address = metadata.get("remote_address") |
||
| 168 | item["remote_address"] = remote_address |
||
| 169 | |||
| 170 | # Action |
||
| 171 | action = metadata.get("action") |
||
| 172 | item["action"] = logview.translate_state(action) |
||
| 173 | |||
| 174 | # Review State |
||
| 175 | review_state = metadata.get("review_state") |
||
| 176 | item["review_state"] = logview.translate_state(review_state) |
||
| 177 | |||
| 178 | # get the previous snapshot |
||
| 179 | prev_snapshot = get_snapshot_by_version(obj, version-1) |
||
| 180 | View Code Duplication | if prev_snapshot: |
|
|
|
|||
| 181 | prev_metadata = get_snapshot_metadata(prev_snapshot) |
||
| 182 | prev_review_state = prev_metadata.get("review_state") |
||
| 183 | if prev_review_state != review_state: |
||
| 184 | item["replace"]["review_state"] = "{} → {}".format( |
||
| 185 | logview.translate_state(prev_review_state), |
||
| 186 | logview.translate_state(review_state)) |
||
| 187 | |||
| 188 | # Rendered Diff |
||
| 189 | diff = compare_snapshots(snapshot, prev_snapshot) |
||
| 190 | item["diff"] = logview.render_diff(diff) |
||
| 191 | |||
| 192 | return item |
||
| 193 | |||
| 206 |