[web2py] Re: represent list:reference

2016-10-21 Thread Leonel Câmara
First you are getting the error because.
db.publicaciones.autores.represent is not defined so it is None and hence 
not callable.

That said. Here's a generic represent function you can use with 
list:reference fields.

def represent_listref(table, *fields):
def represent(values):
result = []
if values is None:
return None
for value in values:
v = db[table][value]
rendered = {'id': value}
for field in fields:
represent = db[table][field].represent
rendered[field] = represent(v[field]) if represent else v[
field]
result.append(rendered)
return result
return represent



The way to use it is in the model set it as represent


db.define_table('publicaciones',
 Field('titulo'),
 Field('descripcion', 'text', label='Descripción'),
 Field('autores', 'list:reference db.personas',
requires= IS_IN_DB(db, 'personas.id', '%(nombres)s %(apellidos)s', 
multiple=True), represent=represent_listref('personas', 'nombres', 
'apellidos')),
 format='%(titulo)s')

Then in your controller, you need to render the rows.

lista_publicaciones = list(db(db.publicaciones.tipo == tipo).select(orderby 
=~ db.publicaciones.fecha).render())

Now in your view for each "publicacione" in autores you will have a list of 
dicts with the fields nombres and apellidos as well as the id which my 
implementation of represent_listref always adds.




-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[web2py] Re: represent list:reference

2016-10-20 Thread leonardoporto
Up.

Em terça-feira, 11 de setembro de 2012 18:35:25 UTC-3, Pepe Araya escreveu:
>
> Hello,
> I need to display the format value of the referenced table in a 
> list:reference and not the list of ids.
>
> I have these tables:
> db.define_table('personas',
>  Field('nombres'),
>  Field('apellidos'),
>  format='%(nombres)s %(apellidos)s')
>
> db.define_table('publicaciones',
>  Field('titulo'),
>  Field('descripcion', 'text', label='Descripción'),
>  Field('autores', 'list:reference db.personas',
> requires= IS_IN_DB(db, 'personas.id', '%(nombres)s 
> %(apellidos)s', multiple=True)),
>  format='%(titulo)s')
>
> this controller function:
> def filtro():
> tipo = request.args(0)
> lista_publicaciones = db(db.publicaciones.tipo == tipo).select(orderby 
> =~ db.publicaciones.fecha)
> if lista_publicaciones:
> lista_publicaciones=lista_publicaciones
> else:
> lista_publicaciones = "0"
> return dict(lista_publicaciones=lista_publicaciones)
>
> and this in the view:
>
> {{if lista_publicaciones !='0':}}
>
> {{for publicacion in lista_publicaciones:}}
> 
> 
>  href="{{=URL('publicaciones','ver_publicacion', args=publicacion.id)}}" 
> >{{=publicacion.titulo}}
> Autores: {{=publicacion.autores}}
>
> {{=XML(publicacion.descripcion)}}
> 
> {{pass}}
> {{else:}}
> something...
> {{pass}}
>
> using this in the view:
> {{=db.publicaciones.autores.represent(publicacion.autores) }}
>
> I get this error:
> ('NoneType' object is not callable)
>
> If I use:
> Autores: {{=publicacion.autores}}
>
> I get:
> Autores: [5,66]
>
> and I want:
>
> Autores: nombres apellidos, nombres apellidos
>
> Please, any help is welcome.
> Thanks.
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [web2py] Re: Represent, list:reference, upload-field

2015-09-27 Thread ncl . biasi
Also after 4 years!!!
Many, many thanks

Nicola

Il giorno martedì 27 maggio 2014 08:37:46 UTC+2, Mandar Vaze ha scritto:
>
> 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
>
> On Friday, October 14, 2011 6:56:07 PM UTC+5:30, Johann Spies wrote:
>>
>>
>>
>> On 14 October 2011 14:49, Massimo Di Pierro  
>> 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)
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [web2py] Re: Represent, list:reference, upload-field

2014-05-26 Thread Mandar Vaze
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

On Friday, October 14, 2011 6:56:07 PM UTC+5:30, Johann Spies wrote:
>
>
>
> On 14 October 2011 14:49, Massimo Di Pierro 
> 
> > 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)
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [web2py] Re: Represent, list:reference, upload-field

2011-10-14 Thread Johann Spies
On 14 October 2011 14:49, Massimo Di Pierro 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)


[web2py] Re: Represent, list:reference, upload-field

2011-10-14 Thread Massimo Di Pierro
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  wrote:
> On 14 October 2011 11:14, Massimo Di Pierro 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
> where it should have downloaded the record with id=6 from db.wbdocuments.
>
> Now I just get a 404 NOT FOUND.
>
> Regards
> Johann


Re: [web2py] Re: Represent, list:reference, upload-field

2011-10-14 Thread Johann Spies
On 14 October 2011 11:14, Massimo Di Pierro 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


[web2py] Re: Represent, list:reference, upload-field

2011-10-14 Thread Massimo Di Pierro
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  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 ('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