-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentsModified.py
143 lines (117 loc) · 4.64 KB
/
DocumentsModified.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
#!/usr/bin/env python
"""Report on modified CDR documents.
"We need a simple 'Documents Modified' Report to be generated in an Excel
spreadsheet, which verifies what documents were changed within a given time
frame."
"""
from cdrcgi import Controller
import datetime
class Control(Controller):
SUBTITLE = "Documents Modified Report"
def build_tables(self):
"""Assemble and return the report table."""
opts = dict(columns=self.columns, sheet_name="Modified Documents")
return self.Reporter.Table(self.rows, **opts)
def populate_form(self, page):
"""Add a date range to the form.
Pass:
page - HTMLPage on which the fields are placed
"""
end = datetime.date.today()
start = end - datetime.timedelta(6)
fieldset = page.fieldset("Report Options")
opts = dict(label="Doc Type", options=["all"]+self.doctypes)
fieldset.append(page.select("doctype", **opts))
opts = dict(label="Start Date", value=start)
fieldset.append(page.date_field("start", **opts))
opts = dict(label="End Date", value=end)
fieldset.append(page.date_field("end", **opts))
page.form.append(fieldset)
@property
def format(self):
"""Override the default for the report so that it's a workbook."""
return "excel"
@property
def columns(self):
"""Column headers for the report."""
return (
self.Reporter.Column("Doc ID", width="70px"),
self.Reporter.Column("Doc Title", width="700px"),
self.Reporter.Column("Last Version", width="100px"),
self.Reporter.Column("Publishable", width="100px"),
)
@property
def start(self):
"""Date range beginning selected on the form."""
try:
start = self.fields.getvalue("start")
return self.parse_date(start)
except Exception:
self.logger.exception("invalid start date")
self.bail("Invalid starting date")
@property
def end(self):
"""Date range end selected on the form."""
try:
end = self.fields.getvalue("end")
return self.parse_date(end)
except Exception:
self.logger.exception("invalid end date")
self.bail("Invalid ending date")
@property
def doctype(self):
"""CDR document type selected for the report."""
if not hasattr(self, "_doctype"):
self._doctype = self.fields.getvalue("doctype")
if self._doctype == "all":
self._doctype = None
if self._doctype:
try:
self._doctype = int(self._doctype)
except Exception:
self.bail()
if self._doctype not in [pair[0] for pair in self.doctypes]:
self.bail()
return self._doctype
@property
def doctypes(self):
"""Active document types for the form's picklist."""
if not hasattr(self, "_doctypes"):
query = self.Query("doc_type", "id", "name").order("name")
query.where("active = 'Y'")
query.where("xml_schema IS NOT NULL")
query.where("name NOT IN ('Filter', 'xxtest', 'schema')")
rows = query.execute(self.cursor).fetchall()
self._doctypes = [list(row) for row in rows]
return self._doctypes
@property
def rows(self):
"""Values for the report table.
Values have been scrubbed, so interpolation within the SQL
subquery is safe. SQL Server limitations prevent the use
of placeholders in subqueries.
"""
subquery = self.Query("doc_version", "id", "MAX(num) as num")
if self.doctype:
subquery.where(f"doc_type = {self.doctype:d}")
subquery.group("id")
if self.start:
subquery.where(f"dt >= '{self.start}'")
if self.end:
subquery.where(f"dt <= '{self.end} 23:59:59'")
subquery.alias("m")
fields = "v.id", "v.title", "v.num", "v.publishable"
query = self.Query("doc_version v", *fields).order("v.id")
query.join(subquery, "m.id = v.id", "m.num = v.num")
rows = []
for row in query.execute(self.cursor).fetchall():
rows.append([
self.Reporter.Cell(row.id, center=True),
self.Reporter.Cell(row.title),
self.Reporter.Cell(row.num, center=True),
self.Reporter.Cell(row.publishable, center=True),
])
return rows
if __name__ == "__main__":
"""Don't execute the script if loaded as a module."""
Control().run()