From 515cc6b8297cb865c8665c17088e54255451d605 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Tue, 18 Dec 2012 09:22:28 -0600 Subject: [PATCH] preventing invalid query when exporting a rows object, thanks Niphlod --- VERSION | 2 +- gluon/sqlhtml.py | 52 +++++++++++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/VERSION b/VERSION index fb91d50d..de4c6a6f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.3.2 (2012-12-17 17:13:45) dev +Version 2.3.2 (2012-12-18 09:21:46) dev diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 88ae6e74..be2d3daa 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2851,15 +2851,16 @@ class ExporterTSV(ExportClass): final = cStringIO.StringIO() import csv writer = csv.writer(out, delimiter='\t') - import codecs - final.write(codecs.BOM_UTF16) - writer.writerow( - [unicode(col).encode("utf8") for col in self.rows.colnames]) - data = out.getvalue().decode("utf8") - data = data.encode("utf-16") - data = data[2:] - final.write(data) - out.truncate(0) + if self.rows: + import codecs + final.write(codecs.BOM_UTF16) + writer.writerow( + [unicode(col).encode("utf8") for col in self.rows.colnames]) + data = out.getvalue().decode("utf8") + data = data.encode("utf-16") + data = data[2:] + final.write(data) + out.truncate(0) records = self.represented() for row in records: writer.writerow( @@ -2881,7 +2882,10 @@ class ExporterCSV(ExportClass): ExportClass.__init__(self, rows) def export(self): - return str(self.rows) + if self.rows: + return str(self.rows) + else: + return '' class ExporterHTML(ExportClass): @@ -2895,12 +2899,13 @@ class ExporterHTML(ExportClass): def export(self): out = cStringIO.StringIO() out.write('\n\n\n') - colnames = [a.split('.') for a in self.rows.colnames] - for row in self.rows.records: - out.write('\n') - for col in colnames: - out.write('\n') - out.write('\n') + if self.rows: + colnames = [a.split('.') for a in self.rows.colnames] + for row in self.rows.records: + out.write('\n') + for col in colnames: + out.write('\n') + out.write('\n') out.write('
' + str(row[col[0]][col[1]]) + '
' + str(row[col[0]][col[1]]) + '
\n\n') return str(out.getvalue()) @@ -2916,12 +2921,13 @@ class ExporterXML(ExportClass): def export(self): out = cStringIO.StringIO() out.write('\n') - colnames = [a.split('.') for a in self.rows.colnames] - for row in self.rows.records: - out.write('\n') - for col in colnames: - out.write( - '<%s>' % col + str(row[col[0]][col[1]]) + '\n' % col) - out.write('\n') + if self.rows: + colnames = [a.split('.') for a in self.rows.colnames] + for row in self.rows.records: + out.write('\n') + for col in colnames: + out.write( + '<%s>' % col + str(row[col[0]][col[1]]) + '\n' % col) + out.write('\n') out.write('') return str(out.getvalue())