Skip to content
Myr

Added support for flashing categories.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This fixes #35.

authored by Armin Ronacher · committed by Armin Ronacher · parent 693e444 · Browse files · 3 files+60 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+60 added−4 removed

  • tests:+29 added−0 removed
  • other:+31 added−4 removed

3 files+60 added−4 removed

modified:tests/flask_tests.py+29 added
155 unmodified lines
156 flask.flash('Zip')157 assert flask.session.modified158 assert list(flask.get_flashed_messages()) == ['Zap', 'Zip']159160 def test_extended_flashing(self):161 app = flask.Flask(__name__)162 app.secret_key = 'testkey'163164 @app.route('/')165 def index():166 flask.flash(u'Hello World')167 flask.flash(u'Hello World', 'error')168 flask.flash(flask.Markup(u'<em>Testing</em>'), 'warning')169 return ''170171 @app.route('/test')172 def test():173 messages = flask.get_flashed_messages(with_categories=True)174 assert len(messages) == 3175 assert messages[0] == ('message', u'Hello World')176 assert messages[1] == ('error', u'Hello World')177 assert messages[2] == ('warning', flask.Markup(u'<em>Testing</em>'))178 return ''179 messages = flask.get_flashed_messages()180 assert len(messages) == 3181 assert messages[0] == u'Hello World'182 assert messages[1] == u'Hello World'183 assert messages[2] == flask.Markup(u'<em>Testing</em>')184185 c = app.test_client()186 c.get('/')187 c.get('/test')188189 def test_request_processing(self):190 app = flask.Flask(__name__)191 evts = []192 @app.before_request
413 unmodified lines
modified:CHANGES+2 added
6 unmodified lines
7-----------89Release date to be announced1011- added support for categories for flashed messages.1213Version 0.214-----------1516Released on May 12th, codename Jägermeister
20 unmodified lines
modified:flask.py+29 added-4 removed
215 unmodified lines
216 return getattr(current_app.jinja_env.get_template(template_name).module,217 attribute)218219220def flash(message):220def flash(message, category='message'):221 """Flashes a message to the next request. In order to remove the222 flashed message from the session and to display it to the user,223 the template has to call :func:`get_flashed_messages`.224225 .. versionchanged: 0.5226 `category` parameter added.227228 :param message: the message to be flashed.229 :param category: the category for the message. The following values230 are recommended: ``'message'`` for any kind of message,231 ``'error'`` for errors, ``'info'`` for information232 messages and ``'warning'`` for warnings. However any233 kind of string and be used as category.234 """227 session.setdefault('_flashes', []).append(message)235 session.setdefault('_flashes', []).append((category, message))236237230def get_flashed_messages():238def get_flashed_messages(with_categories=False):239 """Pulls all flashed messages from the session and returns them.240 Further calls in the same request to the function will return233 the same messages.241 the same messages. By default just the messages are returned,242 but when `with_categories` is set to `True`, the return value will243 be a list of tuples in the form ``(category, message)`` instead.244245 Example usage:246247 .. sourcecode:: html+jinja248249 {% for category, msg in get_flashed_messages(with_categories=true) %}250 <p class=flash-{{ category }}>{{ msg }}251 {% endfor %}252253 .. versionchanged:: 0.5254 `with_categories` parameter added.255256 :param with_categories: set to `True` to also receive categories.257 """258 flashes = _request_ctx_stack.top.flashes259 if flashes is None:260 _request_ctx_stack.top.flashes = flashes = session.pop('_flashes', [])261 if not with_categories:262 return [x[1] for x in flashes]263 return flashes264265266def jsonify(*args, **kwargs):
896 unmodified lines