From 669691bbea7685a77198d2709591ff3eb7fd89bd Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 1 Apr 2013 14:14:41 -0500 Subject: [PATCH] Issue 1423:smartgrid multiple links to related table - how to suppress all but one, thanks cjkske --- VERSION | 2 +- gluon/sqlhtml.py | 50 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/VERSION b/VERSION index 9e9fcba8..a6bdef46 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.5-stable+timestamp.2013.04.01.12.44.20 +Version 2.4.5-stable+timestamp.2013.04.01.14.13.58 diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index e36f423b..380d9061 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2524,8 +2524,9 @@ class SQLFORM(FORM): break if nargs > len(args) + 1: query = (field == id) - if isinstance(linked_tables, dict): - linked_tables = linked_tables.get(table._tablename, []) + # cjk + # if isinstance(linked_tables, dict): + # linked_tables = linked_tables.get(table._tablename, []) if linked_tables is None or referee in linked_tables: field.represent = lambda id, r=None, referee=referee, rep=field.represent: A(callable(rep) and rep(id) or id, _class=trap_class(), _href=url(args=['view', referee, id])) except (KeyError, ValueError, TypeError): @@ -2550,20 +2551,45 @@ class SQLFORM(FORM): check[rfield.tablename] = \ check.get(rfield.tablename, []) + [rfield.name] if isinstance(linked_tables, dict): - linked_tables = linked_tables.get(table._tablename, []) - for tablename in sorted(check): - linked_fieldnames = check[tablename] - tb = db[tablename] - multiple_links = len(linked_fieldnames) > 1 - for fieldname in linked_fieldnames: - if linked_tables is None or tablename in linked_tables: - t = T(tb._plural) if not multiple_links else \ - T(tb._plural + '(' + fieldname + ')') - args0 = tablename + '.' + fieldname + for tbl in linked_tables.keys(): + tb = db[tbl] + if isinstance(linked_tables[tbl], list): + if len(linked_tables[tbl]) > 1: + t = T('%s(%s)' %(tbl, fld)) + else: + t = T(tb._plural) + for fld in linked_tables[tbl]: + if fld not in db[tbl].fields: + raise ValueError('Field %s not in table' %fld) + args0 = tbl + '.' + fld + links.append( + lambda row, t=t, nargs=nargs, args0=args0: + A(SPAN(t), _class=trap_class(), _href=url( + args=[args0, row[id_field_name]]))) + else: + t = T(tb._plural) + fld = linked_tables[tbl] + if fld not in db[tbl].fields: + raise ValueError('Field %s not in table' %fld) + args0 = tbl + '.' + fld links.append( lambda row, t=t, nargs=nargs, args0=args0: A(SPAN(t), _class=trap_class(), _href=url( args=[args0, row[id_field_name]]))) + else: + for tablename in sorted(check): + linked_fieldnames = check[tablename] + tb = db[tablename] + multiple_links = len(linked_fieldnames) > 1 + for fieldname in linked_fieldnames: + if linked_tables is None or tablename in linked_tables: + t = T(tb._plural) if not multiple_links else \ + T(tb._plural + '(' + fieldname + ')') + args0 = tablename + '.' + fieldname + links.append( + lambda row, t=t, nargs=nargs, args0=args0: + A(SPAN(t), _class=trap_class(), _href=url( + args=[args0, row[id_field_name]]))) grid = SQLFORM.grid(query, args=request.args[:nargs], links=links, links_in_grid=links_in_grid,