Skip to content

Commit

Permalink
update filters
Browse files Browse the repository at this point in the history
  • Loading branch information
philae-ael committed Apr 23, 2017
1 parent f15e53b commit d7567e1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 54 deletions.
58 changes: 13 additions & 45 deletions opv_api_client/filter.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
def _make_op(op):
def f(self, val):
self.op(op)
self.value(val)
return self
return f

class Filter:
"""Add primitives that simplify filtering in ressource
This allow to use filters easily. Filter are (nowaday) constitued of 3 fields:
- op
- name
- value
To set those values, you can respectively use the `op`, `name` and `val`/`value` fonction to set those
To set those values, you can respectively use the `name` and `val`/`value` fonction to set those
"""
def op(self, op):
"""Allow to set the op's field of the filter
Args:
op(str): The name of the operation
Return:
Filter: return the modifyed Filter(self)
"""
self._op = op
return self

def name(self, name):
"""Allow to set the name's field of the filter
Expand Down Expand Up @@ -56,23 +37,13 @@ def get(self):
Allow to get the final filter, in a form that can directly be used by the API
"""
return {"name": self._name, "op": self._op, "val": str(self._value)}
return {self._name: self._value}

def __init__(self, name=None):
self._name = name
self._op = None
self._value = None

__eq__ = _make_op("eq")
__ne__ = _make_op("neq")

__lt__ = _make_op("lt")
__gt__ = _make_op("gt")

__le__ = _make_op("le")
__ge__ = _make_op("ge")

# TODO: like, is_null, is_not_null, like, has, any
__eq__ = value

def treat_query(query):
"""A fonction that get filters and transform it into a form understable by the API
Expand All @@ -82,18 +53,15 @@ def treat_query(query):
Args:
query(:obj: list or :obj: Filter): The query to treat
Returns:
list: A list of filters, in a form understable by the API
dict: A dict of filters, in a form understable by the API -> now is a the param dict
"""
if isinstance(query, Filter):
return [query.get()]

if isinstance(query, dict):
return [query]

try:
iter(query) # is iterable ?
except TypeError: # isn't iterable
pass
else:
# recursively treat queries and flatten the list
return sum((treat_query(q) for q in query), [])
return query.get()

# recursively treat queries and flatten the dict
return dict(
sum( # get a list of keys/value (tuple)
(list(q.get().items()) for q in query),
[]
)
)
3 changes: 1 addition & 2 deletions opv_api_client/restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ def make_all(self, ressource, filters=None):

# Create params that will be send
if filters:
filters = treat_query(filters) # Filter is now a list of dict as accepted by the API
params = dict(q=json.dumps(dict(filters=filters)))
params = treat_query(filters) # Filter is now a list of dict as accepted by the API
else:
params = dict()

Expand Down
9 changes: 4 additions & 5 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

def test_filter():
f1 = Filter("test") == 1
f2 = Filter().name("test").op("eq").val(1)
f2 = Filter().name("test").val(1)

assert f1.get() == f2.get()
assert f1.get() == {"name": "test", "op": "eq", "val": "1"}
assert f1.get() == {'test': 1}

def test_treat_query():
q = [Filter("test") == -1,
Filter("T2").op("in").val(5)]
Filter("T2") == 5]

assert treat_query(q) == [{"name": "test", "op": "eq", "val": "-1"},
{"name": "T2", "op": "in", "val": "5"}]
assert treat_query(q) == {"test": -1, "T2": 5}
4 changes: 2 additions & 2 deletions tests/test_restclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def test_make_all():
[{"machin": "truc"}, {"machin": "truc"}]
}

filters = treat_query(Filter("machin") == "truc")
fparams = dict(q=json.dumps(dict(filters=filters)))
filters = Filter("machin") == "truc"
fparams = treat_query(filters)

waited1 = [ressources.Lot(c) for _ in range(3)]
waited1[0]._data = {"machin": "truc"}
Expand Down

0 comments on commit d7567e1

Please sign in to comment.