diff options
| author | John Anderson <sontek@gmail.com> | 2014-12-14 21:09:48 -0800 |
|---|---|---|
| committer | John Anderson <sontek@gmail.com> | 2014-12-14 21:09:48 -0800 |
| commit | 228a5e5b3806c07c4d568ec491c4f83be5facbb1 (patch) | |
| tree | 8a2dda31463bb79e09f7c3c324ed37e0ea8dc67d | |
| parent | 59ee9f5fe96f133ff3582dbf34ea7da43ef39029 (diff) | |
| download | pyramid-228a5e5b3806c07c4d568ec491c4f83be5facbb1.tar.gz pyramid-228a5e5b3806c07c4d568ec491c4f83be5facbb1.tar.bz2 pyramid-228a5e5b3806c07c4d568ec491c4f83be5facbb1.zip | |
Added support for `not_` request_method checks
| -rw-r--r-- | pyramid/scripts/proutes.py | 28 | ||||
| -rw-r--r-- | pyramid/tests/test_scripts/test_proutes.py | 60 |
2 files changed, 85 insertions, 3 deletions
diff --git a/pyramid/scripts/proutes.py b/pyramid/scripts/proutes.py index d68f35cef..5d860b47d 100644 --- a/pyramid/scripts/proutes.py +++ b/pyramid/scripts/proutes.py @@ -9,6 +9,7 @@ from pyramid.interfaces import ( IViewClassifier, IView, ) +from pyramid.config import not_ from pyramid.scripts.common import parse_vars from pyramid.static import static_view @@ -44,6 +45,19 @@ def _get_print_format(max_name, max_pattern, max_view, max_method): def _get_request_methods(route_request_methods, view_request_methods): + excludes = set() + + if route_request_methods: + route_request_methods = set(route_request_methods) + + if view_request_methods: + view_request_methods = set(view_request_methods) + + for method in view_request_methods.copy(): + if method.startswith('!'): + view_request_methods.remove(method) + excludes.add(method[1:]) + has_route_methods = route_request_methods is not None has_view_methods = len(view_request_methods) > 0 has_methods = has_route_methods or has_view_methods @@ -55,14 +69,20 @@ def _get_request_methods(route_request_methods, view_request_methods): elif has_route_methods is True and has_view_methods is False: request_methods = route_request_methods else: - request_methods = set(route_request_methods).intersection( - set(view_request_methods) + request_methods = route_request_methods.intersection( + view_request_methods ) + request_methods = set(request_methods).difference(excludes) + if has_methods and not request_methods: request_methods = '<route mismatch>' elif request_methods: - request_methods = ','.join(request_methods) + if excludes and request_methods == set([ANY_KEY]): + for exclude in excludes: + request_methods.add('!%s' % exclude) + + request_methods = ','.join(sorted(request_methods)) return request_methods @@ -161,6 +181,8 @@ def get_route_data(route, registry): if isinstance(request_method, string_types): request_method = (request_method,) + elif isinstance(request_method, not_): + request_method = ('!%s' % request_method.value,) view_request_methods[view_module].extend(request_method) else: diff --git a/pyramid/tests/test_scripts/test_proutes.py b/pyramid/tests/test_scripts/test_proutes.py index 0713f4ac9..f7a85bd1c 100644 --- a/pyramid/tests/test_scripts/test_proutes.py +++ b/pyramid/tests/test_scripts/test_proutes.py @@ -471,6 +471,66 @@ class TestPRoutesCommand(unittest.TestCase): ] self.assertEqual(compare_to, expected) + def test_route_is_get_view_request_method_not_post(self): + from pyramid.renderers import null_renderer as nr + from pyramid.config import not_ + + def view1(context, request): return 'view1' + + config = self._makeConfig(autocommit=True) + config.add_route('foo', '/a/b', request_method='GET') + config.add_view( + route_name='foo', + view=view1, + renderer=nr, + request_method=not_('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.test_proutes.view1', + 'GET' + ] + self.assertEqual(compare_to, expected) + + def test_view_request_method_not_post(self): + from pyramid.renderers import null_renderer as nr + from pyramid.config import not_ + + def view1(context, request): return 'view1' + + config = self._makeConfig(autocommit=True) + config.add_route('foo', '/a/b') + config.add_view( + route_name='foo', + view=view1, + renderer=nr, + request_method=not_('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.test_proutes.view1', + '!POST,*' + ] + self.assertEqual(compare_to, expected) + class Test_main(unittest.TestCase): def _callFUT(self, argv): from pyramid.scripts.proutes import main |
