librelist archives

« back to archive

Flask multi tenancy

Flask multi tenancy

From:
Daniel Ferreira Jorge
Date:
2015-02-06 @ 13:18
Hi, Can someone point me to the right direction? I'm trying to develop a 
multi tenant Flask app using multiple postgres schemas and sqlalchemy. 
Every tenant will access the app using its own subdomain. Also, there is 
another requirement... Each tenant will access its own schema, AND a 
shared schema...


I found info on subdomain dispatching and multiple binds, but I need to 
combine this so when a tenant access his subdomains, the application will 
use both the shared schema (common to all tenants) and the tenant private 
schema... Are there any info on this??


Thank you!


Daniel Ferreira Jorge

Re: [flask] Flask multi tenancy

From:
Dorian Hoxha
Date:
2015-02-06 @ 20:02
Note, the schema thing doesn't have nothing to do with flask. Find out how
it's done in python and just put it in a before_request function. As far as
I remember, you'll have to do:

something like~ SET SCHEMA xxx;

And you can't set 2 schemas at the same time. So you probably have to
switch in each request.

To make dynamic subdomains:


def add_subdomain_to_global(endpoint, values):
    g.subdomain = values.pop('subdomain', None)


def add_subdomain_to_url_params(endpoint, values):
    if 'subdomain' not in values:
        values['subdomain'] = g.subdomain


def add_subdomain_support(app):
    app.url_value_preprocessor(add_subdomain_to_global)
    app.url_defaults(add_subdomain_to_url_params)


add_subdomain_support(admin_blueprint)
app.register_blueprint(admin_blueprint, subdomain='<subdomain>',
url_prefix='/admin', )


The code makes available 'subdomain' in g.subdomain AND adds on each
url_for(subdomain=g.subdomain)


On Fri, Feb 6, 2015 at 2:18 PM, Daniel Ferreira Jorge <
daniel.jorge@sticorp.com.br> wrote:

>  Hi, Can someone point me to the right direction? I'm trying to develop a
> multi tenant Flask app using multiple postgres schemas and sqlalchemy.
> Every tenant will access the app using its own subdomain. Also, there is
> another requirement... Each tenant will access its own schema, AND a shared
> schema...
>
>
>  I found info on subdomain dispatching and multiple binds, but I need to
> combine this so when a tenant access his subdomains, the application will
> use both the shared schema (common to all tenants) and the tenant private
> schema... Are there any info on this?‚Äč
>
>
>  Thank you!
>
>
>   Daniel Ferreira Jorge
>