Flask Tryton: Transaction context processor and blueprint

288 views
Skip to first unread message

Raimon Esteve

unread,
Jun 4, 2014, 3:51:32 AM6/4/14
to tryto...@googlegroups.com
Hello,

I'm working a Flask APP and use Flask Tryton to connect to Tryton (1)

I have blueprints and context processor to connect to Tryton

* Blueprint (2). Modular Application
* Context Processor: Functions in templates

I have some routes or views call Tryton with @tryton.transaction
Also I have in some context processor call Trytn with @tryton.transaction.

My problem is about I can't start two or more transaction. Second transaction, self.user is not None and get assert error (4)

I don't find solution to use Blueprint and Context Processor working together. Any ideas?

Example:

app.py

from flask import app
from flask_tryton import Tryton
...

@app.context_processor
def cms_processor():

    tryton = Tryton(app)

    @tryton.transaction()
    def menu(code):
        print "Call tryton model"
        return []

    return dict(cms_menu=menu)

blueprint.py

from flask import Blueprint, current_app
from flask_tryton import Tryton

cms = Blueprint('cms', __name__, template_folder='templates')

@cms.route("/<slug>", endpoint="article")
def article(lang, slug):
    tryton = Tryton(current_app)

    @tryton.transaction()
    def _get_article(slug):
        print "Call tryton model"

Cédric Krier

unread,
Jun 4, 2014, 4:11:36 AM6/4/14
to tryto...@googlegroups.com
On 04 Jun 09:51, Raimon Esteve wrote:
> Hello,
>
> I'm working a Flask APP and use Flask Tryton to connect to Tryton (1)
>
> I have blueprints and context processor to connect to Tryton
>
> * Blueprint (2). Modular Application
> * Context Processor: Functions in templates
>
> I have some routes or views call Tryton with @tryton.transaction
> Also I have in some context processor call Trytn with @tryton.transaction.
>
> My problem is about I can't start two or more transaction. Second
> transaction, self.user is not None and get assert error (4)

Why would you want to start more than 1 transaction?

> I don't find solution to use Blueprint and Context Processor working
> together. Any ideas?

The tryton.transaction should only be applied on entry point.

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Raimon Esteve

unread,
Jun 4, 2014, 6:35:07 AM6/4/14
to tryto...@googlegroups.com
2014-06-04 10:11 GMT+02:00 Cédric Krier <cedric...@b2ck.com>:
On 04 Jun 09:51, Raimon Esteve wrote:
> Hello,
>
> I'm working a Flask APP and use Flask Tryton to connect to Tryton (1)
>
> I have blueprints and context processor to connect to Tryton
>
> * Blueprint (2). Modular Application
> * Context Processor: Functions in templates
>
> I have some routes or views call Tryton with @tryton.transaction
> Also I have in some context processor call Trytn with @tryton.transaction.
>
> My problem is about I can't start two or more transaction. Second
> transaction, self.user is not None and get assert error (4)

Why would you want to start more than 1 transaction?

First time I understand each decorator call a "independent" transaction (new transaction each method)

I continue working about context processor and blueprints....

tryton.py

from flask import current_app
from flask_tryton import Tryton

tryton = Tryton(current_app)

Now I get message "RuntimeError: working outside of application context" because I init tryton outsite app. I try "with current_app.app_context()"  and not expect good results.. If I change some parts "Flask tryton" to init without app, I get successfull result.

Any ideas? I continue investigating....


app.py

from tryton import tryton

@app.context_processor
def cms_processor():

    def menu(code=None):
        Menu = tryton.pool.get('cms.menu')
        print "call search tryton

    return dict(cms_menu=menu)

tryton.py

from flask import current_app
from flask_tryton import Tryton

tryton = Tryton()

blueprint.py

from flask import Blueprint, current_app
from galatea.tryton import tryton


cms = Blueprint('cms', __name__, template_folder='templates')

@cms.route("/<slug>", endpoint="article")
@tryton.transaction()
def article(lang, slug):
    Article = tryton.pool.get('cms.article')
    print "call search tryton model"



Cédric Krier

unread,
Jun 4, 2014, 6:59:54 AM6/4/14
to tryto...@googlegroups.com
On 04 Jun 12:35, Raimon Esteve wrote:
> 2014-06-04 10:11 GMT+02:00 Cédric Krier <cedric...@b2ck.com>:
>
> > On 04 Jun 09:51, Raimon Esteve wrote:
> > > Hello,
> > >
> > > I'm working a Flask APP and use Flask Tryton to connect to Tryton (1)
> > >
> > > I have blueprints and context processor to connect to Tryton
> > >
> > > * Blueprint (2). Modular Application
> > > * Context Processor: Functions in templates
> > >
> > > I have some routes or views call Tryton with @tryton.transaction
> > > Also I have in some context processor call Trytn with
> > @tryton.transaction.
> > >
> > > My problem is about I can't start two or more transaction. Second
> > > transaction, self.user is not None and get assert error (4)
> >
> > Why would you want to start more than 1 transaction?
> >
>
> First time I understand each decorator call a "independent" transaction
> (new transaction each method)
>
> I continue working about context processor and blueprints....
>
> *tryton.py*
>
> from flask import current_app
> from flask_tryton import Tryton
>
> tryton = Tryton(current_app)
>
> Now I get message "RuntimeError: working outside of application context"
> because I init tryton outsite app. I try "with current_app.app_context()"
> and not expect good results.. If I change some parts "Flask tryton" to init
> without app, I get successfull result.
>
> Any ideas? I continue investigating....

There was a little mistake in the initial design of flask-tryton.
It should be fixed with
https://code.google.com/p/flask-tryton/source/detail?r=34c8b01ac62f6ad2431c90088f32a87f8e6df7af
Otherwise, you should not use current_app but import the instance of
Flask from the right module.

> *app.py*
>
> from tryton import tryton
>
> @app.context_processor
> def cms_processor():
>
> def menu(code=None):
> Menu = tryton.pool.get('cms.menu')

Should be (with latest code):

tryton = current_app.extensions['Tryton']
Menu = tryton.pool.get('cms.menu')

> print "call search tryton
>
> return dict(cms_menu=menu)
>
> *tryton.py*
>
> from flask import current_app
> from flask_tryton import Tryton
>
> tryton = Tryton()
>
> *blueprint.py*
>
> from flask import Blueprint, current_app
> from galatea.tryton import tryton
>
> cms = Blueprint('cms', __name__, template_folder='templates')
>
> @cms.route("/<slug>", endpoint="article")
> @tryton.transaction()
> def article(lang, slug):
> Article = tryton.pool.get('cms.article')

init_app should be called to use the pool.

> print "call search tryton model"

Raimon Esteve

unread,
Jun 4, 2014, 7:03:09 AM6/4/14
to tryto...@googlegroups.com
Now I get message "RuntimeError: working outside of application context" because I init tryton outsite app. I try "with current_app.app_context()"  and not expect good results.. If I change some parts "Flask tryton" to init without app, I get successfull result.

Eureka! Force ctx push is a solution! Or maybe solution.

app.py

ctx = app.app_context()
ctx.push()

tryton.py

from flask import current_app
from flask_tryton import Tryton

with current_app.app_context():
    tryton = Tryton(current_app)

Reply all
Reply to author
Forward
0 new messages