Permalink
Browse files

Merge branch 'master' of github.com:Pylons/pyramid_handlers

  • Loading branch information...
2 parents 93d26f4 + 66354f5 commit bd065fbf563f5a867e49448254b52820293a7fff @mcdonc mcdonc committed Aug 21, 2011
Showing with 17 additions and 45 deletions.
  1. +1 −1 CONTRIBUTORS.txt
  2. +11 −24 pyramid_handlers/__init__.py
  3. +5 −20 pyramid_handlers/tests.py
View
2 CONTRIBUTORS.txt
@@ -99,4 +99,4 @@ Contributors
- Chris McDonough, 2010/11/08
- Ben Bangert, 2011/1/23
-
+- Michael Merickel, 2011/8/21
View
35 pyramid_handlers/__init__.py
@@ -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
@@ -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,
@@ -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'):
@@ -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:
@@ -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,
View
25 pyramid_handlers/tests.py
@@ -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)

0 comments on commit bd065fb

Please sign in to comment.