Represent, list:reference, upload-field

305 views
Skip to first unread message

Johann Spies

unread,
Oct 14, 2011, 4:29:22 AM10/14/11
to web...@googlegroups.com
This table

db.define_table("wbdocuments",
                Field("name", label = "Document name"),
                Field("file", "upload", label = "Download"),
                format = '%(name)s')

is linked in another table:

db.define_table('wbmaster',
                           Field('documents', 'list:reference wbdocuments'))

As long as I do not add a .represent to the second table's 'documents' I can see the names of the documents in smartgrid as a
comma seperated list. 

I want those doccuments to be downloadable when clicked on.

I see in the book

db.mytable.some_uploadfield.represent = lambda value,row: \
A('get it', _href=URL('download', args=value))


can be used but so far I did not have any  success. 

As soon as I try


db.wbmaster.documents.represent = lambda value, row: \
        A('%s' % row.name, _href = URL('download', args = value))

I just get a concatenation of the id's  (eg. 69) where without it I would see a string like: 'x.docx, y.pdf'

Help would be appreciated.

Regards
Johann
--
 May grace and peace be yours in abundance through the full knowledge of God and of Jesus our Lord!  His divine power has given us everything we need for life and godliness through the full knowledge of the one who called us by his own glory and excellence.
                                                    2 Pet. 1:2b,3a

Massimo Di Pierro

unread,
Oct 14, 2011, 5:14:25 AM10/14/11
to web2py-users
documents is a list of IDs so you have to turn each one of them into a
link. You can try:

db.wbmaster.documents.represent = lambda value, row: SPAN(*[row.name]+
[A(v, _href = URL('download', args = v)) for v in value])

Hope I make sense.

On Oct 14, 3:29 am, Johann Spies <johann.sp...@gmail.com> wrote:
> This table
>
> db.define_table("wbdocuments",
>                 Field("name", label = "Document name"),
>                 Field("file", "upload", label = "Download"),
>                 format = '%(name)s')
>
> is linked in another table:
>
> db.define_table('wbmaster',
>                            Field('documents', 'list:reference wbdocuments'))
>
> As long as I do not add a .represent to the second table's 'documents' I can
> see the names of the documents in smartgrid as a
> comma seperated list.
>
> I want those doccuments to be downloadable when clicked on.
>
> I see in the book
>
> db.mytable.some_uploadfield.represent = lambda value,row: \
>     A <http://web2py.com/book/default/docstring/A>('get it', _href=URL
> <http://web2py.com/book/default/docstring/URL>('download',

Johann Spies

unread,
Oct 14, 2011, 6:35:19 AM10/14/11
to web...@googlegroups.com
On 14 October 2011 11:14, Massimo Di Pierro <massimo....@gmail.com> wrote:
documents is a list of IDs so you have to turn each one of them into a
link. You can try:

db.wbmaster.documents.represent = lambda value, row: SPAN(*[row.name]+
[A(v, _href = URL('download', args = v)) for v in value])

Hope I make sense.

Yes, it does, thank you.

But that also did not do the job.  I still got a '69' in stead of a list of names.  I have changed that to 

db.wbmaster.documents.represent = lambda value, row: [A(db.wbdocuments[v].name + ', ', _href = URL('download', args = v)) for v in value]

(I am not sure how to use 'SPAN' in this case - or why) and now I get a list of links which is what I was looking for. 
But the links are wrong.  When clicked on the first link the URL was  http://localhost:8000/init/wbank/download/6
where it should have downloaded the record with id=6 from db.wbdocuments.

Now I just get a 404 NOT FOUND.

Regards
Johann

Massimo Di Pierro

unread,
Oct 14, 2011, 8:49:57 AM10/14/11
to web2py-users
My bad. Try this:

def render_docs(ids,row):
span = SPAN()
for id in ids:
doc = db.wbdocuments(id)
if doc:
span.append(A(doc.name,_href=URL('download',args=doc.file)))
return span

db.wbmaster.documents.represent = render_docs

On Oct 14, 5:35 am, Johann Spies <johann.sp...@gmail.com> wrote:
> On 14 October 2011 11:14, Massimo Di Pierro <massimo.dipie...@gmail.com>wrote:
>
> > documents is a list of IDs so you have to turn each one of them into a
> > link. You can try:
>
> > db.wbmaster.documents.represent = lambda value, row: SPAN(*[row.name]+
> > [A(v, _href = URL('download', args = v)) for v in value])
>
> > Hope I make sense.
>
> > Yes, it does, thank you.
>
> But that also did not do the job.  I still got a '69' in stead of a list of
> names.  I have changed that to
>
> db.wbmaster.documents.represent = lambda value, row:
> [A(db.wbdocuments[v].name + ', ', _href = URL('download', args = v)) for v
> in value]
>
> (I am not sure how to use 'SPAN' in this case - or why) and now I get a list
> of links which is what I was looking for.
> But the links are wrong.  When clicked on the first link the URL washttp://localhost:8000/init/wbank/download/6

Johann Spies

unread,
Oct 14, 2011, 9:26:07 AM10/14/11
to web...@googlegroups.com
On 14 October 2011 14:49, Massimo Di Pierro <massimo....@gmail.com> wrote:
My bad. Try this:

def render_docs(ids,row):
     span = SPAN()
     for id in ids:
        doc = db.wbdocuments(id)
        if doc:
span.append(A(doc.name,_href=URL('download',args=doc.file)))
     return span

db.wbmaster.documents.represent = render_docs

Fantastic.  Thanks

Johann

--
Because experiencing your loyal love is better than life itself,
my lips will praise you.  (Psalm 63:3)

Mandar Vaze

unread,
May 27, 2014, 2:37:46 AM5/27/14
to web...@googlegroups.com
A Million Thanks for Johann (for asking the question) and Massimo (for providing the answer).
Following is useful even after more than 3 years !!!

Web2py rocks, and web2py community makes it even better :)

-Mandar

ncl....@gmail.com

unread,
Sep 27, 2015, 5:51:17 PM9/27/15
to web2py-users
Also after 4 years!!!
Many, many thanks!!!!

Nicola
Reply all
Reply to author
Forward
0 new messages