example of model w/ model graph, multi-tenancy, record versioning (incl auth tables), and record uid

173 views
Skip to first unread message

Peter Etchells

unread,
Mar 23, 2013, 6:03:35 PM3/23/13
to
I was playing around with impressive model graph feature, & thought you may be interested in the following simple model, with these features:

total code is about 14 lines, all in model db.py in a new empty application

#this goes after auth=Auth(db) 
#model code to implement multi-tenancy and tenant name table, record versioning (including for auth tables), uid for all tables( for syncing multiple dbs)
#this field in a table triggers multi-tenancy
request_tenant=Field('request_tenant', default=request.env.http_host, writable=False) #try request.env.server_port on local machine for local demo
#this field is a unique id for any record, anywhere. could/should be unique=True
unique_id=Field('uuid', length=64, default=lambda: str(uuid.uuid4()), writable=False)
#extra fields for auth & all tables
extra_fields=[request_tenant, auth.signature, unique_id]

#example of adding extra fields to various auth tables
#auth_user gets two extra fields as below. auth_event gets no extra fields. other auth tables get the extra_fields defined above.
auth.settings.extra_fields['auth_user']=[   Field('country'),   Field('tz_offset', 'double', default=0.0),   ]+extra_fields
#example of extra fields for multiple auth tables
#this is to exclude auth_event from getting versioning and archive
for auth_table in 'cas membership permission group'.split():
   auth.settings.extra_fields['auth_'+auth_table]=extra_fields
#auth.settings.extra_fields['auth_event']=[unique_id] # if we wanted unique events across multiple dbs  
#instead of the above, we could just do auth.define_tables after common_fields if we wanted *all* auth_ tables to also have versioning and archive
auth.define_tables(username=False, signature=False)

## configure email
#...
#use_janrain(auth, filename='private/janrain.key')

#this at end of model 

#example of extra fields for all other tables except auth tables, which have already been defined above. this implements record versioning
for f in extra_fields:
    db._common_fields.append(f)
    
#the tenant table below will have only one visible record, depending on tenant (if record exists)...
#eg tenant_name=db().select(db.tenant.name, cacheable=True, cache=(cache.ram, 1000)).first().name)
db.define_table('tenant',
    Field('name', label='tenant name'), #could/should be unique=True
    )
#sample basic table to demonstrate links to web2py tables
db.define_table('mytable',
    Field('name'),
    )

#here define more tables that will have record versioning
auth.enable_record_versioning(db)
#now define any tables that won't have record versioning (eg event logs)


graph.pdf

Massimo Di Pierro

unread,
Mar 31, 2013, 7:38:36 PM3/31/13
to web...@googlegroups.com
Nice example!

Massimo Di Pierro

unread,
Apr 6, 2013, 12:13:40 PM4/6/13
to web...@googlegroups.com
Can you post a web2pyslice about this?

Gene

unread,
Jun 9, 2015, 11:06:47 PM6/9/15
to web...@googlegroups.com
Regarding the appending of common fields, seems to work for me when its declared at the beginning of the model file, not end. Does the placement matter, can anyone confirm/deny?

Massimo Di Pierro

unread,
Jun 10, 2015, 3:52:59 PM6/10/15
to web...@googlegroups.com, ooi...@gmail.com
Yes, it matters.
Reply all
Reply to author
Forward
0 new messages