----motivation:
It wold by nice to have a tool to add arbitrary columns to
db().select() result
to be able to create multiple views of the same tables(including
joins).
That wold be especially valuable when dealing with legacy databases
when you can't design the database according to the web user
interface.

The easiest solution wold be something like:

class ExtraField:
    def new_column(self):
        if self.some_field_in_select==' something':
            return A('some_action_link', _href=URL(
                                f=some_action_controller_function,
                                args=[self.id]))
                                #if select contains 'as' it shell be
'self.as.id'
                                #if select contains join it shell be
'self.table.id'
                                #'id' is just an example, any othe
field can be
                                #used
        else:
            return A('great thing', _href='http://www.web2py.com')

rows=db(db.some_table).select()
rows.colnames.append('some_table.new_column')
rows.setvirtualfields(some_table=ExtraField())
table=SQLTABLE(rows)

This will give an exception telling that
sqlhtml.py:SQLTABLE.__init__ can't do
field = sqlrows.db[tablename][fieldname]
because there is no 'new_column' in the database.
It wants to get the field from the db model to know how to render it.
This is doesn't matter when we add a new field to select result
because it shell(and will by a view) be cast to string.

----The patch:
change
  sqlhtml.py:SQLTABLE.__init__
    field = sqlrows.db[tablename][fieldname]
to
    try:
         field = sqlrows.db[tablename][fieldname]
    except:
         field = None

change
  sqlhtml.py:SQLTABLE.__init__
       if field.represent:
           r = field.represent(r)
to
      if not field:
          pass
      elif field.represent:
          r = field.represent(r)

---Now you are able to include a new field in SQLTABLE
containing anything(text,  link, image, button, form, whatever)
depending on row contents(ExtraField.new_column(self): self is the
Row) ,
by adding the field to db().select() result,.

---Note:
in
class ExtraField:
name of the class doesn't matter. The only time you use it is

rows.setvirtualfields(some_table=ExtraField())

then address your new column by 'some_table.new_column' while rows
object exists.

---Limitation:
in

rows.colnames.append('some_table.new_column')
rows.setvirtualfields(some_table=ExtraField())

'some_table' shell be a table mentioned in your select and present in
the db,
 otherwise
'some_table' will be added as a sub dict to Row object
(IMHO this is wrong, the Row object shell be 1d always, the 'as' info
shell be in column names)
and you will not see it.

WBR
Ivan.

---Disclaimer : my understanding of web2py is not mature. For real
thing ask Massimo.

PS.
---Thank you Massimo. web2py is a great stuff. 1 month I'm learning
python and web2py is the motivation.

PPS.
Can I use <code> or <pre> or other tags to post code on the list?

Reply via email to