Hi guys,

I'm developing a multi-tenant application.  The model is pretty simple:

* Each user as a tenant id
* Some objects has a tenant_id and some doesn't, if the object has a
tenant_id, I have to filter by this colum

I don't want to have a
DBSession.query(SomeObject).filter(filter_by_tenant_id).filter(some_other_filter).all()

So I had a method to DBSession called tdb that do all the work and return a
clear Query Object, so I did the following:

#in application __init__
config.add_subscriber(tenantdb_on_new_request, events.NewRequest)

#In models
DBSession =
scoped_session(sessionmaker(extension=ZopeTransactionExtension()))


def tenantdb_on_new_request(event):

    def tenant_db(*entities, **kwargs):
        q = DBSession.query(*entities, **kwargs)
        for e in entities:
            if hasattr(e, 'tenant_id'):
                q = q.filter(e.tenant_id == event.request.user.tenant)
        return q

    DBSession.tdb = tenant_db

It's works fine, but it's ok? May I have problems when many requests came
in production?  Some idea to do it in other way?

Thanks in advance!

Best regards,

Luis Aguirre

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to