Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/nursix/eden
Browse files Browse the repository at this point in the history
  • Loading branch information
flavour committed Jun 29, 2012
2 parents 9ce391a + 2aa28ee commit 4e1f4cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
21 changes: 20 additions & 1 deletion modules/s3/s3rest.py
Expand Up @@ -5480,6 +5480,10 @@ def belongs(self, value):
def contains(self, value):
return S3ResourceQuery(S3ResourceQuery.CONTAINS, self, value)

# -------------------------------------------------------------------------
def anyof(self, value):
return S3ResourceQuery(S3ResourceQuery.ANYOF, self, value)

# -------------------------------------------------------------------------
def lower(self):
self.op = self.LOWER
Expand Down Expand Up @@ -5595,8 +5599,11 @@ class S3ResourceQuery:
LIKE = "like"
BELONGS = "belongs"
CONTAINS = "contains"
ANYOF = "anyof"

OPERATORS = [NOT, AND, OR, LT, LE, EQ, NE, GE, GT, LIKE, BELONGS, CONTAINS]
OPERATORS = [NOT, AND, OR,
LT, LE, EQ, NE, GE, GT,
LIKE, BELONGS, CONTAINS, ANYOF]

# -------------------------------------------------------------------------
def __init__(self, op, left=None, right=None):
Expand Down Expand Up @@ -5788,6 +5795,8 @@ def _query_bare(self, op, l, r):

if op == self.CONTAINS:
q = l.contains(r, all=True)
elif op == self.ANYOF:
q = l.contains(r, all=False)
elif op == self.BELONGS:
if type(r) is list and None in r:
_r = [item for item in r if item is not None]
Expand Down Expand Up @@ -5930,6 +5939,16 @@ def _probe(self, op, l, r):
if op == self.CONTAINS:
r = convert(l, r)
result = contains(l, r)
elif op == self.ANYOF:
if not isinstance(r, (list, tuple)):
r = [r]
for v in r:
if isinstance(l, (list, tuple, basestring)):
if contains(l, r):
return True
elif l == r:
return True
return False
elif op == self.BELONGS:
r = convert(l, r)
result = contains(r, l)
Expand Down
13 changes: 13 additions & 0 deletions tests/unit_tests/modules/s3/s3rest.py
Expand Up @@ -711,6 +711,19 @@ def testParseValue(self):
self.assertEqual(parse_value('"NONE",1'), ["NONE", "1"])
self.assertEqual(parse_value('"NONE,1"'), "NONE,1")

def testAnyOf(self):

resource = s3mgr.define_resource("org", "organisation")
FS = s3base.S3FieldSelector
q = FS("sector_id").contains([1, 2])
query = q.query(resource)
self.assertEqual(str(query), "((org_organisation.sector_id LIKE '%|1|%') AND "
"(org_organisation.sector_id LIKE '%|2|%'))")
q = FS("sector_id").anyof([1, 2])
query = q.query(resource)
self.assertEqual(str(query), "((org_organisation.sector_id LIKE '%|1|%') OR "
"(org_organisation.sector_id LIKE '%|2|%'))")

def tearDown(self):
auth.s3_impersonate(None)

Expand Down

0 comments on commit 4e1f4cd

Please sign in to comment.