#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)