Skip to content

Commit

Permalink
Merge pull request #284 from mmerickel/fix-subscriber-order
Browse files Browse the repository at this point in the history
fix before render subscriber order
  • Loading branch information
mmerickel authored Apr 21, 2017
2 parents 1056a36 + bbb2eab commit d0d4d35
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Unreleased
console logging during development, partitioned by feature, and silence it for
deployment while still leaving the logging lines activated.

- The toolbar registers a ``BeforeRender`` subscriber in your application to
monitor the rendering of templates. Previously it was possible that the
toolbar would miss rendering information because of the order in which the
subscribers were registered. The toolbar now waits until the application
is created and then appends a new subscriber that encapsulates the
your application's ``BeforeRender`` subscribers.
See https://github.com/Pylons/pyramid_debugtoolbar/pull/284

3.0.5 (2016-11-1)
-----------------
Expand Down
20 changes: 16 additions & 4 deletions pyramid_debugtoolbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ def set_request_authorization_callback(config, callback):
"""
config.registry.registerUtility(callback, IRequestAuthorization)

def inject_toolbar(event):
app = event.app
registry = app.registry

# inject the BeforeRender subscriber after the application is created
# and all other subscribers are registered in hopes that this will be
# the last subscriber in the chain and will be able to see the effects
# of all previous subscribers on the event
config = Configurator(registry=registry, introspection=False)
config.add_subscriber(
'pyramid_debugtoolbar.toolbar.beforerender_subscriber',
'pyramid.events.BeforeRender',
)
config.commit()

def includeme(config):
""" Activate the debug toolbar; usually called via
``config.include('pyramid_debugtoolbar')`` instead of being invoked
Expand All @@ -136,10 +151,7 @@ def includeme(config):
config.registry.registerUtility(application, IToolbarWSGIApp)

config.add_tween('pyramid_debugtoolbar.toolbar_tween_factory')
config.add_subscriber(
'pyramid_debugtoolbar.toolbar.beforerender_subscriber',
'pyramid.events.BeforeRender',
)
config.add_subscriber(inject_toolbar, 'pyramid.events.ApplicationCreated')
config.add_directive('set_debugtoolbar_request_authorization',
set_request_authorization_callback)

Expand Down
32 changes: 32 additions & 0 deletions tests/test_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ def test_with_request(self):
self._callFUT(event)
self.assertTrue(event['processed'])

class Test_beforerender_subscriber_order(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()

def tearDown(self):
testing.tearDown()

def test_it(self):
panels = []
def panel_factory(request):
panel = DummyPanel(request)
panels.append(panel)
return panel
def includeme(config):
config.add_debugtoolbar_panel(panel_factory)
self.config.add_settings({'debugtoolbar.includes': [includeme]})
self.config.include('pyramid_debugtoolbar')
def dummy_subscriber(event):
event['foo'] = 'bar'
self.config.add_subscriber(
dummy_subscriber, 'pyramid.events.BeforeRender')
# add the toolbar after the subscriber
self.config.add_view(lambda r: {}, renderer='json')
app = self.config.make_wsgi_app()
request = Request.blank('/')
request.remote_addr = '127.0.0.1'
request.get_response(app)
self.assertEqual(len(panels), 1)
event = panels[0].event
self.assertEqual(event['foo'], 'bar')


class Test_toolbar_tween_factory(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -411,6 +442,7 @@ def wrap_handler(self, handler):

def process_beforerender(self, event):
event['processed'] = True
self.event = event.copy()

class DummyPanelWithContent(DummyPanel):
has_content = True
Expand Down

0 comments on commit d0d4d35

Please sign in to comment.