json export in grid, thanks Alan

This commit is contained in:
mdipierro
2013-01-16 09:41:36 -06:00
parent 8f79000d2d
commit 3a1c03894f
2 changed files with 24 additions and 25 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.4.1-alpha.2+timestamp.2013.01.15.22.22.06
Version 2.4.1-alpha.2+timestamp.2013.01.16.09.40.52
+23 -24
View File
@@ -1991,6 +1991,7 @@ class SQLFORM(FORM):
csv=(ExporterCSV, 'CSV'),
xml=(ExporterXML, 'XML'),
html=(ExporterHTML, 'HTML'),
json=(ExporterJSON, 'JSON'),
tsv_with_hidden_cols=
(ExporterTSV, 'TSV (Excel compatible, hidden cols)'),
tsv=(ExporterTSV, 'TSV (Excel compatible)'))
@@ -2938,11 +2939,10 @@ class ExporterCSV(ExportClass):
def export(self):
if self.rows:
return str(self.rows)
return self.rows.as_csv()
else:
return ''
class ExporterHTML(ExportClass):
label = 'HTML'
file_ext = "html"
@@ -2952,18 +2952,10 @@ class ExporterHTML(ExportClass):
ExportClass.__init__(self, rows)
def export(self):
out = cStringIO.StringIO()
out.write('<html>\n<body>\n<table>\n')
if self.rows:
colnames = [a.split('.') for a in self.rows.colnames]
for row in self.rows.records:
out.write('<tr>\n')
for col in colnames:
out.write('<td>' + str(row[col[0]][col[1]]) + '</td>\n')
out.write('</tr>\n')
out.write('</table>\n</body>\n</html>')
return str(out.getvalue())
return self.rows.xml()
else:
return '<html>\n<body>\n<table>\n</table>\n</body>\n</html>'
class ExporterXML(ExportClass):
label = 'XML'
@@ -2974,15 +2966,22 @@ class ExporterXML(ExportClass):
ExportClass.__init__(self, rows)
def export(self):
out = cStringIO.StringIO()
out.write('<rows>\n')
if self.rows:
colnames = [a.split('.') for a in self.rows.colnames]
for row in self.rows.records:
out.write('<row>\n')
for col in colnames:
out.write(
'<%s>' % col + str(row[col[0]][col[1]]) + '</%s>\n' % col)
out.write('</row>\n')
out.write('</rows>')
return str(out.getvalue())
return self.rows.as_xml()
else:
return '<rows></rows>'
class ExporterJSON(ExportClass):
label = 'JSON'
file_ext = "json"
content_type = "application/json"
def __init__(self, rows):
ExportClass.__init__(self, rows)
def export(self):
if self.rows:
return self.rows.as_json()
else:
return 'null'