Browse Source

Add Flask.request_globals_class to customize g.

Requested by toothr on #pocoo.
pull/502/head
Ron DuPlain 13 years ago
parent
commit
33bae1a8dc
  1. 2
      CHANGES
  2. 7
      flask/app.py
  3. 3
      flask/ctx.py
  4. 10
      flask/testsuite/appctx.py

2
CHANGES

@ -66,6 +66,8 @@ Relase date to be decided, codename to be chosen.
flashing on sessions implementations which use external storage.
- Changed the behavior of tuple return values from functions. They are no
longer arguments to the response object, they now have a defined meaning.
- Added :attr:`flask.Flask.request_globals_class` to allow a specific class to
be used on creation of the :data:`~flask.g` instance of each request.
Version 0.8.1
-------------

7
flask/app.py

@ -28,7 +28,7 @@ from .helpers import _PackageBoundObject, url_for, get_flashed_messages, \
find_package
from .wrappers import Request, Response
from .config import ConfigAttribute, Config
from .ctx import RequestContext, AppContext
from .ctx import RequestContext, AppContext, _RequestGlobals
from .globals import _request_ctx_stack, request
from .sessions import SecureCookieSessionInterface
from .module import blueprint_is_module
@ -148,6 +148,11 @@ class Flask(_PackageBoundObject):
#: :class:`~flask.Response` for more information.
response_class = Response
#: The class that is used for the :data:`~flask.g` instance.
#:
#: .. versionadded:: 0.9
request_globals_class = _RequestGlobals
#: The debug flag. Set this to `True` to enable debugging of the
#: application. In debug mode the debugger will kick in when an unhandled
#: exception ocurrs and the integrated server will automatically reload

3
flask/ctx.py

@ -18,6 +18,7 @@ from .module import blueprint_is_module
class _RequestGlobals(object):
"""A plain object."""
pass
@ -139,7 +140,7 @@ class RequestContext(object):
self.app = app
self.request = app.request_class(environ)
self.url_adapter = app.create_url_adapter(self.request)
self.g = _RequestGlobals()
self.g = app.request_globals_class()
self.flashes = None
self.session = None

10
flask/testsuite/appctx.py

@ -65,6 +65,16 @@ class AppContextTestCase(FlaskTestCase):
self.assert_equal(cleanup_stuff, [None])
def test_custom_request_globals_class(self):
class CustomRequestGlobals(object):
def __init__(self):
self.spam = 'eggs'
app = flask.Flask(__name__)
app.request_globals_class = CustomRequestGlobals
with app.test_request_context():
self.assert_equal(
flask.render_template_string('{{ g.spam }}'), 'eggs')
def suite():
suite = unittest.TestSuite()

Loading…
Cancel
Save