diff options
| author | Chris McDonough <chrism@plope.com> | 2011-09-05 17:47:19 -0400 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2011-09-05 17:47:19 -0400 |
| commit | e77f6b09f8d91fd792851e3d384b86663bb6384a (patch) | |
| tree | 779e2d84d720107f046e45f422abf883d17c9203 | |
| parent | 888236b7b50b2469e8ba8fb2de6e1d16007a6276 (diff) | |
| parent | c81dbdc8281ad5a64a9c53bdb3cf621f44e0fa5d (diff) | |
| download | pyramid-e77f6b09f8d91fd792851e3d384b86663bb6384a.tar.gz pyramid-e77f6b09f8d91fd792851e3d384b86663bb6384a.tar.bz2 pyramid-e77f6b09f8d91fd792851e3d384b86663bb6384a.zip | |
Merge branch 'master' of github.com:Pylons/pyramid
| -rw-r--r-- | pyramid/config/util.py | 44 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_util.py | 15 |
2 files changed, 37 insertions, 22 deletions
diff --git a/pyramid/config/util.py b/pyramid/config/util.py index 298d73cfd..7ce9adc4f 100644 --- a/pyramid/config/util.py +++ b/pyramid/config/util.py @@ -203,27 +203,6 @@ def make_predicates(xhr=None, request_method=None, path_info=None, predicates.append(match_param_predicate) h.update('match_param:%r' % match_param) - if traverse is not None: - # ``traverse`` can only be used as a *route* "predicate"; it - # adds 'traverse' to the matchdict if it's specified in the - # routing args. This causes the ResourceTreeTraverser to use - # the resolved traverse pattern as the traversal path. - from pyramid.urldispatch import _compile_route - _, tgenerate = _compile_route(traverse) - def traverse_predicate(context, request): - if 'traverse' in context: - return True - m = context['match'] - tvalue = tgenerate(m) - m['traverse'] = traversal_path(tvalue) - return True - # This isn't actually a predicate, it's just a infodict - # modifier that injects ``traverse`` into the matchdict. As a - # result, the ``traverse_predicate`` function above always - # returns True, and we don't need to update the hash or attach - # a weight to it - predicates.append(traverse_predicate) - if custom: for num, predicate in enumerate(custom): if getattr(predicate, '__text__', None) is None: @@ -234,7 +213,7 @@ def make_predicates(xhr=None, request_method=None, path_info=None, # if this happens the predicate is probably a classmethod if hasattr(predicate, '__func__'): predicate.__func__.__text__ = text - else: # # pragma: no cover ; 2.5 doesn't have __func__ + else: # pragma: no cover ; 2.5 doesn't have __func__ predicate.im_func.__text__ = text predicates.append(predicate) # using hash() here rather than id() is intentional: we @@ -246,6 +225,27 @@ def make_predicates(xhr=None, request_method=None, path_info=None, h.update('custom%s:%r' % (num, hash(predicate))) weights.append(1 << 10) + if traverse is not None: + # ``traverse`` can only be used as a *route* "predicate"; it + # adds 'traverse' to the matchdict if it's specified in the + # routing args. This causes the ResourceTreeTraverser to use + # the resolved traverse pattern as the traversal path. + from pyramid.urldispatch import _compile_route + _, tgenerate = _compile_route(traverse) + def traverse_predicate(context, request): + if 'traverse' in context: + return True + m = context['match'] + tvalue = tgenerate(m) + m['traverse'] = traversal_path(tvalue) + return True + # This isn't actually a predicate, it's just a infodict + # modifier that injects ``traverse`` into the matchdict. As a + # result, the ``traverse_predicate`` function above always + # returns True, and we don't need to update the hash or attach + # a weight to it + predicates.append(traverse_predicate) + score = 0 for bit in weights: score = score | bit diff --git a/pyramid/tests/test_config/test_util.py b/pyramid/tests/test_config/test_util.py index 2fcef184b..bc7cf0a82 100644 --- a/pyramid/tests/test_config/test_util.py +++ b/pyramid/tests/test_config/test_util.py @@ -227,6 +227,21 @@ class Test__make_predicates(unittest.TestCase): self.assertEqual(info, {'match': {'a':'a', 'b':'b', 'traverse':('1', 'a', 'b')}}) + def test_custom_predicates_can_affect_traversal(self): + def custom(info, request): + m = info['match'] + m['dummy'] = 'foo' + return True + _, predicates, _ = self._callFUT(custom=(custom,), + traverse='/1/:dummy/:a') + self.assertEqual(len(predicates), 2) + info = {'match':{'a':'a'}} + request = DummyRequest() + self.assertTrue(all([p(info, request) for p in predicates])) + self.assertEqual(info, {'match': + {'a':'a', 'dummy':'foo', + 'traverse':('1', 'foo', 'a')}}) + def test_predicate_text_is_correct(self): _, predicates, _ = self._callFUT( xhr='xhr', |
