moved export links to the bottom

This commit is contained in:
mdipierro
2012-08-19 12:44:42 -05:00
parent 1de7d962bd
commit 7eb047012a
2 changed files with 59 additions and 53 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.00.0 (2012-08-19 10:18:00) dev Version 2.00.0 (2012-08-19 12:44:39) dev
+58 -52
View File
@@ -1770,12 +1770,13 @@ class SQLFORM(FORM):
return ret return ret
exportManager = dict( exportManager = dict(
csv_with_hidden_cols=(ExporterCsv,'csv, hidden cols'), csv_with_hidden_cols=(ExporterCSV,'CSV (hidden cols)'),
csv=ExporterCsv, csv=(ExporterCSV,'CSV'),
html=ExporterHtml, xml=(ExporterXML, 'XML'),
tsv_with_hidden_cols=(ExporterTsv, html=(ExporterHTML, 'HTML'),
'tsv (Excel compatible), hidden cols'), tsv_with_hidden_cols=\
tsv=(ExporterTsv, 'tsv (Excel compatible)')) (ExporterTSV,'TSV (Excel compatible, hidden cols)'),
tsv=(ExporterTSV, 'TSV (Excel compatible)'))
if not exportclasses is None: if not exportclasses is None:
exportManager.update(exportclasses) exportManager.update(exportclasses)
@@ -1789,10 +1790,9 @@ class SQLFORM(FORM):
else: else:
sign, rorder = '', order sign, rorder = '', order
tablename,fieldname = rorder.split('.',1) tablename,fieldname = rorder.split('.',1)
orderby=db[tablename][fieldname]
if sign=='~': if sign=='~':
orderby=~db[tablename][fieldname] orderby=~orderby
else:
orderby=db[tablename][fieldname]
table_fields = [f for f in fields if f._tablename in tablenames] table_fields = [f for f in fields if f._tablename in tablenames]
if export_type in ('csv_with_hidden_cols','tsv_with_hidden_cols'): if export_type in ('csv_with_hidden_cols','tsv_with_hidden_cols'):
@@ -1809,22 +1809,15 @@ class SQLFORM(FORM):
else: else:
rows = dbset.select(left=left,orderby=orderby,*columns) rows = dbset.select(left=left,orderby=orderby,*columns)
if not export_type is None: if exportManager.has_key(export_type):
if exportManager.has_key(export_type): value = exportManager[export_type]
value = exportManager[export_type] clazz = value[0] if hasattr(value, '__getitem__') else value
if hasattr(value, '__getitem__'): oExp = clazz(rows)
clazz = value[0] filename = '.'.join(('rows', oExp.file_ext))
else: response.headers['Content-Type'] = oExp.content_type
clazz = value response.headers['Content-Disposition'] = \
oExp = clazz(rows) 'attachment;filename='+filename+';'
filename = '.'.join(('rows', oExp.file_ext)) raise HTTP(200, oExp.export(),**response.headers)
response.headers['Content-Type'] = oExp.content_type
response.headers['Content-Disposition'] = \
'attachment;filename='+filename+';'
raise HTTP(200, oExp.export(),
**{'Content-Type':oExp.content_type,
'Content-Disposition':'attachment;filename='+filename+';'})
elif request.vars.records and not isinstance( elif request.vars.records and not isinstance(
request.vars.records,list): request.vars.records,list):
@@ -1882,31 +1875,18 @@ class SQLFORM(FORM):
buttontext='Add', buttontext='Add',
buttonurl=url(args=['new',tablename]))) buttonurl=url(args=['new',tablename])))
if csv and nrows: if csv and nrows:
options =[] export_links =[]
for k,v in sorted(exportManager.items()): for k,v in sorted(exportManager.items()):
if hasattr(v, "__getitem__"): label = v[1] if hasattr(v, "__getitem__") else k
label = v[1] link = url2(vars=dict(
else: order=request.vars.order or '',
label = k _export_type=k,
options.append(OPTION(T(label),_value=k)) keywords=request.vars.keywords or ''))
items = url2().split('?', 1) export_links.append(A(T(label),_href=link))
myurl = items[0] export_menu = \
if len(items)>1: DIV(T('Export:'),_class="w2p_export_menu",*export_links)
mysignature = psq(items[1]).get('_signature', [None])[-1] else:
else: export_menu = None
mysignature = ''
f = FORM(BUTTON(SPAN(_class=ui.get('buttonexport')),
"Export", _type="submit", _class=ui.get('button')),
SELECT(options, _name="_export_type"),
INPUT(_type="hidden", _name="order",
_value=request.vars.order),
INPUT(_type="hidden", _name="_signature",
_value=mysignature),
INPUT(_type="hidden", _name="keywords",
_value=request.vars.keywords or ''),
_method="GET", _action=myurl)
search_actions.append(f)
console.append(search_actions) console.append(search_actions)
order = request.vars.order or '' order = request.vars.order or ''
@@ -2087,6 +2067,7 @@ class SQLFORM(FORM):
DIV(paginator,_class=\ DIV(paginator,_class=\
"web2py_paginator %(header)s %(cornerbottom)s" % ui), "web2py_paginator %(header)s %(cornerbottom)s" % ui),
_class='%s %s' % (_class, ui.get('widget'))) _class='%s %s' % (_class, ui.get('widget')))
if export_menu: res.append(export_menu)
res.create_form = create_form res.create_form = create_form
res.update_form = update_form res.update_form = update_form
res.view_form = view_form res.view_form = view_form
@@ -2524,6 +2505,7 @@ form_factory = SQLFORM.factory # for backward compatibility, deprecated
class ExportClass(object): class ExportClass(object):
label = None
file_ext = None file_ext = None
content_type = None content_type = None
@@ -2574,8 +2556,9 @@ class ExportClass(object):
def export(self): def export(self):
raise NotImplementedError raise NotImplementedError
class ExporterTsv(ExportClass): class ExporterTSV(ExportClass):
label = 'TSV'
file_ext = "csv" file_ext = "csv"
content_type = "text/tab-separated-values" content_type = "text/tab-separated-values"
@@ -2607,7 +2590,8 @@ class ExporterTsv(ExportClass):
out.truncate(0) out.truncate(0)
return str(final.getvalue()) return str(final.getvalue())
class ExporterCsv(ExportClass): class ExporterCSV(ExportClass):
label = 'CSV'
file_ext = "csv" file_ext = "csv"
content_type = "text/csv" content_type = "text/csv"
@@ -2617,7 +2601,8 @@ class ExporterCsv(ExportClass):
def export(self): def export(self):
return str(self.rows) return str(self.rows)
class ExporterHtml(ExportClass): class ExporterHTML(ExportClass):
label = 'HTML'
file_ext = "html" file_ext = "html"
content_type = "text/html" content_type = "text/html"
@@ -2637,5 +2622,26 @@ class ExporterHtml(ExportClass):
return str(out.getvalue()) return str(out.getvalue())
class ExporterXML(ExportClass):
label = 'XML'
file_ext = "xml"
content_type = "text/xml"
def __init__(self, rows):
ExportClass.__init__(self, rows)
def export(self):
out = cStringIO.StringIO()
out.write('<rows>\n')
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())