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 pathGlossaryNameDocsModified.py
More file actions
executable file
·367 lines (297 loc) · 12.4 KB
/
GlossaryNameDocsModified.py
File metadata and controls
executable file
·367 lines (297 loc) · 12.4 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
"""Show which glossary name documents have changed recently.
"""
from cdrcgi import Controller
from cdrapi.docs import Doc
import datetime
class Control(Controller):
"""Access to database, session, and report/form building."""
SUBTITLE = "GTN Documents Modified Report"
NAME_LABELS = {"en": "Term Name", "es": "Translated Term Name"}
LANGUAGES = (("en", "English"), ("es", "Spanish"))
STATUS_PATHS = dict(
en="/GlossaryTermName/TermNameStatus",
es="/GlossaryTermName/TranslatedName/TranslatedNameStatus",
)
AUDIENCE_PATHS = dict(
en="/GlossaryTermConcept/TermDefinition/Audience",
es="/GlossaryTermConcept/TranslatedTermDefinition/Audience",
)
CONCEPT_PATH = "/GlossaryTermName/GlossaryTermConcept/@cdr:ref"
def build_tables(self):
"""Assemble the table for the rpoert."""
opts = dict(columns=self.columns, sheet_name="GlossaryTerm")
return self.Reporter.Table(self.rows, **opts)
def populate_form(self, page):
"""Request the information we need for this report.
Pass:
page - HTMLPage object with which the form is built
"""
end = datetime.date.today()
start = end - datetime.timedelta(7)
fieldset = page.fieldset("Date Range")
fieldset.append(page.date_field("start_date", value=start))
fieldset.append(page.date_field("end_date", value=end))
page.form.append(fieldset)
fieldset = page.fieldset("Language")
checked = True
for value, label in self.LANGUAGES:
opts = dict(value=value, label=label, checked=checked)
fieldset.append(page.radio_button("language", **opts))
checked = False
page.form.append(fieldset)
fieldset = page.fieldset("Audience")
for value in self.AUDIENCES:
opts = dict(value=value, checked=True)
fieldset.append(page.checkbox("audience", **opts))
page.form.append(fieldset)
fieldset = page.fieldset("Term Status(es)")
for value in self.statuses:
fieldset.append(page.checkbox("status", value=value, checked=True))
page.form.append(fieldset)
@property
def audience(self):
"""Audience(s) selected for the report."""
if not hasattr(self, "_audience"):
self._audience = self.fields.getlist("audience")
for audience in self._audience:
if audience not in self.AUDIENCES:
self.bail()
return self._audience
@property
def columns(self):
"""Column headers for the report."""
return (
self.Reporter.Column("CDR ID", width="70px"),
self.Reporter.Column(self.name_label, width="350px"),
self.Reporter.Column("Date Last Modified", width="100px"),
self.Reporter.Column("Publishable?", width="100px"),
self.Reporter.Column("Date First Published", width="100px"),
self.Reporter.Column("Last Comment", width="450px"),
self.Reporter.Column("Date Last Publishable", width="100px")
)
@property
def docs(self):
"""Term name documents selected for the report."""
query = self.Query("doc_version v", "v.id", "MAX(v.num) AS num")
query.join("doc_type t", "t.id = v.doc_type")
query.join("active_doc d", "d.id = v.id")
query.where("t.name = 'GlossaryTermName'")
if self.start:
query.where(query.Condition("v.dt", self.start, ">="))
if self.end:
query.where(query.Condition("v.dt", f"{self.end} 23:59:59", "<="))
if self.audience:
query.join("query_term c", "c.doc_id = d.id")
query.where(f"c.path = '{self.CONCEPT_PATH}'")
query.join("query_term a", "a.doc_id = c.int_val")
query.where(f"a.path = '{self.AUDIENCE_PATHS[self.language]}'")
query.where(query.Condition("a.value", self.audience, "IN"))
query.unique()
if self.status:
query.join("query_term s", "s.doc_id = d.id")
query.where(f"s.path = '{self.STATUS_PATHS[self.language]}'")
query.where(query.Condition("s.value", self.status, "IN"))
query.unique()
query.group("v.id")
query.log()
rows = query.execute(self.cursor).fetchall()
docs = [GlossaryTermName(self, row) for row in rows]
return sorted(docs)
@property
def end(self):
"""End of the date range for the report."""
return self.parse_date(self.fields.getvalue("end_date"))
@property
def format(self):
"""Override to get an Excel report."""
return "excel"
@property
def language(self):
"""Language code selected for the report (en or es)."""
if not hasattr(self, "_language"):
self._language = self.fields.getvalue("language")
if self._language not in {"en", "es"}:
self.bail
return self._language
@property
def name_label(self):
"""Custom column header, depending on the report's language."""
return self.NAME_LABELS.get(self.language)
@property
def rows(self):
"""Values for the report table."""
rows = []
for doc in self.docs:
rows += doc.rows
return rows
@property
def start(self):
"""Beginning of the date range for the report."""
return self.parse_date(self.fields.getvalue("start_date"))
@property
def status(self):
"""Term status(es) selected by the user for the report."""
if not hasattr(self, "_status"):
self._status = []
for status in self.fields.getlist("status"):
if status not in self.statuses:
self.bail()
self._status.append(status)
return self._status
@property
def statuses(self):
"""Valid values for term name status checkboxes."""
if not hasattr(self, "_statuses"):
paths = tuple(self.STATUS_PATHS.values())
query = self.Query("query_term", "value").unique()
query.where(query.Condition("path", paths, "IN"))
rows = query.execute(self.cursor).fetchall()
statuses = []
for row in rows:
status = (row.value or "").strip()
if status:
statuses.append(status)
self._statuses = sorted(statuses)
return self._statuses
class GlossaryTermName:
"""Information needed for a glossary term's report rows."""
def __init__(self, control, row):
"""Save the caller's values.
Pass:
control - access to the session and report-building tools
row - values from the database query
"""
self.__control = control
self.__row = row
def __lt__(self, other):
"""Make the documents sortable by document title."""
return self.doc.title < other.doc.title
@property
def control(self):
"""Access to the login session and report-building tools."""
return self.__control
@property
def date_first_published(self):
"""Date this document was first published."""
if not hasattr(self, "_date_first_published"):
self._date_first_published = ""
first_pub = self.doc.first_pub
if first_pub:
self._date_first_published = str(first_pub)[:10]
return self._date_first_published
@property
def date_last_publishable(self):
"""Date of the most recent publishable version, if any."""
if not hasattr(self, "_date_last_publishable"):
self._date_last_publishable = ""
query = self.control.Query("doc_version", "MAX(dt) AS dt")
query.where("publishable = 'Y'")
query.where(query.Condition("id", self.doc.id))
row = query.execute(self.control.cursor).fetchone()
if row and row.dt:
self._date_last_publishable = str(row.dt)[:10]
return self._date_last_publishable
@property
def doc(self):
"""`Doc` object for the glossary term name document."""
if not hasattr(self, "_doc"):
opts = dict(id=self.__row.id, version=self.__row.num)
self._doc = Doc(self.control.session, **opts)
return self._doc
@property
def names(self):
"""Name strings (with comments) pulled from the document."""
tag = "TermName" if self.control.language == "en" else "TranslatedName"
names = []
for node in self.doc.root.findall(tag):
names.append(self.Name(self, node))
return names
@property
def publishable(self):
"""String 'Y' or 'N' depending on whether the version is publshable."""
if not hasattr(self, "_publishable"):
self._publishable = "Y" if self.doc.publishable else "N"
return self._publishable
@property
def rows(self):
"Create a row for each of the term's name strings"
return [name.row for name in self.names]
class Name:
"""A Glossary term can have multiple names, each with a comment."""
def __init__(self, term, node):
"""Save the caller's values.
Pass:
term - object for the CDR document containing the name string
node - portion of the document for this name string
"""
self.__term = term
self.__node = node
@property
def comment(self):
"""First comment in the block with this name string."""
if not hasattr(self, "_comment"):
self._comment = ""
node = self.__node.find("Comment")
if node is not None:
self._comment = self.Comment(node)
return self._comment
@property
def date_last_modified(self):
"""Date the name string for this report was last modified."""
if not hasattr(self, "_date_last_modified"):
self._date_last_modified = ""
node = self.__node.find("DateLastModified")
self._date_last_modified = Doc.get_text(node, "")[:10]
return self._date_last_modified
@property
def row(self):
"""Values for this name string.
Each name for a term gets its own row in the report, repeating
the information common to the term in each row for the term.
"""
Cell = self.__term.control.Reporter.Cell
return (
Cell(self.__term.doc.id, center=True),
self.string,
Cell(self.date_last_modified, center=True),
Cell(self.__term.publishable, center=True),
Cell(self.__term.date_first_published, center=True),
str(self.comment),
Cell(self.__term.date_last_publishable, center=True)
)
@property
def string(self):
"""Term name string for this term."""
return Doc.get_text(self.__node.find("TermNameString"), "")
class Comment:
"Subclass holding text and metadata for a definition comment"
def __init__(self, node):
"""Remember the caller's value.
Pass:
node - element containing the definition comment
"""
self.__node = node
def __str__(self):
"""Serialize the comment's values."""
args = self.date, self.user, self.audience, self.text
return "[date: {}; user; {}; audience: {}] {}".format(*args)
@property
def text(self):
"""String for the comment text."""
return Doc.get_text(self.__node)
@property
def date(self):
"""String for the date the comment was entered."""
return self.__node.get("date")
@property
def audience(self):
"""Is the comment meant for internal or external viewers?"""
return self.__node.get("audience")
@property
def user(self):
"""Account name of the user entering the comment."""
return self.__node.get("user")
if __name__ == "__main__":
"""Don't execute the script if loaded as a module."""
Control().run()