mirror of
git://git.yoctoproject.org/layerindex-web.git
synced 2025-07-19 20:59:01 +02:00
Handle __isnull in API query filtering
If you query on a boolean field you can use the string "False" to match False in the database; however if you try the same with __isnull then the query will match every record which is obviously undesirable. If __isnull is being used, then convert the value to a boolean so that the query works properly. An example of this type of query: http://127.0.0.1:8000/layerindex/api/layerBranches/?filter=yp_compatible_version__isnull:false Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
parent
d27abc10ba
commit
a0396ebec7
|
@ -28,6 +28,7 @@ VALUE_SEPARATOR = "!"
|
||||||
DESCENDING = "-"
|
DESCENDING = "-"
|
||||||
|
|
||||||
def __get_q_for_val(name, value):
|
def __get_q_for_val(name, value):
|
||||||
|
if isinstance(value, str):
|
||||||
if "OR" in value:
|
if "OR" in value:
|
||||||
return functools.reduce(operator.or_, map(lambda x: __get_q_for_val(name, x), [ x for x in value.split("OR") ]))
|
return functools.reduce(operator.or_, map(lambda x: __get_q_for_val(name, x), [ x for x in value.split("OR") ]))
|
||||||
if "AND" in value:
|
if "AND" in value:
|
||||||
|
@ -35,7 +36,6 @@ def __get_q_for_val(name, value):
|
||||||
if value.startswith("NOT"):
|
if value.startswith("NOT"):
|
||||||
kwargs = { name : value.strip("NOT") }
|
kwargs = { name : value.strip("NOT") }
|
||||||
return ~Q(**kwargs)
|
return ~Q(**kwargs)
|
||||||
else:
|
|
||||||
kwargs = { name : value }
|
kwargs = { name : value }
|
||||||
return Q(**kwargs)
|
return Q(**kwargs)
|
||||||
|
|
||||||
|
@ -46,6 +46,10 @@ def _get_filtering_query(filter_string):
|
||||||
values = search_terms[1].split(VALUE_SEPARATOR)
|
values = search_terms[1].split(VALUE_SEPARATOR)
|
||||||
|
|
||||||
querydict = dict(zip(keys, values))
|
querydict = dict(zip(keys, values))
|
||||||
|
for key in keys:
|
||||||
|
if key.endswith('__isnull'):
|
||||||
|
querydict[key] = (querydict[key].lower() == 'true')
|
||||||
|
|
||||||
return functools.reduce(operator.and_, map(lambda x: __get_q_for_val(x, querydict[x]), [k for k in querydict]))
|
return functools.reduce(operator.and_, map(lambda x: __get_q_for_val(x, querydict[x]), [k for k in querydict]))
|
||||||
|
|
||||||
# we check that the input comes in a valid form that we can recognize
|
# we check that the input comes in a valid form that we can recognize
|
||||||
|
|
Loading…
Reference in New Issue
Block a user