mirror of https://github.com/pallets/flask.git
				
				
				
			Implemented flask.has_request_context()
This commit is contained in:
		
							parent
							
								
									f58c98904f
								
							
						
					
					
						commit
						fbd488678f
					
				
							
								
								
									
										1
									
								
								CHANGES
								
								
								
								
							
							
						
						
									
										1
									
								
								CHANGES
								
								
								
								
							|  | @ -39,6 +39,7 @@ Release date to be announced, codename to be selected | ||||||
| - Implemented a silent flag for `config.from_pyfile`. | - Implemented a silent flag for `config.from_pyfile`. | ||||||
| - Added `teardown_request` decorator, for functions that should run at the end | - Added `teardown_request` decorator, for functions that should run at the end | ||||||
|   of a request regardless of whether an exception occurred. |   of a request regardless of whether an exception occurred. | ||||||
|  | - Implemented :func:`flask.has_request_context` | ||||||
| 
 | 
 | ||||||
| Version 0.6.1 | Version 0.6.1 | ||||||
| ------------- | ------------- | ||||||
|  |  | ||||||
|  | @ -224,6 +224,8 @@ Useful Functions and Classes | ||||||
| 
 | 
 | ||||||
|    This is a proxy.  See :ref:`notes-on-proxies` for more information. |    This is a proxy.  See :ref:`notes-on-proxies` for more information. | ||||||
| 
 | 
 | ||||||
|  | .. autofunction:: has_request_context | ||||||
|  | 
 | ||||||
| .. autofunction:: url_for | .. autofunction:: url_for | ||||||
| 
 | 
 | ||||||
| .. function:: abort(code) | .. function:: abort(code) | ||||||
|  |  | ||||||
|  | @ -21,6 +21,7 @@ from .helpers import url_for, jsonify, json_available, flash, \ | ||||||
|     send_file, send_from_directory, get_flashed_messages, \ |     send_file, send_from_directory, get_flashed_messages, \ | ||||||
|     get_template_attribute, make_response |     get_template_attribute, make_response | ||||||
| from .globals import current_app, g, request, session, _request_ctx_stack | from .globals import current_app, g, request, session, _request_ctx_stack | ||||||
|  | from .ctx import has_request_context | ||||||
| from .module import Module | from .module import Module | ||||||
| from .templating import render_template, render_template_string | from .templating import render_template, render_template_string | ||||||
| from .session import Session | from .session import Session | ||||||
|  |  | ||||||
							
								
								
									
										32
									
								
								flask/ctx.py
								
								
								
								
							
							
						
						
									
										32
									
								
								flask/ctx.py
								
								
								
								
							|  | @ -19,6 +19,38 @@ class _RequestGlobals(object): | ||||||
|     pass |     pass | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def has_request_context(): | ||||||
|  |     """If you have code that wants to test if a request context is there or | ||||||
|  |     not this function can be used.  For instance if you want to take advantage | ||||||
|  |     of request information is it's available but fail silently if the request | ||||||
|  |     object is unavailable. | ||||||
|  | 
 | ||||||
|  |     :: | ||||||
|  | 
 | ||||||
|  |         class User(db.Model): | ||||||
|  | 
 | ||||||
|  |             def __init__(self, username, remote_addr=None): | ||||||
|  |                 self.username = username | ||||||
|  |                 if remote_addr is None and has_request_context(): | ||||||
|  |                     remote_addr = request.remote_addr | ||||||
|  |                 self.remote_addr = remote_addr | ||||||
|  | 
 | ||||||
|  |     Alternatively you can also just test any of the context bound objects | ||||||
|  |     (such as :class:`request` or :class:`g` for truthness):: | ||||||
|  | 
 | ||||||
|  |         class User(db.Model): | ||||||
|  | 
 | ||||||
|  |             def __init__(self, username, remote_addr=None): | ||||||
|  |                 self.username = username | ||||||
|  |                 if remote_addr is None and request: | ||||||
|  |                     remote_addr = request.remote_addr | ||||||
|  |                 self.remote_addr = remote_addr | ||||||
|  | 
 | ||||||
|  |     .. versionadded:: 0.7 | ||||||
|  |     """ | ||||||
|  |     return _request_ctx_stack.top is not None | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class _RequestContext(object): | class _RequestContext(object): | ||||||
|     """The request context contains all request relevant information.  It is |     """The request context contains all request relevant information.  It is | ||||||
|     created at the beginning of the request and pushed to the |     created at the beginning of the request and pushed to the | ||||||
|  |  | ||||||
|  | @ -89,6 +89,18 @@ class ContextTestCase(unittest.TestCase): | ||||||
|             assert meh() == 'http://localhost/meh' |             assert meh() == 'http://localhost/meh' | ||||||
|         assert flask._request_ctx_stack.top is None |         assert flask._request_ctx_stack.top is None | ||||||
| 
 | 
 | ||||||
|  |     def test_context_test(self): | ||||||
|  |         app = flask.Flask(__name__) | ||||||
|  |         assert not flask.request | ||||||
|  |         assert not flask.has_request_context() | ||||||
|  |         ctx = app.test_request_context() | ||||||
|  |         ctx.push() | ||||||
|  |         try: | ||||||
|  |             assert flask.request | ||||||
|  |             assert flask.has_request_context() | ||||||
|  |         finally: | ||||||
|  |             ctx.pop() | ||||||
|  | 
 | ||||||
|     def test_manual_context_binding(self): |     def test_manual_context_binding(self): | ||||||
|         app = flask.Flask(__name__) |         app = flask.Flask(__name__) | ||||||
|         @app.route('/') |         @app.route('/') | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue