1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Use contains for search query in SmartFilter

This commit is contained in:
Wayne Witzel III 2017-05-22 13:48:42 -04:00
parent 903e3076aa
commit 356a2bc057
2 changed files with 6 additions and 6 deletions

View File

@ -114,13 +114,13 @@ class TestSmartFilterQueryFromString():
@pytest.mark.parametrize("filter_string,q_expected", [
('search=foo', Q(Q(**{u"name": u"foo"}) | Q(**{ u"description": u"foo"}))),
('group__search=foo', Q(Q(**{u"group__name": u"foo"}) | Q(**{u"group__description": u"foo"}))),
('search=foo', Q(Q(**{u"name__contains": u"foo"}) | Q(**{ u"description__contains": u"foo"}))),
('group__search=foo', Q(Q(**{u"group__name__contains": u"foo"}) | Q(**{u"group__description__contains": u"foo"}))),
('search=foo and group__search=foo', Q(
Q(**{u"name": u"foo"}) | Q(**{ u"description": u"foo"}),
Q(**{u"group__name": u"foo"}) | Q(**{u"group__description": u"foo"}))),
Q(**{u"name__contains": u"foo"}) | Q(**{ u"description__contains": u"foo"}),
Q(**{u"group__name__contains": u"foo"}) | Q(**{u"group__description__contains": u"foo"}))),
('search=foo or ansible_facts__a=null',
Q(Q(**{u"name": u"foo"}) | Q(**{u"description": u"foo"})) |
Q(Q(**{u"name__contains": u"foo"}) | Q(**{u"description__contains": u"foo"})) |
Q(**{u"ansible_facts__contains": {u"a": u"null"}})),
])
def test_search_related_fields(self, mock_get_host_model, filter_string, q_expected):

View File

@ -51,7 +51,7 @@ class SmartFilter(object):
search_kwargs = self._expand_search(k, v)
if search_kwargs:
kwargs.update(search_kwargs)
q = reduce(lambda x, y: x | y, [django.db.models.Q(**{u'%s' % _k:_v}) for _k, _v in kwargs.items()])
q = reduce(lambda x, y: x | y, [django.db.models.Q(**{u'%s__contains' % _k:_v}) for _k, _v in kwargs.items()])
self.result = Host.objects.filter(q)
else:
kwargs[k] = v