librelist archives

« back to archive

Blueprints & dynamic subdomains

Blueprints & dynamic subdomains

From:
Amirouche Boubekki
Date:
2011-08-28 @ 23:44
Héllo,


In my next project I would like to build an application with dynamic
subdomains something in the like of appspot, blogspot and many other
applications where you can request a subdomain for your account.

My idea was the do something like this:

from flask import current_app

def get_blueprint_for_name(name):
    """returns a blueprint with for the desired subdomain name"""
    ....
    return blueprint

current_app.add_blueprint(get_blueprint_for_name('my_name')


I understand why this is a bad idea, but then how should I do that the
proper, anyone has the good idea ?


Thanks in advance

Amirouche aka. abki

Re: [flask] Blueprints & dynamic subdomains

From:
Armin Ronacher
Date:
2011-08-29 @ 01:11
Hi,

Use subdomain matching with url processors:

bp = Blueprint('frontend', __name__, subdomain='<user>')

@bp.url_defaults
def add_username(endpoint, values):
     values.setdefault('user', g.user.username)

@bp.url_value_preprocessor
def pull_user(endpoint, values):
     query = User.query.filter_by(username=values.pop('user'))
     g.user = query.first_or_404()


app = Flask(__name__)
app.register_blueprint(bp)

See this for more information: 
http://flask.pocoo.org/docs/patterns/urlprocessors/


Regards,
Armin

Re: [flask] Blueprints & dynamic subdomains

From:
Amirouche Boubekki
Date:
2011-08-29 @ 22:04
Hi,

Thanks for the fast response. I'm going to test it.

Amirouche

2011/8/29 Armin Ronacher <armin.ronacher@active-4.com>

> Hi,
>
> Use subdomain matching with url processors:
>
> bp = Blueprint('frontend', __name__, subdomain='<user>')
>
> @bp.url_defaults
> def add_username(endpoint, values):
>     values.setdefault('user', g.user.username)
>
> @bp.url_value_preprocessor
> def pull_user(endpoint, values):
>     query = User.query.filter_by(username=values.pop('user'))
>     g.user = query.first_or_404()
>
>
> app = Flask(__name__)
> app.register_blueprint(bp)
>
> See this for more information:
> http://flask.pocoo.org/docs/patterns/urlprocessors/
>
>
> Regards,
> Armin
>