-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom_formatters.py
164 lines (129 loc) · 6.01 KB
/
custom_formatters.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
from cubes.formatters import Formatter,CrossTableFormatter
try:
import jinja2
except ImportError:
from .common import MissingPackage
jinja2 = MissingPackage("jinja2", "Templating engine")
def _jinja_env():
"""Create and return cubes jinja2 environment"""
loader = jinja2.loaders.FileSystemLoader('templates')
env = jinja2.Environment(loader=loader)
return env
class FilterTableFormatter(Formatter):
mime_type = "text/html"
def __init__(self, table_style=None):
super(FilterTableFormatter, self).__init__()
self.env = _jinja_env()
self.template = self.env.get_template("filter_table.html")
self.table_style = table_style
def format(self, members, dimension, hierarchy, level):
levels = hierarchy.levels_for_depth(hierarchy.level_index(level) + 1)
pattern = '/'.join(['%%(%s)s' % (hlevel.label_attribute.ref) for hlevel in levels])
key_pattern = '~'.join(['%%(%s)s' % (hlevel.key.ref) for hlevel in levels])
labels = [(pattern % member,key_pattern % member) for member in members]
print labels
output = self.template.render(labels=labels, level_label=level.label,
table_style=self.table_style)
return output
class SummaryTableFormatter(Formatter):
parameters = [
{
"name": "aggregates_on",
"type": "string",
"label": "Localtion of measures. Can be columns, rows or "
"cells",
"scope": "formatter",
},
{
"name": "table_style",
"description": "CSS style for the table"
}
]
mime_type = "text/html"
def __init__(self, aggregates_on="columns", table_style=None):
"""Create a simple HTML table formatter. See `CrossTableFormatter` for
information about arguments."""
if aggregates_on not in ["columns", "rows"]:
raise ArgumentError("aggregates_on sohuld be either 'columns' "
"or 'rows', is %s" % aggregates_on)
super(SummaryTableFormatter, self).__init__()
self.env = _jinja_env()
self.template = self.env.get_template("summary_table.html")
self.table_style = table_style
self.aggregates_on = aggregates_on
def format(self, result, aggregates=None, aggregates_on=None):
aggregates_on = aggregates_on or self.aggregates_on
columns = []
rows = []
data = []
cube = result.cell.cube
aggregates = cube.get_aggregates(aggregates)
for agg in aggregates:
if aggregates_on == "rows":
rows.append(agg.label or agg.name)
data.append([result.summary[agg.ref()]])
else:
columns.append(agg.label or agg.name)
if len(data) != 0:
data[0].append(result.summary[agg.ref()])
else:
data.append([result.summary[agg.ref()]])
output = self.template.render(columns=columns, rows=rows, data=data,
table_style=self.table_style)
return output
class GraphFormatter(Formatter):
def format(self, result, request_object, aggregates_on="columns"):
print "levels %s" % result.levels
cross_table = CrossTableFormatter().format(request_object['cube'],result,request_object['onrows']['labels'],request_object['oncolumns']['labels']
,request_object['aggregates']['labels'],aggregates_on)
result = {
"resultset":[
],
"metadata":[]
};
print cross_table
cross_table = eval(cross_table)
def populate_metadata(series,category=False):
print "request_object %s" % request_object
print "lo k debe salir %s" % [level_object[2].label for level_object in request_object[series]['objects']]
result['metadata'].append({
"colIndex":0,
"colType":"String",
"colName":"%s" % '~'.join([level_object[2].label for level_object in request_object[series]['objects']])
})
series_index = 1
if category:
result['metadata'].append({
"colIndex":1,
"colType":"String",
"colName":"%s" % '~'.join([level_object[2].label for level_object in request_object[category]['objects']])
})
series_index+= 1
result['metadata'].append({
"colIndex":series_index,
"colType":"Numeric",
"colName":"Value"
})
if cross_table['rows'] or cross_table['columns']:
if cross_table['rows'][0]:
if cross_table['columns'][0]:
populate_metadata('oncolumns','onrows')
for head, row in zip(cross_table['rows'], cross_table['data']):
for index,column in enumerate(cross_table['columns']):
data = ["~".join([str(c) for c in column])]
data.append("~".join([str(h) for h in head]))
data.append(float(row[index]))
result['resultset'].append(data)
else:
populate_metadata('onrows')
for head, row in zip(cross_table['rows'], cross_table['data']):
data = ["~".join([str(h) for h in head])]
data.append(float(row[0]))
elif cross_table['columns'][0]:
populate_metadata('oncolumns')
row = cross_table['data'][0]
for index,column in enumerate(cross_table['columns']):
data = data = ["~".join([str(c) for c in column])]
data.append(float(row[index]))
result['resultset'].append(data)
return result