Skip to content

Commit

Permalink
add tests and documentation for using an optional context (to overrid…
Browse files Browse the repository at this point in the history
…e the context of the view in which the panel is called) as argument in calling a panel
  • Loading branch information
mrijken committed Jul 25, 2013
1 parent 0a804f1 commit d399653
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyramid_layout/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def add_panel(config, panel=None, name="", context=None,
name
The panel name.
The optional panel name, which defaults to an empty string.
context
Expand Down
7 changes: 6 additions & 1 deletion pyramid_layout/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ def render_panel(self, name=u'', *args, **kw):
"""
Renders the named panel, returning a `unicode` object that is the
rendered HTML for the panel. The panel is looked up using the current
context and given name. The panel is called passing in the current
context (or the context given as keyword argument, to override the
context in which the panel is called) and an optional given name
(which defaults to an empty string).
The panel is called passing in the current
context, request and any additional parameters passed into the
`render_panel` call. In case a panel isn't found, `None` is returned.
"""
context = kw.get('context', None) or self.context
if kw.get('context', None):
del kw['context']
request = self.request
adapters = request.registry.adapters
panel = adapters.lookup((providedBy(context),), IPanel, name=name)
Expand Down
33 changes: 33 additions & 0 deletions pyramid_layout/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,36 @@ def test_it_w_method(self, venusian):
.assert_called_once_with('MYMODULE')
config.add_panel.assert_called_once_with(panel=panel, attr='howdy',
name='', renderer=None, context=None, _info='FOOCODE')

def test_context_lookup(self):
from pyramid_layout.config import add_panel
from pyramid_layout.layout import LayoutManager
from pyramid.config import Configurator
from pyramid.testing import DummyRequest

request = DummyRequest()
config = Configurator()
request.config = config
request.registry = config.registry

def panel_int(context, request):
return 'panel int %s' % context
def panel_str(context, request):
return 'panel str %s' % context
def panel_str_noname(context, request):
return 'panel str noname %s' % context

add_panel(config, panel_int, name='howdy', context=int)

add_panel(config, panel_str, name='howdy', context=str)

add_panel(config, panel_str_noname)

config.commit()

self.assertEqual(LayoutManager(1, request).render_panel('howdy'), 'panel int 1')
self.assertEqual(LayoutManager(1, request).render_panel('howdy', context='1'), 'panel str 1')
self.assertEqual(LayoutManager('2', request).render_panel('howdy'), 'panel str 2')
self.assertEqual(LayoutManager('2', request).render_panel('howdy', context=2), 'panel int 2')
self.assertEqual(LayoutManager('2', request).render_panel(), 'panel str noname 2')

0 comments on commit d399653

Please sign in to comment.