-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlossaryTermAudioReviewReport.py
281 lines (234 loc) · 9.73 KB
/
GlossaryTermAudioReviewReport.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
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
#!/usr/bin/env python
"""Report on audio files with a specified review disposition.
"""
from cdrcgi import Controller, Reporter
class Control(Controller):
"""Script logic."""
SUBTITLE = "Glossary Term Audio Review Statistical Report"
INSTRUCTIONS = (
"Select a language and approval status for the term names "
"to include in the report. Optionally add start and/or "
"end dates for the term reviews to limit the size of the output."
)
LANGUAGES = "English", "Spanish"
STATUSES = "Approved", "Rejected", "Unreviewed"
REPORT_TYPES = (
"Full report showing terms",
"Summary report with status totals only",
)
def build_tables(self):
"""Create the full or summary report, as requested by the user."""
self.subtitle = f"{self.status} {self.language} Audio Pronunciations"
tables = [self.summary_table]
if self.type == "full":
tables += [audio_set.table for audio_set in self.sets]
return tables
def populate_form(self, page):
"""Add the fields to the form.
Pass:
page - HTMLPage object to which we add the fields
"""
fieldset = page.fieldset("Instructions")
fieldset.append(page.B.P(self.INSTRUCTIONS))
page.form.append(fieldset)
fieldset = page.fieldset("Select Language")
checked = True
for language in self.LANGUAGES:
opts = dict(value=language, checked=checked)
fieldset.append(page.radio_button("language", **opts))
checked = False
page.form.append(fieldset)
fieldset = page.fieldset("Select Status")
checked = True
for status in self.STATUSES:
opts = dict(value=status, checked=checked)
fieldset.append(page.radio_button("status", **opts))
checked = False
page.form.append(fieldset)
fieldset = page.fieldset("Select Full or Summary Report")
checked = True
for display in self.REPORT_TYPES:
value = display.split()[0].lower()
opts = dict(value=value, label=display, checked=checked)
fieldset.append(page.radio_button("type", **opts))
checked = False
page.form.append(fieldset)
fieldset = page.fieldset("Optional Start and End Dates")
fieldset.append(page.date_field("start_date"))
fieldset.append(page.date_field("end_date"))
page.form.append(fieldset)
def show_report(self):
"""Override the base class version in order to add style rules."""
css = (
"table { width: 500px; margin-top: 50px; }",
"table.set { width: 75%; }",
".center {width: 150px; }",
".set td:last-child { width: 200px; }",
)
self.report.page.add_css("\n".join(css))
self.report.send()
@property
def end(self):
"""Optional end to report's date range."""
return self.parse_date(self.fields.getvalue("end_date"))
@property
def language(self):
"""Language value selected from the form by the user."""
return self.fields.getvalue("language")
@property
def sets(self):
"""Audio pronunciation sets in scope for this report."""
if not hasattr(self, "_sets"):
fields = "z.id", "z.filename"
query = self.Query("term_audio_zipfile z", *fields).unique()
query.join("term_audio_mp3 m", "m.zipfile_id = z.id")
query.where(query.Condition("m.language", self.language))
query.where(query.Condition("m.review_status", self.status[0]))
if self.start:
query.where(query.Condition("m.review_date", self.start, ">="))
if self.end:
end = f"{str(self.end)[:10]} 23:59:59"
query.where(query.Condition("m.review_date", end, "<="))
rows = query.order("z.id").execute(self.cursor).fetchall()
self._sets = [AudioSet(self, row) for row in rows]
return self._sets
@property
def start(self):
"""Optional start to report's date range."""
return self.parse_date(self.fields.getvalue("start_date"))
@property
def status(self):
"""Status value selected from the form by the user."""
return self.fields.getvalue("status")
@property
def subtitle(self):
"""String displayed immediately under the page's main banner."""
if not hasattr(self, "_subtitle"):
self._subtitle = self.SUBTITLE
return self._subtitle
@subtitle.setter
def subtitle(self, value):
"""Make this modifiable on the fly."""
self._subtitle = value
@property
def summary_table(self):
"""Table showing the totals for each status."""
if not hasattr(self, "_summary_table"):
approved = rejected = unreviewed = 0
for audio_set in self.sets:
approved += audio_set.approved
rejected += audio_set.rejected
unreviewed += audio_set.unreviewed
row = approved, rejected, unreviewed
opts = dict(columns=self.STATUSES, caption="Status Totals")
self._summary_table = Reporter.Table([row], **opts)
return self._summary_table
@property
def type(self):
"""Report type selected by the user (full or summary)."""
return self.fields.getvalue("type")
class AudioSet:
"""Zip file containing glossary term audio pronunciation recordings."""
FIELDS = "m.term_name", "m.review_date", "u.fullname"
COLUMNS = "Term", "Review Date", "User"
def __init__(self, control, row):
"""Capture the caller's values.
Pass:
control - access to the database and the runtime report parameters
row - id and name for this set
"""
self.__control = control
self.__row = row
@property
def audio_files(self):
"""Audio recordings in this set which are in scope for the report."""
if not hasattr(self, "_audio_files"):
class MP3:
def __init__(self, row):
self.name = row.term_name
self.user = row.fullname
self.date = row.review_date
@property
def row(self):
return (
self.name,
str(self.date)[:19],
self.user,
)
status = self.control.status[0]
query = self.control.Query("term_audio_mp3 m", *self.FIELDS)
query.join("usr u", "u.id = m.reviewer_id")
query.where(query.Condition("m.zipfile_id", self.id))
query.where(query.Condition("m.language", self.control.language))
query.where(query.Condition("m.review_status", status))
if self.control.start:
start = self.control.start
query.where(query.Condition("m.review_date", start, ">="))
if self.control.end:
end = f"{str(self.control.end)[:10]} 23:59:59"
query.where(query.Condition("m.review_date", end, "<="))
query.order("m.term_name")
rows = query.execute(self.control.cursor).fetchall()
self._audio_files = [MP3(row) for row in rows]
return self._audio_files
@property
def control(self):
"""Access to the database and the options selected by the user."""
return self.__control
@property
def counts(self):
"""Totals for each status in this set."""
if not hasattr(self, "_counts"):
fields = "review_status", "COUNT(*)"
query = self.control.Query("term_audio_mp3", *fields)
query.where(query.Condition("zipfile_id", self.id))
query.where(query.Condition("language", self.control.language))
query.group("review_status")
rows = query.execute(self.control.cursor).fetchall()
self._counts = dict([tuple(row) for row in rows])
return self._counts
@property
def id(self):
"""Primary key for this set in its database row."""
return self.__row.id
@property
def name(self):
"""File name for this set."""
return self.__row.filename
@property
def subtotals(self):
"""Row in the report table for this set's subtotals."""
if not hasattr(self, "_subtotals"):
subtotals = []
for status in Control.STATUSES:
subtotals.append(f"{status}={self.counts.get(status[0], 0)}")
subtotals = "\xa0 ".join(subtotals)
language = self.control.language
subtotals = f"{language} names in this set:\xa0 {subtotals}"
cell = Reporter.Cell(subtotals, center=True, colspan=3)
self._subtotals = [cell]
return self._subtotals
@property
def table(self):
"""Report table for this set."""
if not hasattr(self, "_table"):
rows = [mp3.row for mp3 in self.audio_files]
rows.append(self.subtotals)
opts = dict(cols=self.COLUMNS, caption=self.name, classes="set")
self._table = Reporter.Table(rows, **opts)
return self._table
@property
def approved(self):
"""Number of approved recordings for the selected language."""
return self.counts.get("A", 0)
@property
def rejected(self):
"""Number of rejected recordings for the selected language."""
return self.counts.get("R", 0)
@property
def unreviewed(self):
"""Number of unreviewed recordings for the selected language."""
return self.counts.get("U", 0)
if __name__ == "__main__":
"""Don't run the script if loaded as a module."""
Control().run()