From eb73248d37e212864c7df67a6bf4899fe69a4030 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Wed, 2 Aug 2017 15:13:27 -0400 Subject: [PATCH] Refactor InventoryHostsList get_queryset to use getattrd --- awx/api/views.py | 5 +---- awx/main/tests/unit/api/test_views.py | 10 +++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 4531cbe5dc..734011f3ef 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1961,10 +1961,7 @@ class InventoryHostsList(SubListCreateAttachDetachAPIView): def get_queryset(self): inventory = self.get_parent_object() - if inventory.kind == 'smart': - filter_qs = SmartFilter.query_from_string(inventory.host_filter) - return filter_qs.distinct() - return super(InventoryHostsList, self).get_queryset() + return getattrd(inventory, self.relationship).all() class HostGroupsList(ControlledByScmMixin, SubListCreateAttachDetachAPIView): diff --git a/awx/main/tests/unit/api/test_views.py b/awx/main/tests/unit/api/test_views.py index 824f5661d3..8d7ccdfc59 100644 --- a/awx/main/tests/unit/api/test_views.py +++ b/awx/main/tests/unit/api/test_views.py @@ -17,6 +17,8 @@ from awx.main.models import ( Host, ) +from awx.main.managers import HostManager + @pytest.fixture def mock_response_new(mocker): @@ -210,10 +212,12 @@ class TestHostInsights(): class TestInventoryHostsList(object): def test_host_list_smart_inventory(self, mocker): - Inventory = namedtuple('Inventory', ['kind', 'host_filter']) - obj = Inventory(kind='smart', host_filter='localhost') + Inventory = namedtuple('Inventory', ['kind', 'host_filter', 'hosts']) + obj = Inventory(kind='smart', host_filter='localhost', hosts=HostManager()) + obj.hosts.instance = obj + with mock.patch.object(InventoryHostsList, 'get_parent_object', return_value=obj): - with mock.patch('awx.api.views.SmartFilter.query_from_string') as mock_query: + with mock.patch('awx.main.utils.filters.SmartFilter.query_from_string') as mock_query: view = InventoryHostsList() view.get_queryset() mock_query.assert_called_once_with('localhost')