我们从Python开源项目中,提取了以下40个代码示例,用于说明如何使用flask.get_flashed_messages()。
def api_cache_key(namespace, *args, **kwargs): def gen_key(**kwargs): path = request.path reqstr = "" if request.args: for k, v in request.args.items(): reqstr += "&{}={}".format(str(k), str(v)) key = namespace + ":" + path + reqstr cache_key = key.encode('utf-8') if get_flashed_messages(): msgs = "|".join([msg[0] for msg in get_flashed_messages(with_categories=True)]) cache_key += "/" + msgs return cache_key return gen_key
def test_flashes(self): app = flask.Flask(__name__) app.secret_key = 'testkey' with app.test_request_context(): self.assert_false(flask.session.modified) flask.flash('Zap') flask.session.modified = False flask.flash('Zip') self.assert_true(flask.session.modified) self.assert_equal(list(flask.get_flashed_messages()), ['Zap', 'Zip'])
def last_flashed_message(): """Return last flashed message by flask.""" messages = get_flashed_messages(with_categories=True) if len(messages) > 0: return messages[-1] else: return None
def validation_error(path=None): if choose_media_type(acceptable_media_types(request), [html]): if path: return flask_redirect(path) else: return flask_redirect(request.referrer) else: return render_json({'errors': get_flashed_messages()})
def message(): messages = get_flashed_messages(category_filter=['message']) return messages[0] if len(messages) else None
def error(): errors = get_flashed_messages(category_filter=['error']) return errors[0] if len(errors) else None
def assert_flashes(self, expected_message, expected_category='message'): flashed_messages = get_flashed_messages(with_categories=True) for category, message in flashed_messages: if category == expected_category: self.assertEqual(message, expected_message) return raise AssertionError("nothing flashed")
def api_messages(): messages = get_flashed_messages() default_message = current_app.config.get('API_ERROR_MESSAGE') if default_message: messages.append(default_message) return messages
def recommendations(profile=None): profile_form = profile_search_form() if not profile: return render_template( 'index.html', profile_form=profile_form, ) if not valid_mal_username(profile): return redirect(url_for('invalid_name', name=profile)) print(f"received {profile} request") profile_form.name.data = profile profile = profile.lower() messages = [] diff, recommendations = latest_recommendations(profile, redis_connection) if recommendations == []: messages += [Markup('<br>I\'ve got nothing! Please make sure that: <ul><li>you\'ve typed the name of your profile correctly, <li> it <b>isn\'t private</b>, <li>and <b>your lists have scores on them</b>,</ul> then try to make another request.\n<br>')] elif recommendations is not None: recs_age = time_hreadable(diff) messages += [f'Displaying data from [{recs_age}] ago.'] request_available = False time_remaining = None queue_pos = total_queue_position(profile, redis_connection) if queue_pos: messages += [f'Your position in queue: {queue_pos}'] messages += [Markup('Refresh the page later to see your recommendations - this process can take a while!')] elif diff is not None and diff < REFRESH_LIMIT: time_remaining = time_hreadable(int(REFRESH_LIMIT-diff)) else: messages += ["You can make a new request now, using the 'update' button on the right."] request_available = True if recommendations and not get_flashed_messages(): messages += [f'Tip: score more media on MAL for more accurate suggestions!'] return render_template( 'recommendations.html', recommendations=recommendations, profile=profile, messages=messages, profile_form=profile_form, heavy_load=display_captcha(redis_connection), update_request=update_request_form(), request_available=request_available, time_remaining=time_remaining, )