Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Flash messages signals #687

Merged
merged 1 commit into from

3 participants

@poiati

If we send a signal when a template is rendered why not when a message
is flashed? One real world use case is in tests, this signal should make
flash messages expectation easier to implement.

@poiati poiati Flash messages signals
If we send a signal when a template is rendered why not when a message
is flashed? One real world use case is in tests, this signal should make
flash messages expectation easier to implement.
b2aae04
@poiati

Any thought about this?

Thanks!

@ghost

+1. Nice idea.

@klinkin

+1

@ghost

Why not add a way to edit the message (like app.process_response)?

@poiati

I think it's already possible to edit flashes in app.process_response.

@mitsuhiko
Owner

Makes a lot of sense. Pulling this.

@mitsuhiko mitsuhiko merged commit 5997890 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2013
  1. @poiati

    Flash messages signals

    poiati authored
    If we send a signal when a template is rendered why not when a message
    is flashed? One real world use case is in tests, this signal should make
    flash messages expectation easier to implement.
This page is out of date. Refresh to see the latest.
View
2  flask/__init__.py
@@ -34,7 +34,7 @@
# the signals
from .signals import signals_available, template_rendered, request_started, \
request_finished, got_request_exception, request_tearing_down, \
- appcontext_tearing_down
+ appcontext_tearing_down, message_flashed
# We're not exposing the actual json module but a convenient wrapper around
# it.
View
3  flask/helpers.py
@@ -35,6 +35,7 @@
from jinja2 import FileSystemLoader
+from .signals import message_flashed
from .globals import session, _request_ctx_stack, _app_ctx_stack, \
current_app, request
@@ -361,6 +362,8 @@ def flash(message, category='message'):
flashes = session.get('_flashes', [])
flashes.append((category, message))
session['_flashes'] = flashes
+ message_flashed.send(current_app._get_current_object(),
+ message=message, category=category)
def get_flashed_messages(with_categories=False, category_filter=[]):
View
1  flask/signals.py
@@ -50,3 +50,4 @@ def _fail(self, *args, **kwargs):
request_tearing_down = _signals.signal('request-tearing-down')
got_request_exception = _signals.signal('got-request-exception')
appcontext_tearing_down = _signals.signal('appcontext-tearing-down')
+message_flashed = _signals.signal('message-flashed')
View
27 flask/testsuite/signals.py
@@ -8,6 +8,8 @@
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
+from __future__ import with_statement
+
import flask
import unittest
from flask.testsuite import FlaskTestCase
@@ -95,6 +97,31 @@ def record(sender, exception):
finally:
flask.got_request_exception.disconnect(record, app)
+ def test_flash_signal(self):
+ app = flask.Flask(__name__)
+ app.config['SECRET_KEY'] = 'secret'
+
+ @app.route('/')
+ def index():
+ flask.flash('This is a flash message', category='notice')
+ return flask.redirect('/other')
+
+ recorded = []
+ def record(sender, message, category):
+ recorded.append((message, category))
+
+ flask.message_flashed.connect(record, app)
+ try:
+ client = app.test_client()
+ with client.session_transaction():
+ client.get('/')
+ self.assert_equal(len(recorded), 1)
+ message, category = recorded[0]
+ self.assert_equal(message, 'This is a flash message')
+ self.assert_equal(category, 'notice')
+ finally:
+ flask.message_flashed.disconnect(record, app)
+
def suite():
suite = unittest.TestSuite()
Something went wrong with that request. Please try again.