Massimo

I think that extending the T2 tables, especially t2_person, could be
quite common and I wondered if people think it would be useful to
extend a t2 table without having to re-define it all in the app
model.  This has the advantage that changes to the "built-in" t2
columns are transparent - assuming that's a good thing.

The following works (with a patch to t2.py - see end) including
automatically applying changes to the app extension fields:

In the app model, e.g. db.py:

from applications.plugin_t2.modules.t2 import T2
db.define_table('t2_person',T2.extend_table(db,'t2_person',now),SQLField('my_extra_field'))
t2=T2(request,response,session,cache,T,db)

In t2.py, a couple of changes:
1) expose a new static method 'extend_table' that returns an SQL table
for the requested t2 table - add private methods like '_person_table'
for any t2 table that can be extended.
    def extend_table(db,tablename,now):
        if tablename=='t2_person':
            return T2._person_table(db,now)
    extend_table=staticmethod(extend_table)

    def _person_table(db,now):
        t=SQLTable(db,'t2_person',
           db.Field('name'),
           db.Field('email'),
           db.Field('registration_key'),
           db.Field('password','password'),
           db.Field('created_by_ip'),
           db.Field('created_on','datetime'))
        t.name.requires=IS_NOT_EMPTY()
 
t.email.requires=[IS_EMAIL(),IS_NOT_IN_DB(db,'t2_person.email')]
        t.password.requires=[IS_NOT_EMPTY(),CRYPT()]
        t.registration_key.default=str(uuid.uuid4())
        t.created_on.default=now
        t.exposes=['name','email','password']
        t.displays=['name','email','password']
        return t
    _person_table=staticmethod(_person_table)

2) in create_tables, to save duplication, replace the define_table
with
        if not db.has_key('t2_person'):
 
t=db.define_table('t2_person',T2._person_table(db),self.now)


On Nov 2, 6:55 am, mdipierro <[EMAIL PROTECTED]> wrote:
> oops, you are right.
>
> Massimo
>
> On Nov 2, 1:46 am, billf <[EMAIL PROTECTED]> wrote:
>
> > You need a slight amendment to the second example above.  I believe
> > the following:
>
> > from gluon.sql import SQLTable
> > base_table=SQLTable('base_table',SQLField('myfield'))
> > db.define_table('derived_table',base_table,SQLField('otherfield'))
>
> > (only derived_table is created)
>
> > ...should be:
>
> > from gluon.sql import SQLTable
> > base_table=SQLTable(db,'base_table',SQLField('myfield'))
> > db.define_table('derived_table',base_table,SQLField('otherfield'))
>
> > Note the addition of 'db' as the first arg of SQLTable  If you omit it
> > you lose the first field of the base_table.
>
> > On Nov 2, 5:30 am, mdipierro <[EMAIL PROTECTED]> wrote:
>
> > > Normally you can do
>
> > > db.define_table('base_table',SQLField('myfield'))
> > > db.define_table('derived_table',db.base_table,SQLField('otherfield'))
>
> > > (both base_table and derived_table are created)
>
> > > or
>
> > > from gluon.sql import SQLTable
> > > base_table=SQLTable('base_table',SQLField('myfield'))
> > > db.define_table('derived_table',base_table,SQLField('otherfield'))
>
> > > (only derived_table is created)
>
> > > In the case T2 things are different because you want to modify the
> > > structure of a table used by T2.
> > > You can do it.
> > > All you need to do is define the table before you instantiate the T2
> > > object. Something like this:
>
> > >      db.define_table('t2_person',
> > >                db.Field('name'),
> > >                db.Field('email'),
> > >                db.Field('registration_key'),
> > >                db.Field('password','password'),
> > >                db.Field('created_by_ip'),
> > >                db.Field('created_on','datetime'))
> > >      db.t2_person.name.requires=IS_NOT_EMPTY()
>
> > > db.t2_person.email.requires=[IS_EMAIL(),IS_NOT_IN_DB(db,'t2_person.email')]
> > >      db.t2_person.password.requires=[IS_NOT_EMPTY(),CRYPT()]
> > >      db.t2_person.exposes=['name','email','password']
> > >      db.t2_person.displays=['name','email','password']
>
> > >      t2=T2(request,response,session,cache,T,db)
>
> > > you can add fields to the t2_person table but you must have the above
> > > fields.
>
> > > Massimo
>
> > > On Nov 1, 11:33 pm, Oscar <[EMAIL PROTECTED]> wrote:
>
> > > > Hi,
>
> > > > I was looking for how to extend a model, I mean, I working with T2 and
> > > > I want to add some more fields to t2_persona table, I tryed defining
> > > > again the table in my new project but It didn't worked. Some oen knows
> > > > how I can accomplish this?
>
> > > > Regards,
>
> > > > Oscar.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to