From 98c8499f2f4a3ea4c751f0b0384d4830f9d59e6d Mon Sep 17 00:00:00 2001 From: adamscmRH Date: Fri, 21 Jul 2017 10:27:46 -0400 Subject: [PATCH] improved query efficiently, refactored code --- awx/api/views.py | 16 +++++----------- awx/main/tests/unit/api/test_views.py | 5 ++++- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 5ec19e2f8c..3a415f7fef 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2481,13 +2481,10 @@ class InventoryInventorySourcesUpdate(RetrieveAPIView): def retrieve(self, request, *args, **kwargs): inventory = self.get_object() update_data = [] - for inventory_source in inventory.inventory_sources.all(): - if inventory_source.source == '': - continue - else: - details = {'inventory_source': inventory_source.pk, - 'can_update': inventory_source.can_update} - update_data.append(details) + for inventory_source in inventory.inventory_sources.exclude(source=''): + details = {'inventory_source': inventory_source.pk, + 'can_update': inventory_source.can_update} + update_data.append(details) return Response(update_data) def post(self, request, *args, **kwargs): @@ -2495,19 +2492,16 @@ class InventoryInventorySourcesUpdate(RetrieveAPIView): update_data = [] successes = 0 failures = 0 - for inventory_source in inventory.inventory_sources.all(): + for inventory_source in inventory.inventory_sources.exclude(source=''): details = {'inventory_source': inventory_source.pk, 'status': None} can_update = inventory_source.can_update project_update = False - if inventory_source.source == 'scm' and inventory_source.update_on_project_update: if not request.user or not request.user.can_access(Project, 'start', inventory_source.source_project): details['status'] = _('You do not have permission to update project `{}`').format(inventory_source.source_project.name) can_update = False else: project_update = True - if inventory_source.source == '': - continue if can_update: if project_update: details['project_update'] = inventory_source.source_project.update().id diff --git a/awx/main/tests/unit/api/test_views.py b/awx/main/tests/unit/api/test_views.py index 3774668b35..6cc4f411d4 100644 --- a/awx/main/tests/unit/api/test_views.py +++ b/awx/main/tests/unit/api/test_views.py @@ -112,7 +112,10 @@ class TestInventoryInventorySourcesUpdate: return [InventorySource(pk=1, source=is_source, source_project=Project, update_on_project_update=is_up_on_proj, can_update=can_update, update=lambda:InventoryUpdate)] - + + def exclude(self, **kwargs): + return self.all() + Inventory = namedtuple('Inventory', ['inventory_sources']) obj = Inventory(inventory_sources=InventorySources())