From 7ac59de75edaeefc270fa29009d0a19b93b231e3 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Fri, 22 Apr 2016 16:41:58 -0400 Subject: [PATCH] scan jobs linked by inventory in org JT counts --- awx/api/views.py | 21 ++++++++++++++++--- .../api/test_organization_counts.py | 20 ++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index 1e72d18bee..273b8db902 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -616,9 +616,14 @@ class OrganizationList(ListCreateAPIView): JT_reference = 'project__organization' db_results['job_templates'] = JobTemplate.accessible_objects( - self.request.user, 'read_role').values(JT_reference).annotate( + self.request.user, 'read_role').exclude(job_type='scan').values(JT_reference).annotate( Count(JT_reference)).order_by(JT_reference) + JT_scan_reference = 'inventory__organization' + db_results['job_templates_scan'] = JobTemplate.accessible_objects( + self.request.user, 'read_role').filter(job_type='scan').values(JT_scan_reference).annotate( + Count(JT_scan_reference)).order_by(JT_scan_reference) + db_results['projects'] = project_qs\ .values('organization').annotate(Count('organization')).order_by('organization') @@ -638,6 +643,8 @@ class OrganizationList(ListCreateAPIView): for res in db_results: if res == 'job_templates': org_reference = JT_reference + elif res == 'job_templates_scan': + org_reference = JT_scan_reference elif res == 'users': org_reference = 'id' else: @@ -651,6 +658,12 @@ class OrganizationList(ListCreateAPIView): continue count_context[org_id][res] = entry['%s__count' % org_reference] + # Combine the counts for job templates with scan job templates + for org in org_id_list: + org_id = org['id'] + if 'job_templates_scan' in count_context[org_id]: + count_context[org_id]['job_templates'] += count_context[org_id].pop('job_templates_scan') + full_context['related_field_counts'] = count_context return full_context @@ -684,8 +697,10 @@ class OrganizationDetail(RetrieveUpdateDestroyAPIView): organization__id=org_id).count() org_counts['projects'] = Project.accessible_objects(**access_kwargs).filter( organization__id=org_id).count() - org_counts['job_templates'] = JobTemplate.accessible_objects(**access_kwargs).filter( - project__organization__id=org_id).count() + org_counts['job_templates'] = JobTemplate.accessible_objects(**access_kwargs).exclude( + job_type='scan').filter(project__organization__id=org_id).count() + org_counts['job_templates'] += JobTemplate.accessible_objects(**access_kwargs).filter( + job_type='scan').filter(inventory__organization__id=org_id).count() full_context['related_field_counts'] = {} full_context['related_field_counts'][org_id] = org_counts diff --git a/awx/main/tests/functional/api/test_organization_counts.py b/awx/main/tests/functional/api/test_organization_counts.py index bc6df133d7..0e57488fab 100644 --- a/awx/main/tests/functional/api/test_organization_counts.py +++ b/awx/main/tests/functional/api/test_organization_counts.py @@ -150,6 +150,26 @@ def test_two_organizations(resourced_organization, organizations, user, get): assert counts[org_id_full] == COUNTS_PRIMES assert counts[org_id_zero] == COUNTS_ZEROS +@pytest.mark.django_db +def test_scan_JT_counted(resourced_organization, user, get): + admin_user = user('admin', True) + # Add a scan job template to the org + resourced_organization.projects.all()[0].jobtemplates.create( + job_type='scan', inventory=resourced_organization.inventories.all()[0], + name='scan-job-template') + counts_dict = COUNTS_PRIMES + counts_dict['job_templates'] += 1 + + # Test list view + list_response = get(reverse('api:organization_list', args=[]), admin_user) + assert list_response.status_code == 200 + assert list_response.data['results'][0]['summary_fields']['related_field_counts'] == counts_dict + + # Test detail view + detail_response = get(reverse('api:organization_detail', args=[resourced_organization.pk]), admin_user) + assert detail_response.status_code == 200 + assert detail_response.data['summary_fields']['related_field_counts'] == counts_dict + @pytest.mark.django_db def test_JT_associated_with_project(organizations, project, user, get): # Check that adding a project to an organization gets the project's JT