def render_table_pivot(ws, table): # Pivot values lines for i, (pivot_label, pivot_lvs) in enumerate(table.pivot_titles()): ws.write_merge(len(table.label_titles()), ensure_unicode(pivot_label), style=style_title) for label in pivot_lvs: ws.write_merge(len(table.value_titles()), ensure_unicode(label), style=style_title) if i == 0: ws.write_merge(len(table.value_titles()), u"Total", rowspan=len(table.pivot_labels), style=style_title) ws.newline() # Column titles line for t in table.label_titles(): ws.write(t and ensure_unicode(t) or None, style=style_title) for label in table.pivot_lvs(): for t in table.value_titles(): ws.write(ensure_unicode(t), style=style_title) for t in table.value_titles(): ws.write(ensure_unicode(t), style=style_title) ws.newline() ws.freeze_titles() # Table data for slice, labels, values, totals in table.rows(): for label in labels: write_label(ws, label) for v in values: ws.write(v.value, style=style_value) for t in totals: ws.write(t.value, style=style_total) ws.newline() # Totals row totals = table.totals() if totals is not None: for t in table.label_titles(): ws.write(None, style=style_total) for v in table.totals(): ws.write(v.value, style=style_total) ws.newline() ws.autofit()
def _render_table_1d(table, links): columns = [] for t in table.label_titles(): col = OrderedDict([ ('label', ensure_unicode(t)), ('type', "label"), ('links', dict( drop_axis=links.add(table.drop_axis_url(t)))), ]) columns.append(col) if t.allow_pivot: col['links']['pivot'] = links.add(table.pivot_url(t)) if columns: columns[0]['links']['reset_order'] = links.add(table.reset_order_url()) tcols = [] for t in table.value_titles(): col = OrderedDict([ ('label', ensure_unicode(t)), ('type', "value"), ('total', None), ('links', dict( order=links.add(table.order_url(t)), order_asc=links.add(table.order_asc_url(t)), hide=links.add(table.hide_value_url(t)), )) ]) columns.append(col) tcols.append(col) rows = [] for row in table.rows(): jrow = [] rows.append(jrow) for label in row.labels: jrow.append([ ensure_unicode(label), {'filter': links.add(table.filter_url(label))}, ]) for v in row.values: jrow.append(v.pretty) for col, tot in zip(tcols, table.totals()): col['total'] = tot.pretty return columns, rows
def __init__(self, name, extract=None, title=None, pretty=None): self._name = name if extract is not None: self.extract = extract self.title = ensure_unicode(title or name.replace("_", " ").title()) if pretty is not None: self.pretty = pretty
def render_nav_json(panel): axes = [] filters = [] values = [] widgets = [] olddim = -1 for label, query in panel.nav.iter_expansions(): if label.dimension != olddim: olddim = label.dimension dimension = OrderedDict([ ('dimension', label.dimension or 'Other axes'), ('axes', []), ]) axes.append(dimension) axes[-1]['axes'].append(OrderedDict([ ('label', ensure_unicode(label)), ('url', panel.get_url(query) if query is not None else None) ])) for f in panel.nav.iter_filters(): filters.append(OrderedDict([ ('label', u"%s %s %s" % (f.pretty_name, f.pretty_op, f.pretty_value)), ('drop_url', panel.get_url(f.query_without)), ])) for label, query in panel.nav.hidden_values(): values.append(OrderedDict([ ('label', ensure_unicode(label)), ('show_url', panel.get_url(query)), ])) for w in panel.widgets: widgets.append(_render_widget(w, panel)) rv = [] if axes: rv.append(('axes', axes)) if filters: rv.append(('filters', filters)) if values: rv.append(('values', values)) if widgets: rv.append(('widgets', widgets)) return OrderedDict(rv)
def _encode(self, s): """Escape unsafe characters with backslashes. Unsafe means in the context of the query language, i.e. if we use ',' as separator, the ',' can't be in a value. The result is usually urlquotes to make the returned string valid in an url. """ s = ensure_unicode(s) return bsescape(s, '/:')
def _render_pages(table, link): rv = [] for label, n, current in table.pages(): url = table.table.to_string_page(n) if n is not None else None rv.append(OrderedDict([ ('label', ensure_unicode(label)), ('url', link.add(url)), ])) if current: rv[-1]['current'] = True return rv
def f(self, query, *args): # the filter syntax is # f:LABEL:OP:VALUE1[:VALUE2...] # f:LABEL:VALUE (implies OP = eq) if len(args) == 2: name, value = args op = 'eq' elif len(args) == 3: name, op, value = args if op in MULTI_ARG_OPS: value = (value,) elif len(args) >= 3: name, op = args[:2] value = args[2:] if op not in MULTI_ARG_OPS: raise errors.QueryError( "bad number of arguments for operator '%s': %d" % (op, len(args) - 2)) else: raise errors.QueryError( "bad number of arguments for a filter: %d" % len(args)) # i have name, op, value here # value is a tuple for MULTI_ARG_OPS, a string for the others label = self.cubedef.get_label(name) if isinstance(value, tuple): value = frozenset(label.parse(ensure_unicode(v)) for v in value) else: value = label.parse(ensure_unicode(value)) return query.add_filter(name, value, operator=op)
def render_table_1d(ws, table): for t in table.label_titles(): ws.write(ensure_unicode(t), style=style_title) for t in table.value_titles(): ws.write(ensure_unicode(t), style=style_title) ws.newline() ws.freeze_titles() for slice, labels, values in table.rows(): for label in labels: write_label(ws, label) for v in values: ws.write(v.value, style=style_value) ws.newline() totals = table.totals() if totals is not None: for t in table.label_titles(): ws.write(None, style=style_total) for v in table.totals(): ws.write(v.value, style=style_total) ws.newline() ws.autofit()
def unparse(self, v): return ensure_unicode(v)
def pretty(self, d, record=None): return d and ensure_unicode(d.year) or 'Unknown'
def unparse(self, v): if isinstance(v, str): return ensure_unicode(v) return ensure_unicode(v)
def __unicode__(self): return ensure_unicode(self.title)
def pretty(self, value, record=None): if isinstance(value, str): return ensure_unicode(value) return ensure_unicode(value)
def _render_table_pivot(table, links): pivots = [] for pivot_label, pivot_lvs in table.pivot_titles(): pivots.append(OrderedDict([ ('label', ensure_unicode(pivot_label)), ('values', []), ('links', dict( pivot=links.add(table.pivot_url(pivot_label)), drop_axis=links.add(table.drop_axis_url(pivot_label)), )), ])) for label in pivot_lvs: pivots[-1]['values'].append(OrderedDict([ ("label", ensure_unicode(label)), ("links", dict( filter=links.add(table.filter_url(label)), hide=links.add(table.hide_labeled_value_url(label)), )) ])) columns = [] for t in table.label_titles(): if t is None: continue columns.append(OrderedDict([ ('label', ensure_unicode(t)), ('type', "label"), ('links', dict( pivot=links.add(table.pivot_url(t)), drop_axis=links.add(table.drop_axis_url(t)))), ])) if columns: columns[0]['links']['reset_order'] = links.add(table.reset_order_url()) tcols = [] for pv, lvs in enumerate(table.pivot_lvs()): for t in table.value_titles(): col = OrderedDict([ ('label', ensure_unicode(t)), ('pivot_value', pv), ('type', "value"), ('total', None), ('links', dict( order=links.add(table.order_url(t, lvs)), order_asc=links.add(table.order_asc_url(t, lvs)), hide=links.add(table.hide_value_url(t)), )) ]) columns.append(col) tcols.append(col) for t in table.value_titles(): col = OrderedDict([ ('label', ensure_unicode(t)), ('type', "total"), ('total', None), ('links', dict( order=links.add(table.order_url(t)), order_asc=links.add(table.order_asc_url(t)), hide=links.add(table.hide_value_url(t)), )) ]) columns.append(col) tcols.append(col) for col, tot in zip(tcols, table.totals()): col['total'] = tot.pretty rows = [] for row in table.rows(): jrow = [] rows.append(jrow) for label in row.labels: if label is None: continue jrow.append([ ensure_unicode(label), {'filter': links.add(table.filter_url(label))}, ]) for v in row.values: jrow.append(v.pretty) for t in row.totals: jrow.append(t.pretty) return pivots, columns, rows