[web2py] Re: Automatic Field representation without rows object?

2013-06-21 Thread anonymouse
:D 

Looks good! I'll have to check out trunk and try it out.

Thanks for the answers (makes me feel like I'm getting pretty good with 
web2py) and especially for the code!

-C

On Thursday, 20 June 2013 22:08:27 UTC-5, Anthony wrote:

 FYI, see possible solution here: 
 https://groups.google.com/d/msg/web2py-developers/kMMG1wH6Xxg/pCYZfJGHVckJ

 Anthony

 On Thursday, June 20, 2013 8:22:54 PM UTC-4, Anthony wrote:


 Not a bad idea. Maybe have a look at how SQLTABLE and SQLFORM.grid (both 
 in gluon.sqlhtml.py) handle field representation. Very simply, we might 
 add a method to the Row class allowing something like:

 row.represent('myfield')


 Actually, Row objects don't know about their db/table, so you'd have to 
 pass that in somehow. Rows objects do know their db, so it would be easier 
 for an entire Rows object.

 Anthony



-- 

--- 
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/groups/opt_out.




[web2py] Re: Automatic Field representation without rows object?

2013-06-21 Thread Anthony
No problem. For this interested, in trunk, you can now do:

rows = db(query).select()
repr_row = rows.repr(0)

The .repr() method of the Rows object takes an index and returns a copy of 
the indexed row, but for fields with represent attributes, the 
represent function is applied to the value in the row. If you don't 
specify an index, you get a generator to iterate over all the rows:

for row in rows.repr():
print row.myfield

Can also be applied to slices:

for row in rows[0:10].repr():
print row.myfield

Note, you should take the slice of the Rows object first, and then apply 
the .repr() method, as you cannot slice a generator.

If you only want to transform selected fields via their represent 
attribute (e.g., some fields may have expensive represent functions 
involving database hits, so you want to avoid running those functions), you 
can list them in the fields argument:

repr_row = row.repr(0, fields=[db.mytable.myfield])

Note, because .repr() returns a transformed copy of the original Row, 
there's no .update_record() (which you wouldn't want anyway) or 
.delete_record() methods for the row.

Also, there has been some discussion of changing the name of this method to 
avoid confusion with the Python built-in repr() function, so consider this 
experimental for now.

Anthony

On Friday, June 21, 2013 4:45:17 PM UTC-4, anonymouse wrote:

 :D 

 Looks good! I'll have to check out trunk and try it out.

 Thanks for the answers (makes me feel like I'm getting pretty good with 
 web2py) and especially for the code!

 -C

 On Thursday, 20 June 2013 22:08:27 UTC-5, Anthony wrote:

 FYI, see possible solution here: 
 https://groups.google.com/d/msg/web2py-developers/kMMG1wH6Xxg/pCYZfJGHVckJ

 Anthony

 On Thursday, June 20, 2013 8:22:54 PM UTC-4, Anthony wrote:


 Not a bad idea. Maybe have a look at how SQLTABLE and SQLFORM.grid (both 
 in gluon.sqlhtml.py) handle field representation. Very simply, we 
 might add a method to the Row class allowing something like:

 row.represent('myfield')


 Actually, Row objects don't know about their db/table, so you'd have to 
 pass that in somehow. Rows objects do know their db, so it would be easier 
 for an entire Rows object.

 Anthony



-- 

--- 
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/groups/opt_out.




[web2py] Re: Automatic Field representation without rows object?

2013-06-20 Thread Anthony


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


I think the represent attribute is currently only used in SQLTABLE, 
SQLFORM.grid (and smartgrid), and in forms (i.e., read-only fields), so no, 
I don't think you're missing anything.
 

 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?


Don't think so, but you should be able to do 
db.mytable.myfield.represent(row.myfield, 
row).
 

 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?


Not a bad idea. Maybe have a look at how SQLTABLE and SQLFORM.grid (both in 
gluon.sqlhtml.py) handle field representation. Very simply, we might add a 
method to the Row class allowing something like:

row.represent('myfield')

and maybe row.represent(), with no arguments, could return a copy of the 
row object with all the fields transformed based on their represent 
attribute. This approach wouldn't involve any HTML, so you would be free to 
use the values however, without being constrained by some fixed HTML 
structure.

Anthony

-- 

--- 
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/groups/opt_out.




[web2py] Re: Automatic Field representation without rows object?

2013-06-20 Thread Anthony


 Not a bad idea. Maybe have a look at how SQLTABLE and SQLFORM.grid (both 
 in gluon.sqlhtml.py) handle field representation. Very simply, we might 
 add a method to the Row class allowing something like:

 row.represent('myfield')


Actually, Row objects don't know about their db/table, so you'd have to 
pass that in somehow. Rows objects do know their db, so it would be easier 
for an entire Rows object.

Anthony

-- 

--- 
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/groups/opt_out.




[web2py] Re: Automatic Field representation without rows object?

2013-06-20 Thread Anthony
FYI, see possible solution 
here: https://groups.google.com/d/msg/web2py-developers/kMMG1wH6Xxg/pCYZfJGHVckJ

Anthony

On Thursday, June 20, 2013 8:22:54 PM UTC-4, Anthony wrote:


 Not a bad idea. Maybe have a look at how SQLTABLE and SQLFORM.grid (both 
 in gluon.sqlhtml.py) handle field representation. Very simply, we might 
 add a method to the Row class allowing something like:

 row.represent('myfield')


 Actually, Row objects don't know about their db/table, so you'd have to 
 pass that in somehow. Rows objects do know their db, so it would be easier 
 for an entire Rows object.

 Anthony


-- 

--- 
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/groups/opt_out.