From 3f9f28322c3b75471aacfc3359f7bd24dd1857ec Mon Sep 17 00:00:00 2001 From: adamscmRH Date: Thu, 20 Jul 2017 17:04:31 -0400 Subject: [PATCH 1/2] Inventory Update All no longer includes manual sources --- awx/api/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 98a04c4626..5ec19e2f8c 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -2482,9 +2482,12 @@ class InventoryInventorySourcesUpdate(RetrieveAPIView): inventory = self.get_object() update_data = [] for inventory_source in inventory.inventory_sources.all(): - details = {'inventory_source': inventory_source.pk, - 'can_update': inventory_source.can_update} - update_data.append(details) + if inventory_source.source == '': + continue + else: + 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): @@ -2503,7 +2506,8 @@ class InventoryInventorySourcesUpdate(RetrieveAPIView): 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 @@ -2885,7 +2889,7 @@ class JobTemplateSurveySpec(GenericAPIView): if survey_item["type"] == "password": if survey_item.get("default") and survey_item["default"].startswith('$encrypted$'): if not obj.survey_spec: - return Response(dict(error=_("$encrypted$ is reserved keyword and may not be used as a default for password {}.".format(str(idx)))), + return Response(dict(error=_("$encrypted$ is reserved keyword and may not be used as a default for password {}.".format(str(idx)))), status=status.HTTP_400_BAD_REQUEST) else: old_spec = obj.survey_spec From 98c8499f2f4a3ea4c751f0b0384d4830f9d59e6d Mon Sep 17 00:00:00 2001 From: adamscmRH Date: Fri, 21 Jul 2017 10:27:46 -0400 Subject: [PATCH 2/2] 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())