Merge pull request #371 from timrichardson/issues/1859

Issues/1859 export from grid fails when virtual fields are present
This commit is contained in:
mdipierro
2014-01-31 15:08:21 -08:00
+17 -6
View File
@@ -2181,32 +2181,43 @@ class SQLFORM(FORM):
orderby = (order[:1] == '~' and ~sort_field) or sort_field orderby = (order[:1] == '~' and ~sort_field) or sort_field
expcolumns = [str(f) for f in columns] expcolumns = [str(f) for f in columns]
selectable_columns = [str(f) for f in columns if not isinstance(f,Field.Virtual)]
if export_type.endswith('with_hidden_cols'): if export_type.endswith('with_hidden_cols'):
expcolumns = [] #expcolumns = [] start with the visible columns, which includes visible virtual fields
selectable_columns=[] #like expcolumns but excluding virtual
for table in tables: for table in tables:
for field in table: for field in table:
if field.readable and field.tablename in tablenames: if field.readable and field.tablename in tablenames:
expcolumns.append(field) if not str(field) in expcolumns:
expcolumns.append(str(field))
if not(isinstance(field,Field.Virtual)):
selectable_columns.append(str(field))
#look for virtual fields not displayed (and virtual method fields to be added here?)
for (field_name,field) in table.iteritems():
if isinstance(field,Field.Virtual) and not str(field) in expcolumns:
expcolumns.append(str(field))
if export_type in exportManager and exportManager[export_type]: if export_type in exportManager and exportManager[export_type]:
if request.vars.keywords: if request.vars.keywords:
try: try:
#the query should be constructed using searchable fields #the query should be constructed using searchable fields but not virtual fields
sfields = reduce(lambda a, b: a + b, sfields = reduce(lambda a, b: a + b,
[[f for f in t if f.readable] for t in tables]) [[f for f in t if f.readable and not isinstance(f,Field.Virtual)] for t in tables])
#how to put virtual fields back?
dbset = dbset(SQLFORM.build_query( dbset = dbset(SQLFORM.build_query(
sfields, request.vars.get('keywords', ''))) sfields, request.vars.get('keywords', '')))
rows = dbset.select(left=left, orderby=orderby, rows = dbset.select(left=left, orderby=orderby,
cacheable=True, *expcolumns) cacheable=True, *selectable_columns)
except Exception, e: except Exception, e:
response.flash = T('Internal Error') response.flash = T('Internal Error')
rows = [] rows = []
else: else:
rows = dbset.select(left=left, orderby=orderby, rows = dbset.select(left=left, orderby=orderby,
cacheable=True, *expcolumns) cacheable=True, *selectable_columns)
value = exportManager[export_type] value = exportManager[export_type]
clazz = value[0] if hasattr(value, '__getitem__') else value clazz = value[0] if hasattr(value, '__getitem__') else value
rows.colnames = expcolumns # rows.colnames is selectable fields, it misses virtual fields
oExp = clazz(rows) oExp = clazz(rows)
filename = '.'.join(('rows', oExp.file_ext)) filename = '.'.join(('rows', oExp.file_ext))
response.headers['Content-Type'] = oExp.content_type response.headers['Content-Type'] = oExp.content_type