api_views: only show filtering by OUs if relevant (#49670)
This commit is contained in:
parent
dd3ed19adf
commit
2d803b4a91
|
@ -1156,7 +1156,9 @@ class StatisticsAPI(ViewSet):
|
|||
|
||||
def list(self, request):
|
||||
statistics = []
|
||||
ous = [{'id': ou.slug, 'label': ou.name} for ou in get_ou_model().objects.all()]
|
||||
OU = get_ou_model()
|
||||
services_ous = [{'id': ou.slug, 'label': ou.name} for ou in OU.objects.exclude(service__isnull=True)]
|
||||
users_ous = [{'id': ou.slug, 'label': ou.name} for ou in OU.objects.exclude(user__isnull=True)]
|
||||
services = [
|
||||
{'id': '%s %s' % (service['slug'], service['ou__slug']), 'label': service['name']}
|
||||
for service in Service.objects.values('slug', 'name', 'ou__slug')
|
||||
|
@ -1175,16 +1177,16 @@ class StatisticsAPI(ViewSet):
|
|||
for action in self.get_extra_actions():
|
||||
url = self.reverse_action(action.url_name)
|
||||
filters = common_filters.copy()
|
||||
if 'services_ou' in action.filters:
|
||||
filters.append(
|
||||
{'id': 'services_ou', 'label': _('Services organizational unit'), 'options': ous}
|
||||
)
|
||||
if 'users_ou' in action.filters:
|
||||
filters.append(
|
||||
{'id': 'users_ou', 'label': _('Users organizational unit'), 'options': ous}
|
||||
)
|
||||
if 'service' in action.filters:
|
||||
filters.append({'id': 'service', 'label': _('Service'), 'options': services})
|
||||
if 'services_ou' in action.filters and len(services_ous) > 1:
|
||||
filters.append(
|
||||
{'id': 'services_ou', 'label': _('Services organizational unit'), 'options': services_ous}
|
||||
)
|
||||
if 'users_ou' in action.filters and len(users_ous) > 1:
|
||||
filters.append(
|
||||
{'id': 'users_ou', 'label': _('Users organizational unit'), 'options': users_ous}
|
||||
)
|
||||
data = {
|
||||
'name': action.kwargs['name'],
|
||||
'url': url,
|
||||
|
|
|
@ -2044,6 +2044,7 @@ def test_api_password_change_user_delete(app, settings, admin, ou1):
|
|||
|
||||
@pytest.mark.skipif(drf_version.startswith('3.4'), reason='no support for old django rest framework')
|
||||
def test_api_statistics_list(app, admin):
|
||||
OU = get_ou_model()
|
||||
headers = basic_authorization_header(admin)
|
||||
resp = app.get('/api/statistics/', headers=headers)
|
||||
assert len(resp.json['data']) == 6
|
||||
|
@ -2063,16 +2064,6 @@ def test_api_statistics_list(app, admin):
|
|||
"required": True,
|
||||
"default": "month",
|
||||
},
|
||||
{
|
||||
'id': 'services_ou',
|
||||
'label': 'Services organizational unit',
|
||||
'options': [{'id': 'default', 'label': 'Default organizational unit'}],
|
||||
},
|
||||
{
|
||||
'id': 'users_ou',
|
||||
'label': 'Users organizational unit',
|
||||
'options': [{'id': 'default', 'label': 'Default organizational unit'}],
|
||||
},
|
||||
{'id': 'service', 'label': 'Service', 'options': []},
|
||||
],
|
||||
}
|
||||
|
@ -2098,12 +2089,49 @@ def test_api_statistics_list(app, admin):
|
|||
|
||||
service = Service.objects.create(name='Service1', slug='service1', ou=get_default_ou())
|
||||
service = Service.objects.create(name='Service2', slug='service2', ou=get_default_ou())
|
||||
login_stats['filters'][3]['options'].append({'id': 'service1 default', 'label': 'Service1'})
|
||||
login_stats['filters'][3]['options'].append({'id': 'service2 default', 'label': 'Service2'})
|
||||
login_stats['filters'][1]['options'].append({'id': 'service1 default', 'label': 'Service1'})
|
||||
login_stats['filters'][1]['options'].append({'id': 'service2 default', 'label': 'Service2'})
|
||||
|
||||
resp = app.get('/api/statistics/', headers=headers)
|
||||
assert login_stats in resp.json['data']
|
||||
|
||||
# adding second ou doesn't change anything
|
||||
ou = OU.objects.create(name='Second OU', slug='second')
|
||||
resp = app.get('/api/statistics/', headers=headers)
|
||||
assert login_stats in resp.json['data']
|
||||
|
||||
# if there are services in two differents OUs, filter is shown
|
||||
service.ou = ou
|
||||
service.save()
|
||||
login_stats['filters'][1]['options'][1]['id'] = 'service2 second'
|
||||
login_stats['filters'].append(
|
||||
{
|
||||
'id': 'services_ou',
|
||||
'label': 'Services organizational unit',
|
||||
'options': [
|
||||
{'id': 'default', 'label': 'Default organizational unit'},
|
||||
{'id': 'second', 'label': 'Second OU'}
|
||||
],
|
||||
}
|
||||
)
|
||||
resp = app.get('/api/statistics/', headers=headers)
|
||||
assert login_stats in resp.json['data']
|
||||
|
||||
# same goes with users
|
||||
user = User.objects.create(username='john.doe', email='john.doe@example.com', ou=ou)
|
||||
login_stats['filters'].append(
|
||||
{
|
||||
'id': 'users_ou',
|
||||
'label': 'Users organizational unit',
|
||||
'options': [
|
||||
{'id': 'default', 'label': 'Default organizational unit'},
|
||||
{'id': 'second', 'label': 'Second OU'}
|
||||
],
|
||||
}
|
||||
)
|
||||
resp = app.get('/api/statistics/', headers=headers)
|
||||
assert login_stats in resp.json['data']
|
||||
|
||||
|
||||
@pytest.mark.skipif(drf_version.startswith('3.4'), reason='no support for old django rest framework')
|
||||
@pytest.mark.parametrize(
|
||||
|
|
Loading…
Reference in New Issue