Add event hook for attaching Rattail config
to new requests.
This commit is contained in:
parent
23ffcc5a78
commit
a958a7b285
4 changed files with 70 additions and 3 deletions
|
@ -36,6 +36,35 @@ from rattail.db.model import User
|
|||
from rattail.db.auth import has_permission
|
||||
|
||||
|
||||
def add_rattail_config_attribute_to_request(event):
|
||||
"""
|
||||
Add a ``rattail_config`` attribute to a request object.
|
||||
|
||||
This function is really just a matter of convenience, but it should help to
|
||||
make other code more terse (example below). It is designed to act as a
|
||||
subscriber to the Pyramid ``NewRequest`` event.
|
||||
|
||||
A global Rattail ``config`` should already be present within the Pyramid
|
||||
application registry's settings, which would normally be accessed via::
|
||||
|
||||
request.registry.settings['rattail_config']
|
||||
|
||||
This function merely "promotes" this config object so that it is more
|
||||
directly accessible, a la::
|
||||
|
||||
request.rattail_config
|
||||
|
||||
.. note::
|
||||
All this of course assumes that a Rattail ``config`` object *has* in
|
||||
fact already been placed in the application registry settings. If this
|
||||
is not the case, this function will do nothing.
|
||||
"""
|
||||
request = event.request
|
||||
rattail_config = request.registry.settings.get('rattail_config')
|
||||
if rattail_config:
|
||||
request.rattail_config = rattail_config
|
||||
|
||||
|
||||
def before_render(event):
|
||||
"""
|
||||
Adds goodies to the global template renderer context.
|
||||
|
@ -96,5 +125,6 @@ def context_found(event):
|
|||
|
||||
|
||||
def includeme(config):
|
||||
config.add_subscriber(add_rattail_config_attribute_to_request, 'pyramid.events.NewRequest')
|
||||
config.add_subscriber(before_render, 'pyramid.events.BeforeRender')
|
||||
config.add_subscriber(context_found, 'pyramid.events.ContextFound')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue