This repository was archived by the owner on May 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDateLastModified.py
More file actions
executable file
·260 lines (215 loc) · 8.8 KB
/
DateLastModified.py
File metadata and controls
executable file
·260 lines (215 loc) · 8.8 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
"""Report documents last modified during a specified time period.
"""
import datetime
from functools import cached_property
from cdrcgi import Controller
class Control(Controller):
"""Top-level report logic."""
SUBTITLE = "Date Last Modified"
COLUMNS = (
("Date Last Modified", "100px"),
("CDR ID", "150px"),
("Document Title", "500px"),
)
def build_tables(self):
"""Create the table (or tables) for the report."""
if isinstance(self.doctype, self.Doctype):
return self.doctype.table
tables = []
for doctype in self.doctype:
if doctype.docs:
tables.append(doctype.table)
if not tables:
self.bail("No documents match the selected date range")
return tables
def populate_form(self, page):
"""Ask the user for the report parameters.
Pass:
page - HTMLPage object on which we place the fields
"""
fieldset = page.fieldset("Select Document Type")
opts = dict(value="all_types", checked=True)
fieldset.append(page.radio_button("doctype", **opts))
doctypes = sorted(self.doctypes.items(), key=lambda t: t[1].lower())
for value, label in doctypes:
opts = dict(value=value, label=label)
fieldset.append(page.radio_button("doctype", **opts))
page.form.append(fieldset)
fieldset = page.fieldset("Date Range For Report")
fieldset.append(page.date_field("start", value=self.range.start))
fieldset.append(page.date_field("end", value=self.range.end))
page.form.append(fieldset)
page.add_output_options(default="html")
@property
def columns(self):
"""Column headers shared by all the tables."""
if not hasattr(self, "_columns"):
self._columns = []
for label, width in self.COLUMNS:
self._columns.append(self.Reporter.Column(label, width=width))
return self._columns
@property
def doctype(self):
"""The `Doctype` object(s) for the types selected by the user."""
if not hasattr(self, "_doctype"):
doctype = self.fields.getvalue("doctype")
if doctype.isdigit():
name = self.doctypes[int(doctype)]
self._doctype = self.Doctype(self, doctype, name)
else:
items = list(self.doctypes.items())
doctypes = sorted(items, key=lambda t: t[1].lower())
self._doctype = []
for id, name in doctypes:
self._doctype.append(self.Doctype(self, id, name))
return self._doctype
@property
def doctypes(self):
"""Dictionary of active CDR document types, indexed by ID."""
if not hasattr(self, "_doctypes"):
query = self.Query("doc_type t", "t.id", "t.name")
query.where("active = 'Y'")
names = {}
for row in query.execute(self.cursor):
if row.name.strip():
names[row.name] = row.id
query = self.Query("query_term", "path").unique()
query.where("path LIKE '%/DateLastModified'")
query.where("int_val IS NOT NULL")
self._doctypes = {}
for row in query.execute(self.cursor):
name = row.path.strip("/").split("/")[0]
id = names.get(name)
if id:
self._doctypes[id] = name
return self._doctypes
@cached_property
def end(self):
"""Ending date for the report's date range."""
return self.parse_date(self.fields.getvalue("end"))
@property
def range(self):
"""Default date range for report."""
if not hasattr(self, "_range"):
today = datetime.date.today()
start = today - datetime.timedelta(7)
class Range:
def __init__(self, start, end):
self.start = start
self.end = end
self._range = Range(start, today)
return self._range
@cached_property
def start(self):
"""Starting date for the report's date range."""
return self.parse_date(self.fields.getvalue("start"))
@property
def subtitle(self):
if not hasattr(self, "_subtitle"):
subtitle = self.SUBTITLE
if self.request == "Submit":
if self.start:
if self.end:
subtitle = f"{subtitle} ({self.start} to {self.end})"
else:
subtitle = f"{subtitle} (since {self.start})"
elif self.end:
subtitle = f"{subtitle} (through {self.end})"
self._subtitle = subtitle
return self._subtitle
@property
def title(self):
"""Override for the Excel version of the report."""
if self.request == "Submit" and self.format == "excel":
return self.SUBTITLE
return self.TITLE
class Doctype:
"""Collect information about docs of this document type."""
def __init__(self, control, id, name):
"""Capture the caller's values."""
self.__control = control
self.__id = id
self.__name = name
@property
def control(self):
"""Access to the database and the report options."""
return self.__control
@property
def docs(self):
"""Sequence of `Doc` objects for the report."""
if not hasattr(self, "_docs"):
fields = "MAX(m.value) AS last_mod", "d.id", "d.title"
query = self.control.Query("query_term m", *fields).unique()
query.join("document d", "d.id = m.doc_id")
if self.start:
query.where(query.Condition("m.value", self.start, ">="))
if self.end:
query.where(query.Condition("m.value", self.end, "<="))
query.where(query.Condition("m.path", self.path, "LIKE"))
query.order("MAX(m.value)", "d.id")
query.group("d.id", "d.title")
rows = query.execute(self.control.cursor).fetchall()
self._docs = [self.Doc(self.control, row) for row in rows]
return self._docs
@property
def end(self):
"""Ending date/time for the report's date range."""
return f"{self.control.end} 23:59:59"
@property
def name(self):
"""String for the document type's name."""
return self.__name
@property
def path(self):
"""Pattern string for searching the query_term table."""
return f"/{self.name}/%DateLastModified"
@property
def start(self):
"""Starting date for the report's date range."""
return str(self.control.start)
@property
def table(self):
"""`Table` object for this document type."""
if not hasattr(self, "_table"):
rows = [doc.row for doc in self.docs]
opts = dict(columns=self.control.columns, caption=self.name)
if self.control.format == "excel":
opts["sheet_name"] = self.name
self._table = self.control.Reporter.Table(rows, **opts)
return self._table
class Doc:
"""CDR document with its most recent "date last modified" value."""
def __init__(self, control, row):
"""Save the caller's values."""
self.__control = control
self.__row = row
@property
def control(self):
"""Access to classes needed to build the row's cells."""
return self.__control
@property
def id(self):
"""Formatter CDR ID."""
return f"CDR{self.__row.id:010d}"
@property
def modified(self):
"""Latest "date last modified" value for the document."""
return self.__row.last_mod
@property
def row(self):
"""Sequence of `Cell` objects for the document's report row."""
if not hasattr(self, "_row"):
self._row = (
self.control.Reporter.Cell(self.modified, center=True),
self.control.Reporter.Cell(self.id, center=True),
self.control.Reporter.Cell(self.title),
)
return self._row
@property
def title(self):
"""The CDR document's title."""
return self.__row.title
if __name__ == "__main__":
"""Don't execute script if loaded as a module."""
Control().run()