Skip to content
Myr

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

5 files+48 added−0 removed

modified:docs/api.rst+2 added
222 unmodified lines
223 by side.224225 This is a proxy. See :ref:`notes-on-proxies` for more information.226227.. autofunction:: has_request_context228229.. autofunction:: url_for230231.. function:: abort(code)232
211 unmodified lines
modified:flask/__init__.py+1 added
19 unmodified lines
20from .helpers import url_for, jsonify, json_available, flash, \21 send_file, send_from_directory, get_flashed_messages, \22 get_template_attribute, make_response23from .globals import current_app, g, request, session, _request_ctx_stack24from .ctx import has_request_context25from .module import Module26from .templating import render_template, render_template_string27from .session import Session28
7 unmodified lines
modified:flask/ctx.py+32 added
17 unmodified lines
18class _RequestGlobals(object):19 pass202122def 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.2728 ::2930 class User(db.Model):3132 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_addr3738 Alternatively you can also just test any of the context bound objects39 (such as :class:`request` or :class:`g` for truthness)::4041 class User(db.Model):4243 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_addr4849 .. versionadded:: 0.750 """51 return _request_ctx_stack.top is not None525354class _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 the
46 unmodified lines
modified:tests/flask_tests.py+12 added
87 unmodified lines
88 with app.test_request_context('/meh'):89 assert meh() == 'http://localhost/meh'90 assert flask._request_ctx_stack.top is None9192 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()103104 def test_manual_context_binding(self):105 app = flask.Flask(__name__)106 @app.route('/')107 def index():
1478 unmodified lines
modified:CHANGES+1 added
37 unmodified lines
38- 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`4344Version 0.6.145-------------46
159 unmodified lines