From 20576684dd94323ab1325706a10e3bad81dde28b Mon Sep 17 00:00:00 2001 From: Tim Richardson Date: Fri, 31 Jan 2014 12:02:46 +1100 Subject: [PATCH 1/4] Step 1 in fixing 1859: this stops the crashing, but virtual fields are not exported --- gluon/sqlhtml.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 573b498f..de6179cc 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2181,29 +2181,34 @@ class SQLFORM(FORM): orderby = (order[:1] == '~' and ~sort_field) or sort_field 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'): expcolumns = [] + selectable_columns=[] #like expcolumns but excluding virtual for table in tables: for field in table: if field.readable and field.tablename in tablenames: expcolumns.append(field) + if not(isinstance(field,Field.Virtual)): + selectable_columns.append(field) if export_type in exportManager and exportManager[export_type]: if request.vars.keywords: 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, - [[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( sfields, request.vars.get('keywords', ''))) rows = dbset.select(left=left, orderby=orderby, - cacheable=True, *expcolumns) + cacheable=True, *selectable_columns) except Exception, e: response.flash = T('Internal Error') rows = [] else: rows = dbset.select(left=left, orderby=orderby, - cacheable=True, *expcolumns) + cacheable=True, *selectable_columns) value = exportManager[export_type] clazz = value[0] if hasattr(value, '__getitem__') else value From cfbae5024887bdc674d28f71f646da820d2ec826 Mon Sep 17 00:00:00 2001 From: Tim Richardson Date: Fri, 31 Jan 2014 13:44:25 +1100 Subject: [PATCH 2/4] step 2 of fixing export from grids when there are virtual fields. Note export hidden columns includes all table fields and any virtual fields which are displayed, but it does not pick up virtual fields which are not displayed. --- gluon/sqlhtml.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index de6179cc..2372b833 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2183,14 +2183,15 @@ class SQLFORM(FORM): 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'): - expcolumns = [] + #expcolumns = [] start with the visible columns, which includes visible virtual fields selectable_columns=[] #like expcolumns but excluding virtual for table in tables: for field in table: - if field.readable and field.tablename in tablenames: - expcolumns.append(field) + if field.readable and field.tablename in tablenames: #this does not find virtual fields + if not str(field) in expcolumns: + expcolumns.append(str(field)) if not(isinstance(field,Field.Virtual)): - selectable_columns.append(field) + selectable_columns.append(str(field)) if export_type in exportManager and exportManager[export_type]: if request.vars.keywords: @@ -2212,6 +2213,7 @@ class SQLFORM(FORM): value = exportManager[export_type] clazz = value[0] if hasattr(value, '__getitem__') else value + rows.colnames = expcolumns # rows.colnames is selectable fields, it misses virtual fields oExp = clazz(rows) filename = '.'.join(('rows', oExp.file_ext)) response.headers['Content-Type'] = oExp.content_type From 18af4e92b0c8e555b08d7b3d6eb3020de6829294 Mon Sep 17 00:00:00 2001 From: Tim Richardson Date: Fri, 31 Jan 2014 14:45:45 +1100 Subject: [PATCH 3/4] when exporting hidden columns, pick up all virtual fields, even those not on display. --- gluon/sqlhtml.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 2372b833..b0c2a87a 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2187,11 +2187,15 @@ class SQLFORM(FORM): selectable_columns=[] #like expcolumns but excluding virtual for table in tables: for field in table: - if field.readable and field.tablename in tablenames: #this does not find virtual fields + if field.readable and field.tablename in tablenames: 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 computed 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 request.vars.keywords: From 64ccd9043e6df727139c753b6d3e53e187c5f8cb Mon Sep 17 00:00:00 2001 From: Tim Richardson Date: Fri, 31 Jan 2014 16:26:42 +1100 Subject: [PATCH 4/4] fixed comment --- gluon/sqlhtml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index b0c2a87a..38573ccb 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2192,7 +2192,7 @@ class SQLFORM(FORM): expcolumns.append(str(field)) if not(isinstance(field,Field.Virtual)): selectable_columns.append(str(field)) - #look for virtual fields not displayed (and computed fields to be added here) + #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))