I made a new diff with Anthony suggestions: Now any helper instance
can be returned by lambda as a second argument.
diff -r 7dd85a51bb2a gluon/sqlhtml.py
--- a/gluon/sqlhtml.py Tue Nov 29 22:32:30 2011 -0600
+++ b/gluon/sqlhtml.py Thu Dec 01 00:44:05 2011 -0300
@@ -1972,6 +1972,8 @@
optional arguments:
:param linkto: URL (or lambda to generate a URL) to edit
individual records
+ To wrap an IMG object or alike with de the id
field link,
+ the lambda function can return a (href, obj)
tuple
:param upload: URL to download uploaded files
:param orderby: Add an orderby link to column headers.
:param headers: dictionary of headers to headers redefinions
@@ -2142,16 +2144,25 @@
if not field:
pass
elif linkto and field.type == 'id':
+ linkwrapped = None
try:
- href = linkto(r, 'table', tablename)
+ t = linkto(r, 'table', tablename)
+ if isinstance(t, (tuple, list)):
+ href, linkwrapped = t
+ else:
+ href = t
except TypeError:
href = '%s/%s/%s' % (linkto, tablename,
r_old)
- r = A(r, _href=href)
+ r = self.link_or_image(r, href, linkwrapped)
elif field.type.startswith('reference'):
if linkto:
ref = field.type[10:]
try:
- href = linkto(r, 'reference', ref)
+ t = linkto(r, 'reference', ref)
+ if isinstance(t, (tuple, list)):
+ href = t[0]
+ else:
+ href = t
except TypeError:
href = '%s/%s/%s' % (linkto, ref, r_old)
if ref.find('.') >= 0:
@@ -2247,6 +2258,14 @@
return css
+
+ def link_or_image(self, r, href, linkwrapped):
+ if linkwrapped is not None:
+ return A(linkwrapped, _href=href)
+ else:
+ return A(r, _href=href)
+
+
form_factory = SQLFORM.factory # for backward compatibility,
deprecated
On Nov 30, 10:32 pm, Anthony <[email protected]> wrote:
> Instead, why not make it more general and simply allow the linkto lambda to
> return an (r, href) tuple, where r could be an IMG object or anything else.
> Something like:
>
> elif linkto and field.type == 'id':
> try:
> href = linkto(r, 'table', tablename)
> if isinstance(href, (list, tuple)):
> r, href = href
>
> Then we don't need a new argument -- all the work can be done in the linkto
> function.
>
> Anthony
>
> On Wednesday, November 30, 2011 8:01:18 PM UTC-5, Alan Etkin wrote:
>
> > I think that it would be practical to have the option when calling
> > SQLTABLE to specify images in place of id links.
>
> > Here is how i would change it (seems to work with version 1.99.2)
>
> > diff -r 7dd85a51bb2a gluon/sqlhtml.py
> > --- a/gluon/sqlhtml.py Tue Nov 29 22:32:30 2011 -0600
> > +++ b/gluon/sqlhtml.py Wed Nov 30 21:50:01 2011 -0300
> > @@ -1972,6 +1972,7 @@
> > optional arguments:
>
> > :param linkto: URL (or lambda to generate a URL) to edit
> > individual records
> > + :param imagelink: URL of an image to wrap the linkto field
> > :param upload: URL to download uploaded files
> > :param orderby: Add an orderby link to column headers.
> > :param headers: dictionary of headers to headers redefinions
> > @@ -2043,6 +2044,7 @@
> > self,
> > sqlrows,
> > linkto=None,
> > + imagelink=None,
> > upload=None,
> > orderby=None,
> > headers={},
> > @@ -2146,7 +2148,7 @@
> > href = linkto(r, 'table', tablename)
> > except TypeError:
> > href = '%s/%s/%s' % (linkto, tablename,
> > r_old)
> > - r = A(r, _href=href)
> > + r = self.image_link(r, href, imagelink)
> > elif field.type.startswith('reference'):
> > if linkto:
> > ref = field.type[10:]
> > @@ -2247,6 +2249,14 @@
>
> > return css
>
> > +
> > + def image_link(self, r, href, imagelink):
> > + if isinstance(imagelink, basestring):
> > + return A(IMG(_src=imagelink, _alt=r), _href=href)
> > + else:
> > + return A(r, _href=href)
> > +
> > +
> > form_factory = SQLFORM.factory # for backward compatibility,
> > deprecated