-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuditTrail.py
223 lines (180 loc) · 7.11 KB
/
AuditTrail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
"""Audit Trail report requested by Lakshmi.
"""
from functools import cached_property
from cdrapi.docs import Doc
from cdrcgi import Controller
class Control(Controller):
SUBTITLE = "Audit Trail"
OPTIONS = (
("blocking", "Include blocking/unblocking actions"),
("comments", "Include comments"),
("order-ascending", "Show oldest actions first"),
)
def build_tables(self):
"""Callback invoked by show_report()."""
return [self.table] if self.doc else self.show_form()
def populate_form(self, page):
"""Ask for a document ID if we didn't get one already."""
if self.doc:
return self.show_report()
fieldset = page.fieldset("Report Request")
fieldset.append(page.text_field("id"))
fieldset.append(page.text_field("rows", value=150))
page.form.append(fieldset)
fieldset = page.fieldset("Optional Inclusions")
for value, label in self.OPTIONS:
fieldset.append(page.checkbox("opts", label=label, value=value))
page.form.append(fieldset)
@cached_property
def doc(self):
"""The subject of the report."""
doc_id = self.fields.getvalue("id", "").strip()
if doc_id:
doc = Doc(self.session, id=doc_id)
if doc.title is None:
message = f"Document {doc_id} was not found."
self.alerts.append(dict(message=message, type="warning"))
return None
return doc
if self.request:
message = "Document ID is required."
self.alerts.append(dict(message=message, type="error"))
return None
@cached_property
def actions(self):
"""Information pulled from the audit trail."""
fields = [
't.dt AS "date"',
'u.fullname AS "username"',
'a.name AS "action"',
]
if self.include_comments:
fields.append("t.comment")
query = self.Query("audit_trail t", *fields)
query.join("usr u", "u.id = t.usr")
query.join("action a", "a.id = t.action")
query.where(query.Condition("t.document", self.doc.id))
rows = query.execute(self.cursor).fetchall()
return [self.Action(self, row) for row in rows]
@cached_property
def blocking_actions(self):
"""Blocking/unblocking actions for the report's document."""
query = self.Query("audit_trail_added_action b", "b.dt", "a.name")
query.join("action a", "a.id = b.action")
query.where(query.Condition("b.document", self.doc.id))
actions = {}
for row in query.execute(self.cursor).fetchall():
actions[str(row.dt)] = row.name
return actions
@cached_property
def counts(self):
"""String showing how many actions were found, how many shown."""
total = len(self.actions) + len(self.locks)
shown = min(total, self.limit)
return f"Showing {shown} of {total} actions"
@cached_property
def include_blocking_actions(self):
"""True if we should include blocking/unblocking actions."""
return "blocking" in self.options
@cached_property
def include_comments(self):
"""True if we should include a column for comments."""
return "comments" in self.options
@cached_property
def limit(self):
"""How many rows should be included in the report."""
return int(self.fields.getvalue("rows", "150"))
@cached_property
def locks(self):
"""Locking actions for this document."""
fields = [
'c.dt_out AS "date"',
'u.fullname AS "username"',
'\'LOCK DOCUMENT\' AS "action"',
]
if self.include_comments:
fields.append("c.comment")
query = self.Query("checkout c", *fields)
query.join("usr u", "u.id = c.usr")
query.where(query.Condition("c.id", self.doc.id))
rows = query.execute(self.cursor).fetchall()
return [self.Action(self, row) for row in rows]
@property
def options(self):
return self.fields.getlist("opts")
@cached_property
def reverse(self):
"""True if the newer actions should be show first (the default)."""
return "order-ascending" not in self.options
@cached_property
def rows(self):
"""Rows for the report's table."""
actions = sorted(self.actions + self.locks)
if self.reverse:
actions = reversed(actions)
return [a.row for a in list(actions)[:self.limit]]
@cached_property
def same_window(self):
"""Don't open more than one new browser tab."""
return [self.SUBMIT] if self.request else []
@cached_property
def table(self):
"""Table assembled for the report."""
caption = self.doc.cdr_id, self.doc.title, self.counts
columns = ["Date/Time", "User Name", "Action"]
if self.include_comments:
columns.append("Comments")
return self.Reporter.Table(self.rows, columns=columns, caption=caption)
@cached_property
def wide_css(self):
"""Use more space if showing comments."""
return self.Reporter.Table.WIDE_CSS if self.include_comments else ""
class Action:
"""Information for one row in the report."""
def __init__(self, control, row):
"""Capture the caller's values.
Required positional arguments:
control - access to report options and blocking information
row - information about the audit action
"""
self.control = control
self.dbrow = row
def __lt__(self, other):
"""Sort the actions by date."""
return self.dbrow.date < other.dbrow.date
@cached_property
def action(self):
"""What the user did with the document."""
action = self.dbrow.action
if not self.control.include_blocking_actions:
return action
verb = action.split()[0]
if verb not in ("ADD", "MODIFY"):
return action
blocking_action = self.control.blocking_actions.get(self.date)
if not blocking_action:
return action
blocking_verb = blocking_action.split()[0]
return f"{verb} and {blocking_verb} DOCUMENT"
@cached_property
def date(self):
"""When the action took place."""
return str(self.dbrow.date)
@cached_property
def row(self):
"""What goes in the report's table."""
row = [
self.control.Reporter.Cell(self.date[:19], classes="nowrap"),
self.control.Reporter.Cell(self.username, classes="nowrap"),
self.control.Reporter.Cell(self.action, classes="nowrap"),
]
if self.control.include_comments:
row.append(self.dbrow.comment)
return row
@cached_property
def username(self):
"""Who performed the action."""
return self.dbrow.username
if __name__ == "__main__":
Control().run()