From d7567e101d20424845998bf9ebe97c0470abb267 Mon Sep 17 00:00:00 2001 From: naegi Date: Sun, 23 Apr 2017 18:10:54 +0200 Subject: [PATCH] update filters --- opv_api_client/filter.py | 58 ++++++++---------------------------- opv_api_client/restclient.py | 3 +- tests/test_filter.py | 9 +++--- tests/test_restclient.py | 4 +-- 4 files changed, 20 insertions(+), 54 deletions(-) diff --git a/opv_api_client/filter.py b/opv_api_client/filter.py index 68943b5..95fffa7 100644 --- a/opv_api_client/filter.py +++ b/opv_api_client/filter.py @@ -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 @@ -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 @@ -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), + [] + ) + ) diff --git a/opv_api_client/restclient.py b/opv_api_client/restclient.py index 64898a6..808980d 100644 --- a/opv_api_client/restclient.py +++ b/opv_api_client/restclient.py @@ -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() diff --git a/tests/test_filter.py b/tests/test_filter.py index 43b9fc4..ef64106 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -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} diff --git a/tests/test_restclient.py b/tests/test_restclient.py index e67b8f4..f33a17a 100644 --- a/tests/test_restclient.py +++ b/tests/test_restclient.py @@ -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"}