librelist archives

« back to archive

Access to g object

Access to g object

From:
arnaud.le-roy@voila.fr
Date:
2012-06-19 @ 14:22
Hi,

I am trying to use the flask_login extension, where I have to write a 
callback to load a user :
@login_manager.user_loader
def load_user(id):
mu = ManageUser()
cur=get_cursor()
(check,result) = mu.checkUserById(cur, int(id))
cur.close()
return result

I use the g variable to keep my database connection :

@app.before_request
def before_request():
g.db = connect_db()

def get_cursor():
cur = g.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
return cur

However, when I call the get_cursor function from load_user, I have an exception
AttributeError: '_RequestGlobals' object has no attribute 'db'
I take as if g exists but before_request is not called yet or hidden when 
in the context of load_user.
Do I have a way to access my g.db variable, or do I need to attach it to 
something different ? (I've looked at the idea of application contexts but
as I am starting for now I don't understand clearly how to use them, while
the g variable is very straightforward)

Thanks,
Arnaud

___________________________________________________________
Obligatoire au 1er juillet, achetez votre éthylotest sur Voila.fr 
http://shopping.voila.fr/thematique/ethylotest

Re: [flask] Access to g object

From:
Simon Sapin
Date:
2012-06-20 @ 07:55
Le 19/06/2012 16:22, arnaud.le-roy@voila.fr a écrit :
> I am trying to use the flask_login extension, where I have to write a 
callback to load a user :
> @login_manager.user_loader
> def load_user(id):
> mu = ManageUser()
> cur=get_cursor()
> (check,result) = mu.checkUserById(cur, int(id))
> cur.close()
> return result
>
> I use the g variable to keep my database connection :
>
> @app.before_request
> def before_request():
> g.db = connect_db()
>
> def get_cursor():
> cur = g.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
> return cur
>
> However, when I call the get_cursor function from load_user, I have an exception
> AttributeError: '_RequestGlobals' object has no attribute 'db'

Hi,

I *guess* that the user loader is called from some other before_request 
function which happens to be in the list before your db stuff.

Either try to register you db setup function before you install 
Flask-Login, or try something like this instead of @app.before_request

app.before_request_funcs.setdefault(None, []).insert(0, f)

where f is your function.

-- 
Simon Sapin

Re: [flask] Access to g object

From:
arnaud.le-roy@voila.fr
Date:
2012-06-20 @ 08:59
Simon, 

Thanks : both solutions are working :-) (moving my @app.before before 
installing flask-login or using app.before_request_funcs after having 
installed flask-login)

Arnaud


> Message du 20/06/12 à 09h59
> De : "Simon Sapin" 
> A : flask@librelist.com
> Copie à : 
> Objet : Re: [flask] Access to g object
> 
> Le 19/06/2012 16:22, arnaud.le-roy@voila.fr a écrit :
> > I am trying to use the flask_login extension, where I have to write a 
callback to load a user :
> > @login_manager.user_loader
> > def load_user(id):
> > mu = ManageUser()
> > cur=get_cursor()
> > (check,result) = mu.checkUserById(cur, int(id))
> > cur.close()
> > return result
> >
> > I use the g variable to keep my database connection :
> >
> > @app.before_request
> > def before_request():
> > g.db = connect_db()
> >
> > def get_cursor():
> > cur = g.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
> > return cur
> >
> > However, when I call the get_cursor function from load_user, I have an
exception
> > AttributeError: '_RequestGlobals' object has no attribute 'db'
> 
> Hi,
> 
> I *guess* that the user loader is called from some other before_request 
> function which happens to be in the list before your db stuff.
> 
> Either try to register you db setup function before you install 
> Flask-Login, or try something like this instead of @app.before_request
> 
> app.before_request_funcs.setdefault(None, []).insert(0, f)
> 
> where f is your function.
> 
> -- 
> Simon Sapin
> 
> 
> 

___________________________________________________________
Obligatoire au 1er juillet, achetez votre éthylotest sur Voila.fr 
http://shopping.voila.fr/thematique/ethylotest