Merge pull request #687 from poiati/flash-signal
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Flash messages signals
authored by Armin Ronacher · committed by Armin Ronacher · parents 0a29c29, b2aae04 · Browse files · 4 files·+32 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+32 added−1 removed
- other:+32 added−1 removed
Provenance
- Commit
5997890- Tree
b165d81- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
0a29c29- Parent 2
b2aae04
4 files changed+32 added−1 removed
modified:flask/testsuite/signals.py+27 added
6 unmodified lines7 8 :copyright: (c) 2011 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+from __future__ import with_statement12+13 import flask14 import unittest15 from flask.testsuite import FlaskTestCase16 79 unmodified lines96 self.assert_(isinstance(recorded[0], ZeroDivisionError))97 finally:98 flask.got_request_exception.disconnect(record, app)99 100+ def test_flash_signal(self):101+ app = flask.Flask(__name__)102+ app.config['SECRET_KEY'] = 'secret'103 104+ @app.route('/')105+ def index():106+ flask.flash('This is a flash message', category='notice')107+ return flask.redirect('/other')108+109+ recorded = []110+ def record(sender, message, category):111+ recorded.append((message, category))112+113+ flask.message_flashed.connect(record, app)114+ try:115+ client = app.test_client()116+ with client.session_transaction():117+ client.get('/')118+ self.assert_equal(len(recorded), 1)119+ message, category = recorded[0]120+ self.assert_equal(message, 'This is a flash message')121+ self.assert_equal(category, 'notice')122+ finally:123+ flask.message_flashed.disconnect(record, app)124+125+126 def suite():127 suite = unittest.TestSuite()128 if flask.signals_available:129 suite.addTest(unittest.makeSuite(SignalsTestCase))1 unmodified linemodified:flask/__init__.py+1 added-1 removed
32 unmodified lines33 34 # the signals35 from .signals import signals_available, template_rendered, request_started, \36 request_finished, got_request_exception, request_tearing_down, \37- appcontext_tearing_down37+ appcontext_tearing_down, message_flashed38 39 # We're not exposing the actual json module but a convenient wrapper around40 # it.41 from . import json8 unmodified linesmodified:flask/helpers.py+3 added
33 unmodified lines34 from werkzeug.utils import wrap_file35 36 from jinja2 import FileSystemLoader37 38+from .signals import message_flashed39 from .globals import session, _request_ctx_stack, _app_ctx_stack, \40 current_app, request41 42 318 unmodified lines361 # implementations that use external storage for keeping their keys/values.362 flashes = session.get('_flashes', [])363 flashes.append((category, message))364 session['_flashes'] = flashes365+ message_flashed.send(current_app._get_current_object(),366+ message=message, category=category)367 368 369 def get_flashed_messages(with_categories=False, category_filter=[]):370 """Pulls all flashed messages from the session and returns them.475 unmodified linesmodified:flask/signals.py+1 added
48 unmodified lines49 request_finished = _signals.signal('request-finished')50 request_tearing_down = _signals.signal('request-tearing-down')51 got_request_exception = _signals.signal('got-request-exception')52 appcontext_tearing_down = _signals.signal('appcontext-tearing-down')53+message_flashed = _signals.signal('message-flashed')