Fix tsv ext, add tsv hidden exporter , use csv exporter for tsv export
This commit is contained in:
+25
-28
@@ -19,7 +19,7 @@ import re
|
|||||||
import copy
|
import copy
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from gluon._compat import StringIO, unichr, urllib_quote, iteritems, basestring, long, integer_types, unicodeT, to_native, to_unicode, urlencode
|
from gluon._compat import StringIO,unichr, urllib_quote, iteritems, basestring, long, integer_types, unicodeT, to_native, to_unicode, urlencode
|
||||||
from gluon.http import HTTP, redirect
|
from gluon.http import HTTP, redirect
|
||||||
from gluon.html import XmlComponent, truncate_string
|
from gluon.html import XmlComponent, truncate_string
|
||||||
from gluon.html import XML, SPAN, TAG, A, DIV, CAT, UL, LI, TEXTAREA, BR, IMG
|
from gluon.html import XML, SPAN, TAG, A, DIV, CAT, UL, LI, TEXTAREA, BR, IMG
|
||||||
@@ -2630,8 +2630,7 @@ class SQLFORM(FORM):
|
|||||||
xml=(ExporterXML, 'XML', T('XML export of columns shown')),
|
xml=(ExporterXML, 'XML', T('XML export of columns shown')),
|
||||||
html=(ExporterHTML, 'HTML', T('HTML export of visible columns')),
|
html=(ExporterHTML, 'HTML', T('HTML export of visible columns')),
|
||||||
json=(ExporterJSON, 'JSON', T('JSON export of visible columns')),
|
json=(ExporterJSON, 'JSON', T('JSON export of visible columns')),
|
||||||
tsv_with_hidden_cols=
|
tsv_with_hidden_cols=(ExporterTSV_hidden, 'TSV (Spreadsheets, hidden cols)', T('Spreadsheet-optimised export of tab-separated content including hidden columns. May be slow')),
|
||||||
(ExporterTSV, 'TSV (Spreadsheets, hidden cols)', T('Spreadsheet-optimised export of tab-separated content including hidden columns. May be slow')),
|
|
||||||
tsv=(ExporterTSV, 'TSV (Spreadsheets)', T('Spreadsheet-optimised export of tab-separated content, visible columns only. May be slow.')))
|
tsv=(ExporterTSV, 'TSV (Spreadsheets)', T('Spreadsheet-optimised export of tab-separated content, visible columns only. May be slow.')))
|
||||||
if exportclasses is not None:
|
if exportclasses is not None:
|
||||||
"""
|
"""
|
||||||
@@ -3697,40 +3696,38 @@ class ExportClass(object):
|
|||||||
|
|
||||||
|
|
||||||
class ExporterTSV(ExportClass):
|
class ExporterTSV(ExportClass):
|
||||||
|
# TSV, represent == True
|
||||||
label = 'TSV'
|
label = 'TSV'
|
||||||
file_ext = "csv"
|
file_ext = "tsv"
|
||||||
content_type = "text/tab-separated-values"
|
content_type = "text/tab-separated-values"
|
||||||
|
|
||||||
def __init__(self, rows):
|
def __init__(self, rows):
|
||||||
ExportClass.__init__(self, rows)
|
ExportClass.__init__(self, rows)
|
||||||
|
|
||||||
def export(self):
|
def export(self): # export TSV with rows.represent
|
||||||
out = StringIO()
|
|
||||||
final = StringIO()
|
|
||||||
import csv
|
|
||||||
writer = csv.writer(out, delimiter='\t')
|
|
||||||
if self.rows:
|
if self.rows:
|
||||||
import codecs
|
s = StringIO()
|
||||||
final.write(codecs.BOM_UTF16)
|
self.rows.export_to_csv_file(s, represent=True,delimiter='\t',newline='\n')
|
||||||
writer.writerow(
|
return s.getvalue()
|
||||||
[to_unicode(col, "utf8") for col in self.rows.colnames])
|
else:
|
||||||
data = out.getvalue().decode("utf8")
|
return None
|
||||||
data = data.encode("utf-16")
|
|
||||||
data = data[2:]
|
|
||||||
final.write(data)
|
|
||||||
out.truncate(0)
|
|
||||||
|
|
||||||
records = self.represented()
|
|
||||||
for row in records:
|
|
||||||
writer.writerow(
|
|
||||||
[str(col).decode('utf8').encode("utf-8") for col in row])
|
|
||||||
data = out.getvalue().decode("utf8")
|
|
||||||
data = data.encode("utf-16")
|
|
||||||
data = data[2:]
|
|
||||||
final.write(data)
|
|
||||||
|
|
||||||
out.truncate(0)
|
class ExporterTSV_hidden(ExportClass):
|
||||||
return str(final.getvalue())
|
label = 'TSV'
|
||||||
|
file_ext = "tsv"
|
||||||
|
content_type = "text/tab-separated-values"
|
||||||
|
|
||||||
|
def __init__(self, rows):
|
||||||
|
ExportClass.__init__(self, rows)
|
||||||
|
|
||||||
|
def export(self): # export TSV with rows.represent
|
||||||
|
if self.rows:
|
||||||
|
s = StringIO()
|
||||||
|
self.rows.export_to_csv_file(s,delimiter='\t',newline='\n')
|
||||||
|
return s.getvalue()
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class ExporterCSV(ExportClass):
|
class ExporterCSV(ExportClass):
|
||||||
|
|||||||
Reference in New Issue
Block a user