mirror of
https://github.com/ansible/awx.git
synced 2026-02-16 10:40:01 -03:30
Merge pull request #1301 from AlanCoding/647_org_detail_counts
Add organization counts in detail view
This commit is contained in:
@@ -691,6 +691,31 @@ class OrganizationDetail(RetrieveUpdateDestroyAPIView):
|
|||||||
model = Organization
|
model = Organization
|
||||||
serializer_class = OrganizationSerializer
|
serializer_class = OrganizationSerializer
|
||||||
|
|
||||||
|
def get_serializer_context(self, *args, **kwargs):
|
||||||
|
full_context = super(OrganizationDetail, self).get_serializer_context(*args, **kwargs)
|
||||||
|
|
||||||
|
if not hasattr(self, 'kwargs'):
|
||||||
|
return full_context
|
||||||
|
org_id = int(self.kwargs['pk'])
|
||||||
|
|
||||||
|
org_counts = {}
|
||||||
|
user_qs = self.request.user.get_queryset(User)
|
||||||
|
org_counts['users'] = user_qs.filter(organizations__id=org_id).count()
|
||||||
|
org_counts['admins'] = user_qs.filter(admin_of_organizations__id=org_id).count()
|
||||||
|
org_counts['inventories'] = self.request.user.get_queryset(Inventory).filter(
|
||||||
|
organization__id=org_id).count()
|
||||||
|
org_counts['teams'] = self.request.user.get_queryset(Team).filter(
|
||||||
|
organization__id=org_id).count()
|
||||||
|
org_counts['projects'] = self.request.user.get_queryset(Project).filter(
|
||||||
|
organizations__id=org_id).count()
|
||||||
|
org_counts['job_templates'] = self.request.user.get_queryset(JobTemplate).filter(
|
||||||
|
inventory__organization__id=org_id).count()
|
||||||
|
|
||||||
|
full_context['related_field_counts'] = {}
|
||||||
|
full_context['related_field_counts'][org_id] = org_counts
|
||||||
|
|
||||||
|
return full_context
|
||||||
|
|
||||||
class OrganizationInventoriesList(SubListAPIView):
|
class OrganizationInventoriesList(SubListAPIView):
|
||||||
|
|
||||||
model = Inventory
|
model = Inventory
|
||||||
|
|||||||
@@ -20,6 +20,25 @@ def resourced_organization(organization, project, team, inventory, user):
|
|||||||
|
|
||||||
return organization
|
return organization
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_org_counts_detail_view(resourced_organization, user, get):
|
||||||
|
# Check that all types of resources are counted by a superuser
|
||||||
|
external_admin = user('admin', True)
|
||||||
|
response = get(reverse('api:organization_detail',
|
||||||
|
args=[resourced_organization.pk]), external_admin)
|
||||||
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
counts = response.data['summary_fields']['related_field_counts']
|
||||||
|
assert counts == {
|
||||||
|
'users': 1,
|
||||||
|
'admins': 1,
|
||||||
|
'job_templates': 1,
|
||||||
|
'projects': 1,
|
||||||
|
'inventories': 1,
|
||||||
|
'teams': 1
|
||||||
|
}
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_org_counts_admin(resourced_organization, user, get):
|
def test_org_counts_admin(resourced_organization, user, get):
|
||||||
# Check that all types of resources are counted by a superuser
|
# Check that all types of resources are counted by a superuser
|
||||||
|
|||||||
Reference in New Issue
Block a user