Skip to content

Commit

Permalink
Merge pull request #2687 from sontek/support_classes_in_proutes
Browse files Browse the repository at this point in the history
This shows the correct view when a class and `attr` is involved.
  • Loading branch information
stevepiercy authored Jul 8, 2016
2 parents 2b8c7d5 + d9dc88d commit 474d4a8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pyramid/scripts/proutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,15 @@ def get_route_data(route, registry):
request_method = view.get('request_methods')

if request_method is not None:
view_callable = view['callable']
view_module = _get_view_module(view_callable)
if view.get('attr') is not None:
view_callable = getattr(view['callable'], view['attr'])
view_module = '%s.%s' % (
_get_view_module(view['callable']),
view['attr']
)
else:
view_callable = view['callable']
view_module = _get_view_module(view_callable)

if view_module not in view_request_methods:
view_request_methods[view_module] = []
Expand Down
2 changes: 2 additions & 0 deletions pyramid/tests/test_scripts/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class DummyView(object):
def __init__(self, **attrs):
self.__request_attrs__ = attrs

def view(context, request): pass

from zope.interface import implementer
from pyramid.interfaces import IMultiView
@implementer(IMultiView)
Expand Down
27 changes: 27 additions & 0 deletions pyramid/tests/test_scripts/test_proutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,33 @@ class IMyRoute(Interface):
'pyramid.tests.test_scripts.test_proutes.view']
)

def test_class_view(self):
from pyramid.renderers import null_renderer as nr

config = self._makeConfig(autocommit=True)
config.add_route('foo', '/a/b')
config.add_view(
route_name='foo',
view=dummy.DummyView,
attr='view',
renderer=nr,
request_method='POST'
)

command = self._makeOne()
L = []
command.out = L.append
command.bootstrap = (dummy.DummyBootstrap(registry=config.registry),)
result = command.run()
self.assertEqual(result, 0)
self.assertEqual(len(L), 3)
compare_to = L[-1].split()
expected = [
'foo', '/a/b',
'pyramid.tests.test_scripts.dummy.DummyView.view', 'POST'
]
self.assertEqual(compare_to, expected)

def test_single_route_one_view_registered_with_factory(self):
from zope.interface import Interface
from pyramid.interfaces import IRouteRequest
Expand Down

0 comments on commit 474d4a8

Please sign in to comment.