forked from orbit-oss/flask
Added appcontext_pushed and appcontext_popped signals
This commit is contained in:
parent
fd99abea57
commit
0676bb8ab5
7 changed files with 87 additions and 2 deletions
|
|
@ -34,7 +34,8 @@ from .templating import render_template, render_template_string
|
|||
# the signals
|
||||
from .signals import signals_available, template_rendered, request_started, \
|
||||
request_finished, got_request_exception, request_tearing_down, \
|
||||
appcontext_tearing_down, message_flashed
|
||||
appcontext_tearing_down, appcontext_pushed, \
|
||||
appcontext_popped, message_flashed
|
||||
|
||||
# We're not exposing the actual json module but a convenient wrapper around
|
||||
# it.
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ from werkzeug.exceptions import HTTPException
|
|||
|
||||
from .globals import _request_ctx_stack, _app_ctx_stack
|
||||
from .module import blueprint_is_module
|
||||
from .signals import appcontext_pushed, appcontext_popped
|
||||
|
||||
|
||||
class _AppCtxGlobals(object):
|
||||
|
|
@ -166,6 +167,7 @@ class AppContext(object):
|
|||
"""Binds the app context to the current context."""
|
||||
self._refcnt += 1
|
||||
_app_ctx_stack.push(self)
|
||||
appcontext_pushed.send(self.app)
|
||||
|
||||
def pop(self, exc=None):
|
||||
"""Pops the app context."""
|
||||
|
|
@ -177,6 +179,7 @@ class AppContext(object):
|
|||
rv = _app_ctx_stack.pop()
|
||||
assert rv is self, 'Popped wrong app context. (%r instead of %r)' \
|
||||
% (rv, self)
|
||||
appcontext_popped.send(self.app)
|
||||
|
||||
def __enter__(self):
|
||||
self.push()
|
||||
|
|
|
|||
|
|
@ -50,4 +50,6 @@ request_finished = _signals.signal('request-finished')
|
|||
request_tearing_down = _signals.signal('request-tearing-down')
|
||||
got_request_exception = _signals.signal('got-request-exception')
|
||||
appcontext_tearing_down = _signals.signal('appcontext-tearing-down')
|
||||
appcontext_pushed = _signals.signal('appcontext-pushed')
|
||||
appcontext_popped = _signals.signal('appcontext-popped')
|
||||
message_flashed = _signals.signal('message-flashed')
|
||||
|
|
|
|||
|
|
@ -96,6 +96,30 @@ class SignalsTestCase(FlaskTestCase):
|
|||
finally:
|
||||
flask.got_request_exception.disconnect(record, app)
|
||||
|
||||
def test_appcontext_signals(self):
|
||||
app = flask.Flask(__name__)
|
||||
recorded = []
|
||||
def record_push(sender, **kwargs):
|
||||
recorded.append('push')
|
||||
def record_pop(sender, **kwargs):
|
||||
recorded.append('push')
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Hello'
|
||||
|
||||
flask.appcontext_pushed.connect(record_push, app)
|
||||
flask.appcontext_popped.connect(record_pop, app)
|
||||
try:
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
self.assert_equal(rv.data, b'Hello')
|
||||
self.assert_equal(recorded, ['push'])
|
||||
self.assert_equal(recorded, ['push', 'pop'])
|
||||
finally:
|
||||
flask.appcontext_pushed.disconnect(record_push, app)
|
||||
flask.appcontext_popped.disconnect(record_pop, app)
|
||||
|
||||
def test_flash_signal(self):
|
||||
app = flask.Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'secret'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue