Add stats to boolean fields

This commit is contained in:
Frédéric Péters 2010-10-18 07:07:48 +00:00
parent 8546b65c56
commit 89065b34f8
1 changed files with 36 additions and 0 deletions

View File

@ -449,6 +449,42 @@ class BoolField(WidgetField):
else:
return ''
def stats(self, values):
no_records = len(values)
r = TemplateIO(html = True)
r += htmltext('<table class="stats">')
r += htmltext('<thead><tr><th colspan="3">')
r += self.label
r += htmltext('</th></tr></thead>')
options = (True, False)
r += htmltext('<tbody>')
for o in options:
r += htmltext('<tr>')
r += htmltext('<td class="label">')
if o is True:
r += _('Yes')
else:
r += _('No')
r += htmltext('</td>')
no = len([None for x in values if x.data.get(self.id) == str(o)])
if no_records:
r += htmltext('<td class="percent">')
r += ' %.2f %%' % (100.*no/no_records)
r += htmltext('</td>')
image_len = 300.*no/no_records
r += htmltext('<td>')
if image_len:
r += htmltext('<img class="bar" src="%simages/bar.png" height="20px" alt="" '
'width="%dpx" />' % (get_publisher().get_root_url(), image_len))
r += htmltext('</td>')
else:
r += htmltext('<td colspan="2"></td>')
#'NaN'
r += htmltext('</tr>')
r += htmltext('</tbody>')
r += htmltext('</table>')
return r.getvalue()
register_field_class(BoolField)