Nice example!

On Saturday, 23 March 2013 16:18:39 UTC-5, Peter Etchells wrote:
>
> I was playing around with impressive model graph feature, & thought you 
> may be interested in the following simple model, with these features:
>
>
>    - multi-tenancy 
> <http://web2py.com/books/default/chapter/29/06#Common-fields-and-multi-tenancy>
>    - record 
> versioning<http://web2py.com/books/default/chapter/29/06#Record-versioning>(including
>  auth 
>    tables <http://web2py.com/books/default/chapter/29/09#Customizing-Auth>
>    )
>    - unique id for each record (eg for syncing across multiple 
> dbs<http://web2py.com/books/default/chapter/29/06#CSV-and-remote-database-synchronization>
>    )
>
> 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)
>
>
> <https://lh5.googleusercontent.com/-deVndbHcF4k/UU4btCCGUvI/AAAAAAAABBI/pSj2cn6ZUoE/s1600/graph.png>
>
>

-- 

--- 
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to