Browse Source

Use setdefault() because it sets session.modified correctly.

pull/1638/head
florentx 15 years ago
parent
commit
b6e7330539
  1. 4
      flask.py
  2. 12
      tests/flask_tests.py

4
flask.py

@ -167,7 +167,7 @@ def flash(message):
:param message: the message to be flashed.
"""
session['_flashes'] = session.get('_flashes', ()) + (message,)
session.setdefault('_flashes', []).append(message)
def get_flashed_messages():
@ -177,7 +177,7 @@ def get_flashed_messages():
"""
flashes = _request_ctx_stack.top.flashes
if flashes is None:
_request_ctx_stack.top.flashes = flashes = session.pop('_flashes', ())
_request_ctx_stack.top.flashes = flashes = session.pop('_flashes', [])
return flashes

12
tests/flask_tests.py

@ -117,6 +117,18 @@ class BasicFunctionalityTestCase(unittest.TestCase):
expect_exception(flask.session.__setitem__, 'foo', 42)
expect_exception(flask.session.pop, 'foo')
def test_flashes(self):
app = flask.Flask(__name__)
app.secret_key = 'testkey'
with app.test_request_context():
assert not flask.session.modified
flask.flash('Zap')
flask.session.modified = False
flask.flash('Zip')
assert flask.session.modified
assert list(flask.get_flashed_messages()) == ['Zap', 'Zip']
def test_request_processing(self):
app = flask.Flask(__name__)
evts = []

Loading…
Cancel
Save