Skip to content

Commit fbd4886

Browse files
committed
Implemented flask.has_request_context()
1 parent f58c989 commit fbd4886

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Release date to be announced, codename to be selected
3939
- Implemented a silent flag for `config.from_pyfile`.
4040
- Added `teardown_request` decorator, for functions that should run at the end
4141
of a request regardless of whether an exception occurred.
42+
- Implemented :func:`flask.has_request_context`
4243

4344
Version 0.6.1
4445
-------------

docs/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ Useful Functions and Classes
224224

225225
This is a proxy. See :ref:`notes-on-proxies` for more information.
226226

227+
.. autofunction:: has_request_context
228+
227229
.. autofunction:: url_for
228230

229231
.. function:: abort(code)

flask/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
send_file, send_from_directory, get_flashed_messages, \
2222
get_template_attribute, make_response
2323
from .globals import current_app, g, request, session, _request_ctx_stack
24+
from .ctx import has_request_context
2425
from .module import Module
2526
from .templating import render_template, render_template_string
2627
from .session import Session

flask/ctx.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ class _RequestGlobals(object):
1919
pass
2020

2121

22+
def has_request_context():
23+
"""If you have code that wants to test if a request context is there or
24+
not this function can be used. For instance if you want to take advantage
25+
of request information is it's available but fail silently if the request
26+
object is unavailable.
27+
28+
::
29+
30+
class User(db.Model):
31+
32+
def __init__(self, username, remote_addr=None):
33+
self.username = username
34+
if remote_addr is None and has_request_context():
35+
remote_addr = request.remote_addr
36+
self.remote_addr = remote_addr
37+
38+
Alternatively you can also just test any of the context bound objects
39+
(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 = username
45+
if remote_addr is None and request:
46+
remote_addr = request.remote_addr
47+
self.remote_addr = remote_addr
48+
49+
.. versionadded:: 0.7
50+
"""
51+
return _request_ctx_stack.top is not None
52+
53+
2254
class _RequestContext(object):
2355
"""The request context contains all request relevant information. It is
2456
created at the beginning of the request and pushed to the

tests/flask_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ def meh():
8989
assert meh() == 'http://localhost/meh'
9090
assert flask._request_ctx_stack.top is None
9191

92+
def test_context_test(self):
93+
app = flask.Flask(__name__)
94+
assert not flask.request
95+
assert not flask.has_request_context()
96+
ctx = app.test_request_context()
97+
ctx.push()
98+
try:
99+
assert flask.request
100+
assert flask.has_request_context()
101+
finally:
102+
ctx.pop()
103+
92104
def test_manual_context_binding(self):
93105
app = flask.Flask(__name__)
94106
@app.route('/')

0 commit comments

Comments
 (0)