mirror of
https://github.com/ansible/awx.git
synced 2026-03-24 04:15:02 -02:30
DynamicFilterQuerySet -> DynamicFilter
This commit is contained in:
@@ -35,7 +35,7 @@ from jsonfield import JSONField as upstream_JSONField
|
|||||||
from jsonbfield.fields import JSONField as upstream_JSONBField
|
from jsonbfield.fields import JSONField as upstream_JSONBField
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.querysets import DynamicFilterQuerySet
|
from awx.main.utils.filters import DynamicFilter
|
||||||
from awx.main.models.rbac import batch_role_ancestor_rebuilding, Role
|
from awx.main.models.rbac import batch_role_ancestor_rebuilding, Role
|
||||||
from awx.main import utils
|
from awx.main import utils
|
||||||
|
|
||||||
@@ -334,7 +334,7 @@ class DynamicFilterField(models.TextField):
|
|||||||
if value is None:
|
if value is None:
|
||||||
return value
|
return value
|
||||||
try:
|
try:
|
||||||
DynamicFilterQuerySet().query_from_string(value)
|
DynamicFilter().query_from_string(value)
|
||||||
except RuntimeError, e:
|
except RuntimeError, e:
|
||||||
raise models.base.ValidationError(e)
|
raise models.base.ValidationError(e)
|
||||||
return super(DynamicFilterField, self).get_prep_value(value)
|
return super(DynamicFilterField, self).get_prep_value(value)
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from django.utils.timezone import now
|
|||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from awx.main.querysets import DynamicFilterQuerySet
|
from awx.main.utils.filters import DynamicFilter
|
||||||
|
|
||||||
|
|
||||||
class HostManager(models.Manager):
|
class HostManager(models.Manager):
|
||||||
@@ -26,10 +26,10 @@ class HostManager(models.Manager):
|
|||||||
"""When the Inventory this host belongs to has a `host_filter` set
|
"""When the Inventory this host belongs to has a `host_filter` set
|
||||||
generate the QuerySet using that filter. Otherwise just return the default filter.
|
generate the QuerySet using that filter. Otherwise just return the default filter.
|
||||||
"""
|
"""
|
||||||
qs = DynamicFilterQuerySet(self.model, using=self._db)
|
qs = super(HostManager, self).get_queryset()
|
||||||
if self.instance is not None:
|
if self.instance is not None:
|
||||||
if hasattr(self.instance, 'host_filter') and self.instance.host_filter is not None:
|
if hasattr(self.instance, 'host_filter') and self.instance.host_filter is not None:
|
||||||
q = qs.query_from_string(self.instance.host_filter)
|
q = DynamicFilter.query_from_string(self.instance.host_filter)
|
||||||
# If we are using host_filters, disable the core_filters, this allows
|
# If we are using host_filters, disable the core_filters, this allows
|
||||||
# us to access all of the available Host entries, not just the ones associated
|
# us to access all of the available Host entries, not just the ones associated
|
||||||
# with a specific FK/relation.
|
# with a specific FK/relation.
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.querysets import DynamicFilterQuerySet
|
from awx.main.utils.filters import DynamicFilter
|
||||||
|
|
||||||
# Django
|
# Django
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
|
|
||||||
|
|
||||||
class TestDynamicFilterQuerySetQueryFromString():
|
class TestDynamicFilterQueryFromString():
|
||||||
@pytest.mark.parametrize("filter_string,q_expected", [
|
@pytest.mark.parametrize("filter_string,q_expected", [
|
||||||
('facts__facts__blank=""', Q(**{u"facts__facts__blank": u""})),
|
('facts__facts__blank=""', Q(**{u"facts__facts__blank": u""})),
|
||||||
('"facts__facts__ space "="f"', Q(**{u"facts__facts__ space ": u"f"})),
|
('"facts__facts__ space "="f"', Q(**{u"facts__facts__ space ": u"f"})),
|
||||||
@@ -23,7 +23,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
|
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
|
||||||
])
|
])
|
||||||
def test_query_generated(self, filter_string, q_expected):
|
def test_query_generated(self, filter_string, q_expected):
|
||||||
q = DynamicFilterQuerySet.query_from_string(filter_string)
|
q = DynamicFilter.query_from_string(filter_string)
|
||||||
assert unicode(q) == unicode(q_expected)
|
assert unicode(q) == unicode(q_expected)
|
||||||
|
|
||||||
@pytest.mark.parametrize("filter_string", [
|
@pytest.mark.parametrize("filter_string", [
|
||||||
@@ -32,7 +32,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
])
|
])
|
||||||
def test_invalid_filter_strings(self, filter_string):
|
def test_invalid_filter_strings(self, filter_string):
|
||||||
with pytest.raises(RuntimeError) as e:
|
with pytest.raises(RuntimeError) as e:
|
||||||
DynamicFilterQuerySet.query_from_string(filter_string)
|
DynamicFilter.query_from_string(filter_string)
|
||||||
assert e.value.message == u"Invalid query " + 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", [
|
||||||
@@ -40,7 +40,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
(u'(ansible_facts__a=abc\u1F5E3def)', Q(**{u"ansible_facts__contains": {u"a": u"abc\u1F5E3def"}})),
|
(u'(ansible_facts__a=abc\u1F5E3def)', Q(**{u"ansible_facts__contains": {u"a": u"abc\u1F5E3def"}})),
|
||||||
])
|
])
|
||||||
def test_unicode(self, filter_string, q_expected):
|
def test_unicode(self, filter_string, q_expected):
|
||||||
q = DynamicFilterQuerySet.query_from_string(filter_string)
|
q = DynamicFilter.query_from_string(filter_string)
|
||||||
assert unicode(q) == unicode(q_expected)
|
assert unicode(q) == unicode(q_expected)
|
||||||
|
|
||||||
@pytest.mark.parametrize("filter_string,q_expected", [
|
@pytest.mark.parametrize("filter_string,q_expected", [
|
||||||
@@ -54,7 +54,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
('a=b or a=d or a=e or a=z and b=h and b=i and b=j and b=k', Q(**{u"a": u"b"}) | Q(**{u"a": u"d"}) | Q(**{u"a": u"e"}) | Q(**{u"a": u"z"}) & Q(**{u"b": u"h"}) & Q(**{u"b": u"i"}) & Q(**{u"b": u"j"}) & Q(**{u"b": u"k"}))
|
('a=b or a=d or a=e or a=z and b=h and b=i and b=j and b=k', Q(**{u"a": u"b"}) | Q(**{u"a": u"d"}) | Q(**{u"a": u"e"}) | Q(**{u"a": u"z"}) & Q(**{u"b": u"h"}) & Q(**{u"b": u"i"}) & Q(**{u"b": u"j"}) & Q(**{u"b": u"k"}))
|
||||||
])
|
])
|
||||||
def test_boolean_parenthesis(self, filter_string, q_expected):
|
def test_boolean_parenthesis(self, filter_string, q_expected):
|
||||||
q = DynamicFilterQuerySet.query_from_string(filter_string)
|
q = DynamicFilter.query_from_string(filter_string)
|
||||||
assert unicode(q) == unicode(q_expected)
|
assert unicode(q) == unicode(q_expected)
|
||||||
|
|
||||||
@pytest.mark.parametrize("filter_string,q_expected", [
|
@pytest.mark.parametrize("filter_string,q_expected", [
|
||||||
@@ -74,7 +74,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
|
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
|
||||||
])
|
])
|
||||||
def test_contains_query_generated(self, filter_string, q_expected):
|
def test_contains_query_generated(self, filter_string, q_expected):
|
||||||
q = DynamicFilterQuerySet.query_from_string(filter_string)
|
q = DynamicFilter.query_from_string(filter_string)
|
||||||
assert unicode(q) == unicode(q_expected)
|
assert unicode(q) == unicode(q_expected)
|
||||||
|
|
||||||
@pytest.mark.parametrize("filter_string,q_expected", [
|
@pytest.mark.parametrize("filter_string,q_expected", [
|
||||||
@@ -84,7 +84,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
|
#('a__b\"__c="true"', Q(**{u"a__b\"__c": "true"})),
|
||||||
])
|
])
|
||||||
def test_contains_query_generated_unicode(self, filter_string, q_expected):
|
def test_contains_query_generated_unicode(self, filter_string, q_expected):
|
||||||
q = DynamicFilterQuerySet.query_from_string(filter_string)
|
q = DynamicFilter.query_from_string(filter_string)
|
||||||
assert unicode(q) == unicode(q_expected)
|
assert unicode(q) == unicode(q_expected)
|
||||||
|
|
||||||
@pytest.mark.parametrize("filter_string,q_expected", [
|
@pytest.mark.parametrize("filter_string,q_expected", [
|
||||||
@@ -92,7 +92,7 @@ class TestDynamicFilterQuerySetQueryFromString():
|
|||||||
('ansible_facts__c="null"', Q(**{u"ansible_facts__contains": {u"c": u"\"null\""}})),
|
('ansible_facts__c="null"', Q(**{u"ansible_facts__contains": {u"c": u"\"null\""}})),
|
||||||
])
|
])
|
||||||
def test_contains_query_generated_null(self, filter_string, q_expected):
|
def test_contains_query_generated_null(self, filter_string, q_expected):
|
||||||
q = DynamicFilterQuerySet.query_from_string(filter_string)
|
q = DynamicFilter.query_from_string(filter_string)
|
||||||
assert unicode(q) == unicode(q_expected)
|
assert unicode(q) == unicode(q_expected)
|
||||||
|
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ from pyparsing import (
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['DynamicFilterQuerySet']
|
__all__ = ['DynamicFilter']
|
||||||
|
|
||||||
|
|
||||||
unicode_spaces = [unichr(c) for c in xrange(sys.maxunicode) if unichr(c).isspace()]
|
unicode_spaces = [unichr(c) for c in xrange(sys.maxunicode) if unichr(c).isspace()]
|
||||||
@@ -33,7 +33,7 @@ def string_to_type(t):
|
|||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
||||||
class DynamicFilterQuerySet(models.QuerySet):
|
class DynamicFilter(object):
|
||||||
|
|
||||||
|
|
||||||
class BoolOperand(object):
|
class BoolOperand(object):
|
||||||
Reference in New Issue
Block a user