Skip to content

Commit

Permalink
Initial support for the find operator
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 3, 2019
1 parent 929f748 commit 630f7f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/quorum/model.py
Expand Up @@ -1426,20 +1426,28 @@ def default(cls):
return default

@classmethod
def filter_merge(cls, name, filter, kwargs):
def filter_merge(cls, name, filter, kwargs, operator = None):
# retrieves a possible previous filter defined for the
# provided name in case it does exists must concatenate
# that previous value in an and statement
# provided name in case it does exist must concatenate
# that previous value in a join statement according to
# the currently defined operator
filter_p = kwargs.get(name, None)
if filter_p:
if filter_p or not operator == None:
# defaults the operator for the join of the names to the
# value and then ensures that the value of the operator
# is within a valid range of values
operator = operator or "$and"
util.verify(operator in ("$and", "$or"))

# retrieves the and references for the current arguments
# and appends the two filter values (current and previous)
# then deletes the current name reference in the arguments
# and updates the name value to the and value
filter_a = kwargs.get("$and", [])
filter = filter_a + [{name : filter}, {name : filter_p}]
del kwargs[name]
name = "$and"
filter_a = kwargs.get(operator, [])
if filter_p: filter = filter_a + [{name : filter}, {name : filter_p}]
else: filter = filter_a + [{name : filter}]
if name in kwargs: del kwargs[name]
name = operator

# sets the currently defined filter structures in the keyword
# based arguments map for the currently defined name
Expand Down Expand Up @@ -1769,6 +1777,10 @@ def _find_d(cls, kwargs):
# meant to be done on this method
if not "find_d" in kwargs: return

# tries to retrieve the value of the operator that is going
# to be used to "join" the multiple find parts (find values)
find_o = kwargs.pop("find_o", None)

# retrieves the find definition into a local variable, then
# removes the find definition from the named arguments map
# so that it's not going to be erroneously used by the
Expand Down Expand Up @@ -1828,7 +1840,7 @@ def _find_d(cls, kwargs):
# current set of filters for the provided (keyword) arguments
find_v = {operator : value} if operator else value
if insensitive: find_v["$options"] = "-i"
cls.filter_merge(name, find_v, kwargs)
cls.filter_merge(name, find_v, kwargs, operator = find_o)

@classmethod
def _bases(cls, subclass = None):
Expand Down
2 changes: 2 additions & 0 deletions src/quorum/util.py
Expand Up @@ -116,6 +116,7 @@ def to_sort(sort_s):
"filter_def" : "find_d",
"filter_string" : "find_s",
"filter_name" : "find_n",
"filter_operator" : "find_o",
"insensitive" : "find_i",
"order" : "sort",
"offset" : "skip",
Expand All @@ -133,6 +134,7 @@ def to_sort(sort_s):
find_i = bool,
find_t = legacy.UNICODE,
find_n = legacy.UNICODE,
find_o = legacy.UNICODE,
sort = to_sort,
meta = bool,
fields = list
Expand Down

0 comments on commit 630f7f7

Please sign in to comment.