Skip to content

Commit

Permalink
Update view_handler to properly register the explicit action under the
Browse files Browse the repository at this point in the history
right circumstances.
  • Loading branch information
bbangert committed Mar 23, 2011
1 parent 6a4c0c1 commit 45a7367
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pyramid_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ def locate_view_by_name(config, handler, route_name, action_decorator, name):
method_name = name
if method_name is None:
method_name = '__call__'

# Scan the controller for any other methods with this action name
for attr, method in inspect.getmembers(handler, inspect.ismethod):
for meth_name, method in inspect.getmembers(handler, inspect.ismethod):
configs = getattr(method, '__exposed__', [{}])
for expose_config in configs:
# Don't re-register the same view if this method name is
# the action name
if attr == name:
if meth_name == name:
continue
# We only reg a view if the name matches the action
if expose_config.get('name') != method_name:
Expand All @@ -139,17 +139,25 @@ def locate_view_by_name(config, handler, route_name, action_decorator, name):
# so we copy each
view_args = expose_config.copy()
del view_args['name']
config.add_view(view=handler, attr=attr,
config.add_view(view=handler, attr=meth_name,
route_name=route_name,
decorator=action_decorator, **view_args)

# Now register the method itself
method = getattr(handler, method_name, None)
if method:
configs = getattr(method, '__exposed__', [{}])
configs = getattr(method, '__exposed__', [])
view_regged = False
for expose_config in configs:
if 'name' in expose_config and expose_config['name'] != name:
continue
view_regged = True
config.add_view(view=handler, attr=name, route_name=route_name,
decorator=action_decorator, **expose_config)
if not view_regged:
config.add_view(view=handler, attr=name, route_name=route_name,
decorator=action_decorator)



class ActionPredicate(object):
Expand Down
16 changes: 16 additions & 0 deletions pyramid_handlers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ def one(self): pass
config.add_handler('name', None, DummyHandler, action='two')
self.assertEqual(len(views), 0)

def test_add_handler_explicit_action_and_extra_exposed(self):
import pyramid
config = self._makeOne()
config.add_route('name', ':def')
views = []
def dummy_add_view(**kw): views.append(kw)
class DummyHandler(object):
def two(self): pass
two.__exposed__ = [{'name':'one'}]
config.add_view = dummy_add_view # shouldn't be called
config.add_route = None # shouldn't be called
config.add_handler('name', None, DummyHandler, action='two')
self.assertEqual(len(views), 1)
view = views[0]
self.assertEqual(view['view'], DummyHandler)

def _assertRoute(self, config, name, path, num_predicates=0):
from pyramid.interfaces import IRoutesMapper
mapper = config.registry.getUtility(IRoutesMapper)
Expand Down

0 comments on commit 45a7367

Please sign in to comment.