Add Flask.request_globals_class to customize g.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Requested by toothr on #pocoo.
authored by Ron DuPlain · committed by Ron DuPlain · parent 26da6a5 · Browse files · 4 files·+20 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+20 added−2 removed
- other:+20 added−2 removed
Provenance
- Commit
33bae1a- Tree
1da5c51- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
26da6a5
4 files changed+20 added−2 removed
modified:flask/testsuite/appctx.py+10 added
63 unmodified lines64 pass65 66 self.assert_equal(cleanup_stuff, [None])67 68+ def test_custom_request_globals_class(self):69+ class CustomRequestGlobals(object):70+ def __init__(self):71+ self.spam = 'eggs'72+ app = flask.Flask(__name__)73+ app.request_globals_class = CustomRequestGlobals74+ with app.test_request_context():75+ self.assert_equal(76+ flask.render_template_string('{{ g.spam }}'), 'eggs')77 78+79 def suite():80 suite = unittest.TestSuite()81 suite.addTest(unittest.makeSuite(AppContextTestCase))82 return suitemodified:flask/app.py+6 added-1 removed
26 unmodified lines27 locked_cached_property, _tojson_filter, _endpoint_from_view_func, \28 find_package29 from .wrappers import Request, Response30 from .config import ConfigAttribute, Config31-from .ctx import RequestContext, AppContext31+from .ctx import RequestContext, AppContext, _RequestGlobals32 from .globals import _request_ctx_stack, request33 from .sessions import SecureCookieSessionInterface34 from .module import blueprint_is_module35 from .templating import DispatchingJinjaLoader, Environment, \111 unmodified lines147 #: The class that is used for response objects. See148 #: :class:`~flask.Response` for more information.149 response_class = Response150 151+ #: The class that is used for the :data:`~flask.g` instance.152+ #:153+ #: .. versionadded:: 0.9154+ request_globals_class = _RequestGlobals155+156 #: The debug flag. Set this to `True` to enable debugging of the157 #: application. In debug mode the debugger will kick in when an unhandled158 #: exception ocurrs and the integrated server will automatically reload159 #: the application if changes in the code are detected.1525 unmodified linesmodified:flask/ctx.py+2 added-1 removed
16 unmodified lines17 from .module import blueprint_is_module18 19 20 class _RequestGlobals(object):21+ """A plain object."""22 pass23 24 25 def _push_app_if_necessary(app):113 unmodified lines139 def __init__(self, app, environ):140 self.app = app141 self.request = app.request_class(environ)142 self.url_adapter = app.create_url_adapter(self.request)142- self.g = _RequestGlobals()143+ self.g = app.request_globals_class()144 self.flashes = None145 self.session = None146 147 # indicator if the context was preserved. Next time another context106 unmodified linesmodified:CHANGES+2 added
64 unmodified lines65 - Fixed an assumption in sessions implementation which could break message66 flashing on sessions implementations which use external storage.67 - Changed the behavior of tuple return values from functions. They are no68 longer arguments to the response object, they now have a defined meaning.69+- Added :attr:`flask.Flask.request_globals_class` to allow a specific class to70+ be used on creation of the :data:`~flask.g` instance of each request.71 72 Version 0.8.173 -------------74 294 unmodified lines