fields: give out statistics even when data source is json (#6881)

This commit is contained in:
Frédéric Péters 2015-04-01 16:24:10 +02:00
parent d899cb5950
commit 128978d782
1 changed files with 16 additions and 5 deletions

View File

@ -786,23 +786,34 @@ register_field_class(DateField)
def item_items_stats(field, values):
if field.data_source:
options = data_sources.get_items(field.data_source)
else:
options = field.items or []
if len(options) == 0:
return None
no_records = len(values)
r = TemplateIO(html = True)
r += htmltext('<table class="stats">')
r += htmltext('<thead><tr><th colspan="4">')
r += field.label
r += htmltext('</th></tr></thead>')
options = field.items or []
r += htmltext('<tbody>')
for o in options:
for option in options:
r += htmltext('<tr>')
r += htmltext('<td class="label">')
r += o
if type(option) in (tuple, list):
option_label = option[1]
option_value = str(option[0])
else:
option_label = option
option_value = option
r += option_label
r += htmltext('</td>')
if field.type == 'item':
no = len([None for x in values if x.data.get(field.id) == o])
no = len([None for x in values if x.data.get(field.id) == option_value])
else:
no = len([None for x in values if o in (x.data.get(field.id) or [])])
no = len([None for x in values if option_value in (x.data.get(field.id) or [])])
if no_records:
r += htmltext('<td class="percent">')
r += ' %.2f %%' % (100.*no/no_records)