diff options
| -rw-r--r-- | CHANGES.txt | 4 | ||||
| -rw-r--r-- | docs/narr/urldispatch.rst | 11 | ||||
| -rw-r--r-- | repoze/bfg/tests/test_zcml.py | 29 | ||||
| -rw-r--r-- | repoze/bfg/zcml.py | 11 |
4 files changed, 52 insertions, 3 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 46c6e52ea..c4d286c4d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -10,6 +10,10 @@ Features the respective interface type imported from ``repoze.bfg.interfaces``. +- The ``route`` ZCML directive now accepts ``request_type`` as an + alias for its ``condition_method`` argument for symmetry with the + ``view`` directive. + Removals -------- diff --git a/docs/narr/urldispatch.rst b/docs/narr/urldispatch.rst index d43a344f2..f19faa031 100644 --- a/docs/narr/urldispatch.rst +++ b/docs/narr/urldispatch.rst @@ -122,10 +122,17 @@ filter A Python dotted-path name to a Routes `filter function <http://routes.groovie.org/manual.html#filter-functions>`_. -condition_method +request_type The name of the HTTP method used as the Routes `condition method - <http://routes.groovie.org/manual.html#conditions>`_. + <http://routes.groovie.org/manual.html#conditions>`_. A string, + e.g. ``GET`` or ``POST``. Note that :term:`interface` references + don't work here as they do in the ``view`` decorator or ``bfg_view`` + ZCML directive. Only strings representing an HTTP method will work. + +condition_method + + An alias for the ``request_type`` attribute. condition_subdomain diff --git a/repoze/bfg/tests/test_zcml.py b/repoze/bfg/tests/test_zcml.py index 0b24071e2..e35944add 100644 --- a/repoze/bfg/tests/test_zcml.py +++ b/repoze/bfg/tests/test_zcml.py @@ -415,6 +415,34 @@ class TestConnectRouteFunction(unittest.TestCase): 'conditions':{'method':'GET'} }) + def test_request_type(self): + mapper = self._registerRoutesMapper() + directive = DummyRouteDirective(static=True, explicit=True, + request_type='GET') + self._callFUT(directive) + self.assertEqual(len(mapper.connections), 1) + self.assertEqual(mapper.connections[0][1], + {'requirements': {}, + '_static':True, + '_explicit':True, + 'conditions':{'method':'GET'} + }) + + def test_condition_method_and_request_type(self): + mapper = self._registerRoutesMapper() + directive = DummyRouteDirective(static=True, explicit=True, + request_type='GET', + condition_method='POST') + self._callFUT(directive) + self.assertEqual(len(mapper.connections), 1) + self.assertEqual(mapper.connections[0][1], + {'requirements': {}, + '_static':True, + '_explicit':True, + 'conditions':{'method':'POST'} + }) + + def test_subdomains(self): mapper = self._registerRoutesMapper() directive = DummyRouteDirective(name='name', @@ -693,6 +721,7 @@ class DummyRouteDirective: parent_member_name = None parent_collection_name = None condition_method = None + request_type = None condition_subdomain = None condition_function = None subdomains = None diff --git a/repoze/bfg/zcml.py b/repoze/bfg/zcml.py index 0f9d4a22b..bd12e926c 100644 --- a/repoze/bfg/zcml.py +++ b/repoze/bfg/zcml.py @@ -219,6 +219,7 @@ class IRouteDirective(Interface): absolute = Bool(title=u'absolute', required=False) member_name = TextLine(title=u'member_name', required=False) collection_name = TextLine(title=u'collection_name', required=False) + request_type = TextLine(title=u'request_type', required=False) condition_method = TextLine(title=u'condition_method', required=False) condition_subdomain = TextLine(title=u'condition_subdomain', required=False) condition_function = GlobalObject(title=u'condition_function', @@ -258,6 +259,11 @@ def connect_route(directive): 'collection_name':directive.parent_collection_name, } conditions = {} + + # request_type and condition_method are aliases; condition_method + # "wins" + if directive.request_type: + conditions['method'] = directive.request_type if directive.condition_method: conditions['method'] = directive.condition_method if directive.condition_subdomain: @@ -288,6 +294,7 @@ class Route(zope.configuration.config.GroupingContextDecorator): member_name = None collection_name = None condition_method = None + request_type = None condition_subdomain = None condition_function = None parent_member_name = None @@ -320,9 +327,11 @@ class Route(zope.configuration.config.GroupingContextDecorator): view(self.context, self.permission, IRoutesContext, self.view, self.name, None) + method = self.condition_method or self.request_type + self.context.action( discriminator = ('route', self.path, repr(self.requirements), - self.condition_method, self.condition_subdomain, + method, self.condition_subdomain, self.condition_function, self.subdomains), callable = connect_route, args = (self,), |
