Browse Source

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 <armin.ronacher@active-4.com>
pull/203/head
mvantellingen 14 years ago committed by Armin Ronacher
parent
commit
b3fc9eb36b
  1. 11
      flask/ctx.py

11
flask/ctx.py

@ -30,11 +30,9 @@ class _RequestContext(object):
self.app = app self.app = app
self.request = app.request_class(environ) self.request = app.request_class(environ)
self.url_adapter = app.create_url_adapter(self.request) 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.g = _RequestGlobals()
self.flashes = None self.flashes = None
self.session = None
try: try:
url_rule, self.request.view_args = \ url_rule, self.request.view_args = \
@ -47,6 +45,13 @@ class _RequestContext(object):
"""Binds the request context.""" """Binds the request context."""
_request_ctx_stack.push(self) _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): def pop(self):
"""Pops the request context.""" """Pops the request context."""
_request_ctx_stack.pop() _request_ctx_stack.pop()

Loading…
Cancel
Save