Implemented flask.has_request_context()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent f58c989 · Browse files · 5 files·+48 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+48 added−0 removed
- tests:+12 added−0 removed
- other:+36 added−0 removed
Provenance
- Commit
fbd4886- Tree
a6b263d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f58c989
5 files changed+48 added−0 removed
modified:docs/api.rst+2 added
222 unmodified lines223 by side.224 225 This is a proxy. See :ref:`notes-on-proxies` for more information.226 227+.. autofunction:: has_request_context228+229 .. autofunction:: url_for230 231 .. function:: abort(code)232 211 unmodified linesmodified:flask/__init__.py+1 added
19 unmodified lines20 from .helpers import url_for, jsonify, json_available, flash, \21 send_file, send_from_directory, get_flashed_messages, \22 get_template_attribute, make_response23 from .globals import current_app, g, request, session, _request_ctx_stack24+from .ctx import has_request_context25 from .module import Module26 from .templating import render_template, render_template_string27 from .session import Session28 7 unmodified linesmodified:flask/ctx.py+32 added
17 unmodified lines18 class _RequestGlobals(object):19 pass20 21 22+def has_request_context():23+ """If you have code that wants to test if a request context is there or24+ not this function can be used. For instance if you want to take advantage25+ of request information is it's available but fail silently if the request26+ object is unavailable.27+28+ ::29+30+ class User(db.Model):31+32+ def __init__(self, username, remote_addr=None):33+ self.username = username34+ if remote_addr is None and has_request_context():35+ remote_addr = request.remote_addr36+ self.remote_addr = remote_addr37+38+ Alternatively you can also just test any of the context bound objects39+ (such as :class:`request` or :class:`g` for truthness)::40+41+ class User(db.Model):42+43+ def __init__(self, username, remote_addr=None):44+ self.username = username45+ if remote_addr is None and request:46+ remote_addr = request.remote_addr47+ self.remote_addr = remote_addr48+49+ .. versionadded:: 0.750+ """51+ return _request_ctx_stack.top is not None52+53+54 class _RequestContext(object):55 """The request context contains all request relevant information. It is56 created at the beginning of the request and pushed to the57 `_request_ctx_stack` and removed at the end of it. It will create the46 unmodified linesmodified:tests/flask_tests.py+12 added
87 unmodified lines88 with app.test_request_context('/meh'):89 assert meh() == 'http://localhost/meh'90 assert flask._request_ctx_stack.top is None91 92+ def test_context_test(self):93+ app = flask.Flask(__name__)94+ assert not flask.request95+ assert not flask.has_request_context()96+ ctx = app.test_request_context()97+ ctx.push()98+ try:99+ assert flask.request100+ assert flask.has_request_context()101+ finally:102+ ctx.pop()103+104 def test_manual_context_binding(self):105 app = flask.Flask(__name__)106 @app.route('/')107 def index():1478 unmodified linesmodified:CHANGES+1 added
37 unmodified lines38 - Added `create_jinja_loader` to override the loader creation process.39 - Implemented a silent flag for `config.from_pyfile`.40 - Added `teardown_request` decorator, for functions that should run at the end41 of a request regardless of whether an exception occurred.42+- Implemented :func:`flask.has_request_context`43 44 Version 0.6.145 -------------46 159 unmodified lines