Skip to content
Myr

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

4 files+20 added−2 removed

modified:flask/testsuite/appctx.py+10 added
63 unmodified lines
64 pass6566 self.assert_equal(cleanup_stuff, [None])6768 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')777879def suite():80 suite = unittest.TestSuite()81 suite.addTest(unittest.makeSuite(AppContextTestCase))82 return suite
modified:flask/app.py+6 added-1 removed
26 unmodified lines
27 locked_cached_property, _tojson_filter, _endpoint_from_view_func, \28 find_package29from .wrappers import Request, Response30from .config import ConfigAttribute, Config31from .ctx import RequestContext, AppContext31from .ctx import RequestContext, AppContext, _RequestGlobals32from .globals import _request_ctx_stack, request33from .sessions import SecureCookieSessionInterface34from .module import blueprint_is_module35from .templating import DispatchingJinjaLoader, Environment, \
111 unmodified lines
147 #: The class that is used for response objects. See148 #: :class:`~flask.Response` for more information.149 response_class = Response150151 #: The class that is used for the :data:`~flask.g` instance.152 #:153 #: .. versionadded:: 0.9154 request_globals_class = _RequestGlobals155156 #: 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 lines
modified:flask/ctx.py+2 added-1 removed
16 unmodified lines
17from .module import blueprint_is_module181920class _RequestGlobals(object):21 """A plain object."""22 pass232425def _push_app_if_necessary(app):
113 unmodified lines
139 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 = None146147 # indicator if the context was preserved. Next time another context
106 unmodified lines
modified:CHANGES+2 added
64 unmodified lines
65- 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.7172Version 0.8.173-------------74
294 unmodified lines