Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Pylons/pyramid_handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdonc committed Aug 21, 2011
2 parents 93d26f4 + 66354f5 commit bd065fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ Contributors

- Chris McDonough, 2010/11/08
- Ben Bangert, 2011/1/23

- Michael Merickel, 2011/8/21
35 changes: 11 additions & 24 deletions pyramid_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from pyramid.exceptions import ConfigurationError

action_re = re.compile(r'''({action}|:action)''')

def add_handler(self, route_name, pattern, handler, action=None, **kw):
""" Add a Pylons-style view handler. This function adds a
route and some number of views based on a handler object
Expand All @@ -19,9 +21,7 @@ def add_handler(self, route_name, pattern, handler, action=None, **kw):
``pattern`` is the matching pattern,
e.g. ``'/blog/{action}'``. ``pattern`` may be ``None``, in
which case the pattern of an existing route named the same as
``route_name`` is used. If ``pattern`` is ``None`` and no
route named ``route_name`` exists, a ``ConfigurationError`` is
raised.
``route_name`` is used.
``handler`` is a dotted name of (or direct reference to) a
Python handler class,
Expand All @@ -39,28 +39,17 @@ def add_handler(self, route_name, pattern, handler, action=None, **kw):
Any extra keyword arguments are passed along to ``add_route``.
See :ref:`views_chapter` for more explanatory documentation.
This method returns the result of add_route."""
handler = self.maybe_dotted(handler)
See :ref:`views_chapter` for more explanatory documentation."""
if pattern is None:
raise ConfigurationError('As of version 0.3 pattern cannot be None')

default_view_args = {
'permission': kw.pop('view_permission', kw.pop('permission', None))
}

if pattern is not None:
route = self.add_route(route_name, pattern, **kw)
else:
mapper = self.get_routes_mapper()
route = mapper.get_route(route_name)
if route is None:
raise ConfigurationError(
'The "pattern" parameter may only be "None" when a route '
'with the route_name argument was previously registered. '
'No such route named %r exists' % route_name)

pattern = route.pattern
self.add_route(route_name, pattern, **kw)

handler = self.maybe_dotted(handler)
action_decorator = getattr(handler, '__action_decorator__', None)
if action_decorator is not None:
if hasattr(action_decorator, 'im_self'):
Expand All @@ -75,14 +64,13 @@ def add_handler(self, route_name, pattern, handler, action=None, **kw):
'staticmethod, classmethod, function, or an instance '
'which is a callable')

path_has_action = ':action' in pattern or '{action}' in pattern

if action and path_has_action:
action_pattern = action_re.search(pattern)
if action and action_pattern:
raise ConfigurationError(
'action= (%r) disallowed when an action is in the route '
'path %r' % (action, pattern))

if path_has_action:
if action_pattern:
scan_handler(self, handler, route_name, action_decorator,
**default_view_args)
else:
Expand All @@ -94,7 +82,6 @@ def add_handler(self, route_name, pattern, handler, action=None, **kw):
name=action,
**default_view_args
)
return route


def scan_handler(config, handler, route_name, action_decorator,
Expand Down
25 changes: 5 additions & 20 deletions pyramid_handlers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,46 +269,31 @@ def test_add_handler_pattern_None_no_previous_route(self):
'name', None, 'pyramid')

def test_add_handler_pattern_None_with_previous_route(self):
import pyramid
from pyramid.exceptions import ConfigurationError
config = self._makeOne()
config.add_route('name', ':def')
views = []
def dummy_add_view(**kw):
views.append(kw)
class DummyHandler(object):
def one(self): pass
config.add_view = dummy_add_view
config.add_route = None # shouldn't be called
config.add_handler('name', None, DummyHandler)
self.assertEqual(len(views), 1)
view = views[0]
self.assertEqual(view['view'], DummyHandler)
self.assertRaises(ConfigurationError, config.add_handler,
'name', None, 'pyramid')

def test_add_handler_explicit_action_lacking(self):
import pyramid
config = self._makeOne()
config.add_route('name', ':def')
views = []
def dummy_add_view(**kw): views.append(kw)
class DummyHandler(object):
def one(self): pass
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')
config.add_handler('name', ':def', 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')
config.add_handler('name', ':def', DummyHandler, action='two')
self.assertEqual(len(views), 1)
view = views[0]
self.assertEqual(view['view'], DummyHandler)
Expand Down

0 comments on commit bd065fb

Please sign in to comment.