Skip to content
Browse files

allow request_param predicate to accept a tuple of constraints

  • Loading branch information...
1 parent 4a81d6d commit 3fb934e8d600018800932fb66d001f5bacdac2c8 @mmerickel committed
Showing with 41 additions and 16 deletions.
  1. +23 −16 pyramid/config/predicates.py
  2. +18 −0 pyramid/tests/test_config/test_predicates.py
View
39 pyramid/config/predicates.py
@@ -64,28 +64,35 @@ def __call__(self, context, request):
class RequestParamPredicate(object):
def __init__(self, val, config):
- name = val
- v = None
- if '=' in name:
- name, v = name.split('=', 1)
- name, v = name.strip(), v.strip()
- if v is None:
- self._text = 'request_param %s' % (name,)
- else:
- self._text = 'request_param %s = %s' % (name, v)
- self.name = name
- self.val = v
+ if not is_nonstr_iter(val):
+ val = (val,)
+ val = sorted(val)
+ self.val = val
+ reqs = []
+ for p in val:
+ k = p
+ v = None
+ if '=' in p:
+ k, v = p.split('=', 1)
+ k, v = k.strip(), v.strip()
+ reqs.append((k, v))
+ self.reqs = reqs
def text(self):
- return self._text
+ return 'request_param %s' % ','.join(
+ ['%s = %s' % (x,y) if y else x for x, y in self.reqs]
+ )
phash = text
def __call__(self, context, request):
- if self.val is None:
- return self.name in request.params
- return request.params.get(self.name) == self.val
-
+ for k, v in self.reqs:
+ actual = request.params.get(k)
+ if actual is None:
+ return False
+ if v is not None and actual != v:
+ return False
+ return True
class HeaderPredicate(object):
def __init__(self, val, config):
View
18 pyramid/tests/test_config/test_predicates.py
@@ -117,6 +117,20 @@ def test___call___true_withval(self):
result = inst(None, request)
self.assertTrue(result)
+ def test___call___true_multi(self):
+ inst = self._makeOne(('abc', 'def =2 '))
+ request = Dummy()
+ request.params = {'abc':'1', 'def': '2'}
+ result = inst(None, request)
+ self.assertTrue(result)
+
+ def test___call___false_multi(self):
+ inst = self._makeOne(('abc=3', 'def =2 '))
+ request = Dummy()
+ request.params = {'abc':'3', 'def': '1'}
+ result = inst(None, request)
+ self.assertFalse(result)
+
def test___call___false(self):
inst = self._makeOne('abc')
request = Dummy()
@@ -132,6 +146,10 @@ def test_text_withval(self):
inst = self._makeOne('abc= 1')
self.assertEqual(inst.text(), 'request_param abc = 1')
+ def test_text_multi(self):
+ inst = self._makeOne(('abc= 1', 'def'))
+ self.assertEqual(inst.text(), 'request_param abc = 1,def')
+
def test_phash_exists(self):
inst = self._makeOne('abc')
self.assertEqual(inst.phash(), 'request_param abc')

0 comments on commit 3fb934e

Please sign in to comment.
Something went wrong with that request. Please try again.