def indicator_number(indicator, geo_record, time=None): try: datapoint = DataPoint.objects.get(indicator=indicator, record=geo_record, time=time) value = datapoint.value_set.get(denominator__isnull=True) f_num = "%s" % util_format_number(value.number, data_type=indicator.data_type) #check for moe if value.moe: # we need to format the moe a ltitle differently div = Div() div.add_attr('data-original-title') div.attr.data_original_title = 'Margin of Error: %s' % util_format_number( value.moe, data_type=indicator.data_type) div.attr.className = 'value-cell' div.attr.rel = 'tooltip' div.inner_text = f_num icon = Span() icon.attr.className = 'moe-icon attr-tbl' icon.inner_text = '±' div.append_child(icon) return str(div) else: return f_num except (DataPoint.DoesNotExist, Value.DoesNotExist): return ''
def indicator_moe(indicator, geo_record, time): try: datapoint = DataPoint.objects.get(indicator=indicator, record=geo_record, time=time) value = datapoint.value_set.get(denominator__isnull=True) return util_format_number(value.moe, data_type=indicator.data_type) except (DataPoint.DoesNotExist, Value.DoesNotExist): return ''
def indicator_percent(indicator, geo_record, time=None): try: datapoint = DataPoint.objects.get(indicator=indicator, record=geo_record, time=time) value = datapoint.value_set.get(denominator__isnull=True) return "%s" % util_format_number(value.percent) except (DataPoint.DoesNotExist, Value.DoesNotExist): return ''
def indicator_number(indicator, geo_record, time=None): try: datapoint = DataPoint.objects.get(indicator=indicator, record=geo_record, time=time) value = datapoint.value_set.get(denominator__isnull=True) f_num = "%s" % util_format_number(value.number, data_type=indicator.data_type) #check for moe if value.moe: # we need to format the moe a ltitle differently div = Div() div.add_attr('data-original-title') div.attr.data_original_title = 'Margin of Error: %s' % util_format_number(value.moe, data_type=indicator.data_type) div.attr.className='value-cell' div.attr.rel='tooltip' div.inner_text = f_num icon = Span() icon.attr.className = 'moe-icon attr-tbl' icon.inner_text= '±' div.append_child(icon) return str(div) else: return f_num except (DataPoint.DoesNotExist, Value.DoesNotExist): return ''
def format_number(number, data_type='COUNT'): num = util_format_number(number, data_type=data_type) return num
def indicators_table(title, indicators, geo_record, data_domain): """ title: the name of the data domain indicators: A list of indicator objects get_record: the current GeoRecord data_domain: the current DataDomain """ import markdown try: indicators = indicators.order_by('display_name') except AttributeError: # its a single indicator indicators = [indicators] sorted_indicators = get_time_sorted_indicatorset(indicators, geo_record, data_domain) tables = '' # build tables for i in sorted_indicators: ind_set = sorted_indicators[i] tbl = Table() tbl.attr.className = "table table-bordered data-table" + " times_%s" % len( ind_set['times']) thead = tbl.add_header() thr = thead.add_row() thcol = thr.add_col() thcol.add_attr('data-original-title') thcol.attr.data_original_title = 'Click an Indicator name to map' thcol.add_attr('rel') thcol.attr.rel = "tooltip" thcol.attr.className = "field_lbl indicator-lbl" thcol.inner_text = 'Indicator' #thr.add_col(className='field_lbl indicator-lbl', inner_text='Indicator') for t in ind_set['times']: thr.add_col(colspan='2', className='field_lbl', inner_text=t) if ind_set['display_change']: thr.add_col(colspan='1', inner_text='Change') #thr.add_col(colspan='1', inner_text='Notes', className='notes-col') #tbody tbody = tbl.add_body() for i_set in ind_set['indicators']: umoe = False # keeps track of unacceptable moe ind = i_set['indicator'] i_vals = i_set['values'] i_times = i_set['indicator_times'] # times objs i_times.sort(key=lambda tm: tm.name, reverse=True) suppressed_val = False tbr = tbody.add_row() #tr tbr.attr.id = ind.id ind_col = tbr.add_col( className='indicator-name') # this is the indicator title ind_col.add_attr('data-original-title') ind_col.attr.data_original_title = 'Click to map' ind_col.attr.rel = "tooltip" cell_wrap = Div() cell_wrap.attr.className = "cell-wrap" cell_wrap.inner_html = '<a href="%s">%s</a>' % (i_set['href'], ind.display_name) ind_col.append_child(cell_wrap) #-----------------------------------------------------------------------------------notes notes = ind.get_notes() if notes: n_href = A() cell_wrap.append_child(n_href) n_href.add_attr('date-toggle') n_href.data_toggle = "modal" n_href.attr.className = "notes-icon" n_href.attr.href = "#%s_notes_modal" % ind.slug modal = Div() cell_wrap.append_child(modal) modal.attr.className = "modal hide" modal.attr.id = "%s_notes_modal" % ind.slug m_header = Div() m_header.attr.className = "modal-header" m_header.inner_html = "<h3>%s</h3>" % ind.display_name m_body = Div() m_body.attr.className = "modal-body" # append fields for n in notes: m_body.inner_html += u'<h4>%s</h4>' % n['label'] m_body.inner_html += u'<p>%s</p>' % mark_safe( markdown.markdown(n['text'])) modal.append_child(m_header, m_body) ###############################################Table values################################################33 for t in i_times: # values are keyed to time by the time id raw_val = i_vals[t.id] # Value Object if raw_val: # check if there is a value d_val = raw_val.to_dict() f_val = d_val['f_number'] if f_val == "-1": suppressed_val = True td = tbr.add_col() if raw_val.moe: # check to see if there is MOe, if so we need some fancy classes and attrs to display twitter bs(bootstrap that is) :D td.add_attr('data-original-title' ) # this is a custom attribute td.attr.rel = 'tooltip' td.attr.className = 'value moe' td.attr.data_original_title = "Margin of Error: %s" % util_format_number( raw_val.moe, ind.data_type) td.inner_html = '<span class="moe-icon">+/-</span>%s<span class="pr-moe">+/-%s</span>' % ( f_val, raw_val.moe) td.attr.colspan = '2' #check if moe is acceptable if raw_val.moe > raw_val.number: umoe = True td.attr.className += ' u-moe' # unacceptable moe td.attr.data_original_title = "+/-%s </br>The Margin of Error for this value is larger than the value making it unreliable. " % util_format_number( raw_val.moe, ind.data_type) td.inner_html = '<span class="moe-icon"><span class="u-moe"></span> +/-</span>%s<span class="pr-moe">+/-%s</span><div class="cell-wrap"></div>' % ( f_val, raw_val.moe) else: # no moe td.attr.className = 'value' td.attr.colspan = '2' td.inner_html = '<div class="cell-wrap">%s</div>' % f_val else: td = tbr.add_col(className="value empty none") td.attr.colspan = '2' # we need to insert a blank value cell here #tbr.add_col(className="value empty") #change if ind_set['display_change']: change_val = i_set['change'] # Value Obj if change_val and suppressed_val == False: #if change_val.number: # tbr.add_col(className='value change-num',inner_text=util_format_number(change_val.number, ind.data_type)) #else: # tbr.add_col(className='value empty no-change-num') if change_val.percent: col = tbr.add_col( className='value change-perc', inner_text="%s%%" % util_format_number(change_val.percent, "PERCENT")) if umoe: col.attr.className += ' u-moe' else: tbr.add_col(className='value empty no-change-perc') else: tbr.add_col(className="value empty change-none") #tbr.add_col(className="value empty change-none") #denominators if suppressed_val == False: for denom in i_set['denominators']: den = denom['denominator'] # the denominator obj den_vals = denom['values'] dtr = tbody.add_row(className='denom') # denom tr dtr.add_col( className='denom', inner_html= '<div class="cell-wrap"><a href="%s?denom=%s">...as %s</a></div>' % (i_set['href'], den.id, den.label)) # denom label # get vals with time keys for t in i_times: den_val = den_vals[t.id] if den_val: # check if None if den_val.percent: dtr.add_col(className="value denom denom-perc", inner_text="%s%%" % util_format_number( den_val.percent, "PERCENT")) else: dtr.add_col( className="value empty no-denom-perc") if den_val.number: dtr.add_col( className="value denom denom-divsor", inner_text="%s" % util_format_number( den_val.number, ind.data_type)) else: dtr_col( className="value empty no-denom-divisor") else: dtr.add_col(className="value empty denom-none") dtr.add_col(className="value empty denom-none") # denom_change if ind_set['display_change']: change_val = denom['change'] # Value Obj if change_val: # check if None #dtr.add_col(className='value denom-change-num',inner_text=util_format_number(change_val.number)) if change_val.percent: dtr.add_col( className='value denom-change-perc', inner_text="%spts" % util_format_number( change_val.percent, "PERCENT")) else: dtr.add_col( className="value empty no-denom-perc") else: dtr.add_col(className="value empty change-none") tables += str(tbl) return tables
def indicators_table(title, indicators, geo_record, data_domain): """ title: the name of the data domain indicators: A list of indicator objects get_record: the current GeoRecord data_domain: the current DataDomain """ import markdown try: indicators = indicators.order_by('display_name') except AttributeError: # its a single indicator indicators = [indicators] sorted_indicators = get_time_sorted_indicatorset(indicators, geo_record, data_domain) tables = '' # build tables for i in sorted_indicators: ind_set = sorted_indicators[i] tbl = Table() tbl.attr.className = "table table-bordered data-table" + " times_%s" % len(ind_set['times']) thead = tbl.add_header() thr = thead.add_row() thcol = thr.add_col() thcol.add_attr('data-original-title') thcol.attr.data_original_title = 'Click an Indicator name to map' thcol.add_attr('rel') thcol.attr.rel="tooltip" thcol.attr.className="field_lbl indicator-lbl" thcol.inner_text='Indicator' #thr.add_col(className='field_lbl indicator-lbl', inner_text='Indicator') for t in ind_set['times']: thr.add_col(colspan='2', className='field_lbl', inner_text=t) if ind_set['display_change']: thr.add_col(colspan='1', inner_text='Change') #thr.add_col(colspan='1', inner_text='Notes', className='notes-col') #tbody tbody = tbl.add_body() for i_set in ind_set['indicators']: umoe=False # keeps track of unacceptable moe ind = i_set['indicator'] i_vals = i_set['values'] i_times = i_set['indicator_times'] # times objs i_times.sort(key=lambda tm: tm.name, reverse=True) suppressed_val = False tbr = tbody.add_row() #tr tbr.attr.id = ind.id ind_col = tbr.add_col(className='indicator-name') # this is the indicator title ind_col.add_attr('data-original-title') ind_col.attr.data_original_title = 'Click to map' ind_col.attr.rel = "tooltip" cell_wrap = Div() cell_wrap.attr.className="cell-wrap" cell_wrap.inner_html = '<a href="%s">%s</a>' % (i_set['href'], ind.display_name) ind_col.append_child(cell_wrap) #-----------------------------------------------------------------------------------notes notes = ind.get_notes() if notes: n_href = A() cell_wrap.append_child(n_href) n_href.add_attr('date-toggle') n_href.data_toggle = "modal" n_href.attr.className = "notes-icon" n_href.attr.href = "#%s_notes_modal" % ind.slug modal = Div() cell_wrap.append_child(modal) modal.attr.className = "modal hide" modal.attr.id = "%s_notes_modal" % ind.slug m_header = Div() m_header.attr.className = "modal-header" m_header.inner_html ="<h3>%s</h3>" % ind.display_name m_body = Div() m_body.attr.className = "modal-body" # append fields for n in notes: m_body.inner_html += u'<h4>%s</h4>' % n['label'] m_body.inner_html += u'<p>%s</p>' % mark_safe(markdown.markdown(n['text'])) modal.append_child(m_header, m_body) ###############################################Table values################################################33 for t in i_times: # values are keyed to time by the time id raw_val = i_vals[t.id] # Value Object if raw_val: # check if there is a value d_val = raw_val.to_dict() f_val = d_val['f_number'] if f_val == "-1": suppressed_val = True td = tbr.add_col() if raw_val.moe: # check to see if there is MOe, if so we need some fancy classes and attrs to display twitter bs(bootstrap that is) :D td.add_attr('data-original-title') # this is a custom attribute td.attr.rel = 'tooltip' td.attr.className = 'value moe' td.attr.data_original_title = "Margin of Error: %s" % util_format_number(raw_val.moe, ind.data_type) td.inner_html = '<span class="moe-icon">+/-</span>%s<span class="pr-moe">+/-%s</span>' % (f_val, raw_val.moe) td.attr.colspan='2' #check if moe is acceptable if raw_val.moe > raw_val.number: umoe=True td.attr.className +=' u-moe' # unacceptable moe td.attr.data_original_title = "+/-%s </br>The Margin of Error for this value is larger than the value making it unreliable. " % util_format_number(raw_val.moe, ind.data_type) td.inner_html = '<span class="moe-icon"><span class="u-moe"></span> +/-</span>%s<span class="pr-moe">+/-%s</span><div class="cell-wrap"></div>' % (f_val, raw_val.moe) else: # no moe td.attr.className='value' td.attr.colspan='2' td.inner_html = '<div class="cell-wrap">%s</div>' % f_val else: td = tbr.add_col(className="value empty none") td.attr.colspan='2' # we need to insert a blank value cell here #tbr.add_col(className="value empty") #change if ind_set['display_change']: change_val = i_set['change'] # Value Obj if change_val and suppressed_val == False: #if change_val.number: # tbr.add_col(className='value change-num',inner_text=util_format_number(change_val.number, ind.data_type)) #else: # tbr.add_col(className='value empty no-change-num') if change_val.percent: col = tbr.add_col(className='value change-perc', inner_text="%s%%" % util_format_number(change_val.percent, "PERCENT")) if umoe: col.attr.className +=' u-moe' else: tbr.add_col(className='value empty no-change-perc') else: tbr.add_col(className="value empty change-none") #tbr.add_col(className="value empty change-none") #denominators if suppressed_val == False: for denom in i_set['denominators']: den = denom['denominator'] # the denominator obj den_vals = denom['values'] dtr = tbody.add_row(className='denom') # denom tr dtr.add_col(className='denom', inner_html='<div class="cell-wrap"><a href="%s?denom=%s">...as %s</a></div>' % (i_set['href'], den.id, den.label)) # denom label # get vals with time keys for t in i_times: den_val = den_vals[t.id] if den_val: # check if None if den_val.percent: dtr.add_col(className="value denom denom-perc",inner_text="%s%%" % util_format_number(den_val.percent, "PERCENT") ) else: dtr.add_col(className="value empty no-denom-perc") if den_val.number: dtr.add_col(className="value denom denom-divsor",inner_text="%s" % util_format_number(den_val.number,ind.data_type )) else: dtr_col(className="value empty no-denom-divisor") else: dtr.add_col(className="value empty denom-none") dtr.add_col(className="value empty denom-none") # denom_change if ind_set['display_change']: change_val = denom['change'] # Value Obj if change_val: # check if None #dtr.add_col(className='value denom-change-num',inner_text=util_format_number(change_val.number)) if change_val.percent: dtr.add_col(className='value denom-change-perc', inner_text="%spts" % util_format_number(change_val.percent, "PERCENT")) else: dtr.add_col(className="value empty no-denom-perc") else: dtr.add_col(className="value empty change-none") tables += str(tbl) return tables