Hi all,

I was playing with record/field representation and was wondering if there 
is a simple solution for my problem.

Here's what I'm trying to do -- print out a record using the "represent" 
function from the DB table. 

Here's my model:

models/document.py
db.define_table('document',
  Field('name', required=True, label='File Name'),
  Field('type', required=True),
  Field('ext'),
  Field('file', 'upload'),
  Field('created_on', 'datetime', default=request.now),
  Field('created_by', 'reference auth_user', default=auth.user_id),
  format='%(name)s'
)
db.document.id.represent = lambda val,r: SPAN(A('view' if r.type=='image' 
else 'get', _href='/img/' + str(val)))
db.document.created_on.represent = lambda val: val.strftime('%Y-%m-%d %H:%M'
) if val else 'NULL'


This for a view works OK:

{{=TABLE(records)}}

But it's not very pretty and I don't need to list all the fields, the 
header values are ugly and I don't really want to use a table anyhow. 
document.iddocument.namedocument.typedocument.extdocument.file
document.created_ondocument.created_byview<https://ara.wipac.wisc.edu/img/5>
Mayan-Calendarimage.jpegfile2013-06-17 
16:03usernameget<https://ara.wipac.wisc.edu/img/4>
adwawdocument.docxfile2013-06-17 
15:16usernameget<https://ara.wipac.wisc.edu/img/3>
thesis-1784...document.pdffile2013-06-17 
15:16usernameview<https://ara.wipac.wisc.edu/img/2>
hose_reelimage.jpgfile2013-06-17 
14:59usernameget<https://ara.wipac.wisc.edu/img/1>
jobsdocument.docxfile2013-06-17 14:58username
So the natural thing for me to do is to loop over the records as such:

{{
  for r in records:
    cols = [
     DIV(r.id, _class='media-id'),
     DIV(r.name, _class='media-name'),
     DIV(r.type, _class='media-type'),
     DIV(r.created_on.strftime('%Y-%m-%d %H:%M'), _class='media-timestamp'),
     DIV(r.created_by, _class='media-owner'),
    ]
    =DIV(*cols, _class='media-row')
  pass
}}

Which yields (with the proper css of course):

5
Mayan-Calendar
image
2013-06-17 16:03:04
1
4
adwaw
document
2013-06-17 15:16:19
1
3
thesis-1784
document
2013-06-17 15:16:09
1
2
hose_reel
image
2013-06-17 14:59:11
1
1
jobs
document
2013-06-17 14:58:07
1

(I also tried for r in records: TR(r) pass inside of a table -- it seems 
that only Rows objects represent the fields properly, not Row objects)

But now the ID field and the created_on field do not use the representation 
I've specified nor does created_by use the default auth_user representation.

So I try this -- create a function called rep that represents the thing 
from it's database table (which means now I have to send the table to the 
view or just assume the table name in the view) -- which ends up being a 
little bit of a pain:

{{
  def rep(record, field, tag=DIV, class_part=None):
    fr = table[field].represent
    args = None
    val = record[field]

    if fr and fr.func_code.co_argcount == 1:
      val = fr(record[field])
    elif fr and fr.func_code.co_argcount == 2:
      val = fr(record[field], record)
    pass  
         
    #if fr and args:
    #  val = fr(*args)
    #pass

    if not class_part:
      class_part = field
    pass
    
    return tag(val, _class='media-%s' % class_part)
}}  
...
{{
  for r in records: 
    cols = [
      rep(r, 'id'), 
      rep(r, 'name'),
      rep(r, 'type'),
      rep(r, 'created_by', class_part='owner'),
      rep(r, 'created_on', class_part='time'),
    =DIV(*cols, _class='media-row')
  pass
}}

Ok, so now I have this:

view <https://ara.wipac.wisc.edu/img/5>
Mayan-Calendar
image
1
2013-06-17 16:03
get <https://ara.wipac.wisc.edu/img/4>
adwaw
document
1
2013-06-17 15:16
get <https://ara.wipac.wisc.edu/img/3>
thesis-1784
document
1
2013-06-17 15:16
view <https://ara.wipac.wisc.edu/img/2>
allwell
image
1
2013-06-17 14:59
get <https://ara.wipac.wisc.edu/img/1>
jobs
document
1
2013-06-17 14:58



But now still doesn't pick up the representation of the auth_user table 
(i.e. the "1" column), 

So now I have to explicitly define the representation of the "created_by" 
field of db.document to reference db.auth_user.username or 
first_name/last_name or whatever.

MY QUESTIONs are -- 

1) Am I missing something with the abilities of 'represent' to easily 
represent records' field values?

2) is there a way, given a *row* object, to very easily (e.g. not writing 
supporting code beyond lambda statements) represent the field value without 
explicitly calling the db.table.field.represent() function on the field 
value?

3) would it be feasible and recommended to create a simple class that takes 
Rows objects and creates a DIV-based representation? How might I go about 
doing this if so? 

Any other suggestions that accomplish my goals are welcome. Thanks all!


PS: running web2py v2.4.6-stable (2013-4-6 17:37)

-- 

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to