From 9d2ed04ed407133c767cefbdaba5b22e2e149f9d Mon Sep 17 00:00:00 2001 From: mdipierro Date: Tue, 17 Jul 2012 16:15:29 -0500 Subject: [PATCH] added TSV exporter to grid, thanks niphlod --- VERSION | 2 +- gluon/sqlhtml.py | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index 198a1058..39753ff6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.00.0 (2012-07-17 16:11:31) dev +Version 2.00.0 (2012-07-17 16:15:26) dev diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index c6b4d1a8..19f6423b 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -1714,7 +1714,9 @@ class SQLFORM(FORM): exportManager = dict(csv_with_hidden_cols=(ExporterCsv,'csv with hidden cols'), csv=ExporterCsv, - html=ExporterHtml) + html=ExporterHtml, + tsv_with_hidden_cols=(ExporterTsv, 'tsv with hidden cols'), + tsv=ExporterTsv) if not exportclasses is None: exportManager.update(exportclasses) @@ -1734,7 +1736,7 @@ class SQLFORM(FORM): orderby=db[tablename][fieldname] table_fields = [f for f in fields if f._tablename in tablenames] - if export_type =='csv_with_hidden_cols': + if export_type in ('csv_with_hidden_cols','tsv_with_hidden_cols'): if request.vars.keywords: try: dbset=dbset(SQLFORM.build_query( @@ -1832,7 +1834,7 @@ class SQLFORM(FORM): #================================================================ options =[] - for k,v in exportManager.items(): + for k,v in sorted(exportManager.items()): if hasattr(v, "__getitem__"): label = v[1] else: @@ -2463,6 +2465,19 @@ class ExportClass(object): def export(self): raise NotImplementedError +class ExporterTsv(ExportClass): + file_ext = "tsv" + content_type = "text/tab-separated-values" + + def __init__(self, rows): + ExportClass.__init__(self, rows) + + def export(self): + out = cStringIO.StringIO() + self.rows.export_to_csv_file(out, delimiter='\t', represent=True) + return str(out.getvalue()) + + class ExporterCsv(ExportClass): file_ext = "csv" content_type = "text/csv" @@ -2483,10 +2498,11 @@ class ExporterHtml(ExportClass): def export(self): out = cStringIO.StringIO() out.write('\n\n\n') - for cols in self.rows: + colnames = [a.split('.') for a in self.rows.colnames] + for row in self.rows.records: out.write('\n') - for colname,value in cols.items(): - out.write('\n') + for col in colnames: + out.write('\n') out.write('\n') out.write('
'+str(value)+''+str(row[col[0]][col[1]])+'
\n\n') return str(out.getvalue())