librelist archives

« back to archive

Extension that registers a blueprint

Extension that registers a blueprint

From:
Alfred Hall
Date:
2011-10-06 @ 09:00
Hi there,

I have blueprints, views and templates that I'm reusing pretty much
across multiple apps. What I want to do is create a flask extension
that registers a blueprint. For the sake of simplicity lets say that
my extension is Flask-Blog with layout such as:

flaskext/
    __init__.py
    blog/
        __init__.py
        views.py (registers routes such as /blog/post/new/ and loads
of other stuff)
        deps.py (includes a way of configuring dependencies that this
extension depends on)
        models.py
        forms.py
        templates/
           blog/
              index.html
              show.html
   tests/
       test_views.py (Creates a flask app and registers the blueprints
and does bunch of tests on it)

Then in this layout I'd configure the extension like this:

from flaskext.blog import Blog
from flaskext.blog import get_deps

db = SQLAlchemy(app)

deps = get_deps()
deps.db = db
deps.perm_blogpost_create = admin_permission
deps.perm_blogpost_edit = some_other_permission

Blog(app)

Then in models.py the models derive from deps.db.Model.

What I'm asking is if there is a more de-facto way of creating a flask
extensions that depend on other extensions and register blueprints and
if what I'm doing here is a sensible way or if there is an
obvious pitfall with this approach. I haven't found any extension
online that does anything similar to this. One problem I can see is
maintaining the database schema when I release a new version
of my extensions but that could be solved with database migrations or
simply good documentation.

Cheers,
Al

Re: [flask] Extension that registers a blueprint

From:
Italo Maia
Date:
2011-10-06 @ 13:50
Why don't you register your blueprints inside the factory? It is very
simple.

The pattern: http://flask.pocoo.org/docs/patterns/appfactories/
The example: https://bitbucket.org/danjac/newsmeme


2011/10/6 Alfred Hall <ahall@ahall.org>

> Hi there,
>
> I have blueprints, views and templates that I'm reusing pretty much
> across multiple apps. What I want to do is create a flask extension
> that registers a blueprint. For the sake of simplicity lets say that
> my extension is Flask-Blog with layout such as:
>
> flaskext/
>    __init__.py
>    blog/
>        __init__.py
>        views.py (registers routes such as /blog/post/new/ and loads
> of other stuff)
>        deps.py (includes a way of configuring dependencies that this
> extension depends on)
>        models.py
>        forms.py
>        templates/
>           blog/
>              index.html
>              show.html
>   tests/
>       test_views.py (Creates a flask app and registers the blueprints
> and does bunch of tests on it)
>
> Then in this layout I'd configure the extension like this:
>
> from flaskext.blog import Blog
> from flaskext.blog import get_deps
>
> db = SQLAlchemy(app)
>
> deps = get_deps()
> deps.db = db
> deps.perm_blogpost_create = admin_permission
> deps.perm_blogpost_edit = some_other_permission
>
> Blog(app)
>
> Then in models.py the models derive from deps.db.Model.
>
> What I'm asking is if there is a more de-facto way of creating a flask
> extensions that depend on other extensions and register blueprints and
> if what I'm doing here is a sensible way or if there is an
> obvious pitfall with this approach. I haven't found any extension
> online that does anything similar to this. One problem I can see is
> maintaining the database schema when I release a new version
> of my extensions but that could be solved with database migrations or
> simply good documentation.
>
> Cheers,
> Al
>



-- 
"A arrogância é a arma dos fracos."

===========================
Italo Moreira Campelo Maia
Bacharel em Ciência da Computação - UECE
Desenvolvedor WEB e Desktop (Java, Python, Lua)
Coordenador do Pug-CE
-----------------------------------------------------
http://www.italomaia.com/
http://twitter.com/italomaia/
http://eusouolobomau.blogspot.com/
-----------------------------------------------------
Turtle Linux  9.10 - http://tiny.cc/blogturtle910
Turtle Linux 10.10 - http://bit.ly/cEw4ET
===========================