1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of SENAITE.CORE |
4
|
|
|
# |
5
|
|
|
# Copyright 2018 by it's authors. |
6
|
|
|
# Some rights reserved. See LICENSE.rst, CONTRIBUTORS.rst. |
7
|
|
|
|
8
|
|
|
import collections |
9
|
|
|
|
10
|
|
|
from bika.lims import api |
11
|
|
|
from bika.lims import bikaMessageFactory as _ |
12
|
|
|
from bika.lims.browser.bika_listing import BikaListingView |
13
|
|
|
from plone.memoize import view |
14
|
|
|
from DateTime import DateTime |
15
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
16
|
|
|
from plone.app.layout.viewlets.content import ContentHistoryView |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class LogView(BikaListingView): |
20
|
|
|
"""Review/Revision log view |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
template = ViewPageTemplateFile("templates/log.pt") |
24
|
|
|
|
25
|
|
|
def __init__(self, context, request): |
26
|
|
|
super(LogView, self).__init__(context, request) |
27
|
|
|
|
28
|
|
|
self.show_select_column = False |
29
|
|
|
self.pagesize = 999999 |
30
|
|
|
self.show_search = False |
31
|
|
|
|
32
|
|
|
self.icon = "{}/{}/{}".format( |
33
|
|
|
self.portal_url, |
34
|
|
|
"++resource++bika.lims.images", |
35
|
|
|
"%s_big.png" % context.portal_type.lower(), |
36
|
|
|
) |
37
|
|
|
|
38
|
|
|
self.title = "{} Log".format(api.get_title(context)) |
39
|
|
|
|
40
|
|
|
self.columns = collections.OrderedDict(( |
41
|
|
|
("Version", { |
42
|
|
|
"title": _("Version"), "sortable": False}), |
43
|
|
|
("Date", { |
44
|
|
|
"title": _("Date"), "sortable": False}), |
45
|
|
|
("Actor", { |
46
|
|
|
"title": _("Actor"), "sortable": False}), |
47
|
|
|
("Action", { |
48
|
|
|
"title": _("Action"), "sortable": False}), |
49
|
|
|
)) |
50
|
|
|
|
51
|
|
|
self.review_states = [ |
52
|
|
|
{ |
53
|
|
|
"id": "default", |
54
|
|
|
"title": "All", |
55
|
|
|
"contentFilter": {}, |
56
|
|
|
"columns": self.columns.keys(), |
57
|
|
|
} |
58
|
|
|
] |
59
|
|
|
|
60
|
|
|
def update(self): |
61
|
|
|
"""Update hook |
62
|
|
|
""" |
63
|
|
|
super(LogView, self).update() |
64
|
|
|
self.total = len(self.get_full_history()) |
65
|
|
|
|
66
|
|
|
def get_revision_history(self): |
67
|
|
|
"""Return the revision history of the current context |
68
|
|
|
""" |
69
|
|
|
chv = ContentHistoryView(self.context, self.request) |
70
|
|
|
return chv.revisionHistory() |
71
|
|
|
|
72
|
|
|
def get_review_history(self): |
73
|
|
|
"""Return the review history of the current context |
74
|
|
|
""" |
75
|
|
|
return api.get_review_history(self.context) |
76
|
|
|
|
77
|
|
|
@view.memoize |
78
|
|
|
def get_full_history(self): |
79
|
|
|
history = self.get_revision_history() + self.get_review_history() |
80
|
|
|
if len(history) == 0: |
81
|
|
|
return [] |
82
|
|
|
history.sort(key=lambda x: x["time"], reverse=True) |
83
|
|
|
return history |
84
|
|
|
|
85
|
|
|
def get_entry_version(self, entry, default=0): |
86
|
|
|
"""Return the version of the entry |
87
|
|
|
""" |
88
|
|
|
version = entry.get("version_id", default) or default |
89
|
|
|
return version |
90
|
|
|
|
91
|
|
|
def get_entry_date(self, entry): |
92
|
|
|
"""Return the action date |
93
|
|
|
""" |
94
|
|
|
date = DateTime(entry.get("time")) |
95
|
|
|
return self.ulocalized_time(date, long_format=1), |
96
|
|
|
|
97
|
|
|
def get_entry_actor(self, entry): |
98
|
|
|
"""Return the fullname of the actor |
99
|
|
|
""" |
100
|
|
|
actor = entry.get("actor") |
101
|
|
|
if isinstance(actor, basestring): |
102
|
|
|
actor = api.get_user_properties(actor) or dict(actorid=actor) |
103
|
|
|
fullname = actor.get("fullname") |
104
|
|
|
if not fullname: |
105
|
|
|
return actor.get("actorid") |
106
|
|
|
return actor.get("fullname") |
107
|
|
|
|
108
|
|
|
def get_entry_action(self, entry): |
109
|
|
|
"""Return the action |
110
|
|
|
""" |
111
|
|
|
# revision entries have a transition_title |
112
|
|
|
transition_title = entry.get("transition_title") |
113
|
|
|
if transition_title: |
114
|
|
|
return transition_title |
115
|
|
|
# review history items have only an action |
116
|
|
|
action = entry.get("action") or "Created" |
117
|
|
|
# make the action human readable |
118
|
|
|
action = action.capitalize().replace("_", " ") |
119
|
|
|
return _(action) |
120
|
|
|
|
121
|
|
View Code Duplication |
def make_empty_item(self, **kw): |
|
|
|
|
122
|
|
|
"""Create a new empty item |
123
|
|
|
""" |
124
|
|
|
item = { |
125
|
|
|
"uid": None, |
126
|
|
|
"before": {}, |
127
|
|
|
"after": {}, |
128
|
|
|
"replace": {}, |
129
|
|
|
"allow_edit": [], |
130
|
|
|
"disabled": False, |
131
|
|
|
"state_class": "state-active", |
132
|
|
|
} |
133
|
|
|
item.update(**kw) |
134
|
|
|
return item |
135
|
|
|
|
136
|
|
|
def make_log_entry(self, entry, index): |
137
|
|
|
"""Create a log entry |
138
|
|
|
""" |
139
|
|
|
item = self.make_empty_item(**entry) |
140
|
|
|
|
141
|
|
|
item["Version"] = self.get_entry_version(entry) |
142
|
|
|
item["Date"] = self.get_entry_date(entry) |
143
|
|
|
item["Actor"] = self.get_entry_actor(entry) |
144
|
|
|
item["Action"] = self.get_entry_action(entry) |
145
|
|
|
|
146
|
|
|
return item |
147
|
|
|
|
148
|
|
|
def folderitems(self): |
149
|
|
|
"""Generate folderitems for each review history item |
150
|
|
|
""" |
151
|
|
|
items = [] |
152
|
|
|
|
153
|
|
|
history = self.get_full_history() |
154
|
|
|
for num, entry in enumerate(history): |
155
|
|
|
items.append(self.make_log_entry(entry, num)) |
156
|
|
|
return items |
157
|
|
|
|