From b3fc9eb36b8021ad966be57532a5fa5ae4231e13 Mon Sep 17 00:00:00 2001 From: mvantellingen Date: Fri, 11 Mar 2011 15:33:35 +0100 Subject: [PATCH] Open the session after the request context is pushed on the stack instead of in the constructor. This allows you to access the request context in a custom open_session method. Signed-off-by: Armin Ronacher --- flask/ctx.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/flask/ctx.py b/flask/ctx.py index 1b17086c..d3ac231d 100644 --- a/flask/ctx.py +++ b/flask/ctx.py @@ -30,11 +30,9 @@ class _RequestContext(object): self.app = app self.request = app.request_class(environ) self.url_adapter = app.create_url_adapter(self.request) - self.session = app.open_session(self.request) - if self.session is None: - self.session = _NullSession() self.g = _RequestGlobals() self.flashes = None + self.session = None try: url_rule, self.request.view_args = \ @@ -47,6 +45,13 @@ class _RequestContext(object): """Binds the request context.""" _request_ctx_stack.push(self) + # Open the session at the moment that the request context is + # available. This allows a custom open_session method to use the + # request context (e.g. flask-sqlalchemy). + self.session = self.app.open_session(self.request) + if self.session is None: + self.session = _NullSession() + def pop(self): """Pops the request context.""" _request_ctx_stack.pop()