mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 11:27:36 -02:30
Merge pull request #6014 from chrismeyersfsu/improvement-graceful_query_fail
400 on invalid host filter query instead of 500
This commit is contained in:
@@ -1710,6 +1710,13 @@ class HostList(ListCreateAPIView):
|
|||||||
qs = qs.filter(filter_q)
|
qs = qs.filter(filter_q)
|
||||||
return qs
|
return qs
|
||||||
|
|
||||||
|
def list(self, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
except Exception as e:
|
||||||
|
return Response(dict(error=_(unicode(e))), status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
return Response(dict(results=self.serializer_class(queryset, many=True).data))
|
||||||
|
|
||||||
|
|
||||||
class HostDetail(RetrieveUpdateDestroyAPIView):
|
class HostDetail(RetrieveUpdateDestroyAPIView):
|
||||||
|
|
||||||
|
|||||||
@@ -488,13 +488,8 @@ class DynamicFilterField(models.TextField):
|
|||||||
* handle keys with " via: a.\"b.c="yeah"
|
* handle keys with " via: a.\"b.c="yeah"
|
||||||
* handle key with __ in it
|
* handle key with __ in it
|
||||||
|
|
||||||
* add not support
|
|
||||||
|
|
||||||
* transform [] into contains via: a.b.c[].d[].e.f[]="blah"
|
|
||||||
|
|
||||||
* handle optional value quoted: a.b.c=""
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
filter_string_raw = filter_string
|
||||||
filter_string = unicode(filter_string)
|
filter_string = unicode(filter_string)
|
||||||
|
|
||||||
atom = CharsNotIn(unicode_spaces_other)
|
atom = CharsNotIn(unicode_spaces_other)
|
||||||
@@ -511,7 +506,11 @@ class DynamicFilterField(models.TextField):
|
|||||||
("or", 2, opAssoc.LEFT, cls.BoolOr),
|
("or", 2, opAssoc.LEFT, cls.BoolOr),
|
||||||
])
|
])
|
||||||
|
|
||||||
res = boolExpr.parseString('(' + filter_string + ')')
|
try:
|
||||||
|
res = boolExpr.parseString('(' + filter_string + ')')
|
||||||
|
except:
|
||||||
|
raise RuntimeError(u"Invalid query %s" % filter_string_raw)
|
||||||
|
|
||||||
if len(res) > 0:
|
if len(res) > 0:
|
||||||
return res[0].result
|
return res[0].result
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
|
|
||||||
# Python
|
# Python
|
||||||
import pytest
|
import pytest
|
||||||
from pyparsing import ParseException
|
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.fields import DynamicFilterField
|
from awx.main.fields import DynamicFilterField
|
||||||
@@ -32,8 +31,9 @@ class TestDynamicFilterFieldFilterStringToQ():
|
|||||||
'a__b__c__ space =ggg',
|
'a__b__c__ space =ggg',
|
||||||
])
|
])
|
||||||
def test_invalid_filter_strings(self, filter_string):
|
def test_invalid_filter_strings(self, filter_string):
|
||||||
with pytest.raises(ParseException):
|
with pytest.raises(RuntimeError) as e:
|
||||||
DynamicFilterField.filter_string_to_q(filter_string)
|
DynamicFilterField.filter_string_to_q(filter_string)
|
||||||
|
assert e.value.message == u"Invalid query " + filter_string
|
||||||
|
|
||||||
@pytest.mark.parametrize("filter_string,q_expected", [
|
@pytest.mark.parametrize("filter_string,q_expected", [
|
||||||
(u'(a=abc\u1F5E3def)', Q(**{u"a": u"abc\u1F5E3def"})),
|
(u'(a=abc\u1F5E3def)', Q(**{u"a": u"abc\u1F5E3def"})),
|
||||||
|
|||||||
Reference in New Issue
Block a user