Projet

Général

Profil

0003-api_views-only-show-filtering-by-OUs-if-relevant-496.patch

Valentin Deniaud, 25 février 2021 17:49

Télécharger (5,83 ko)

Voir les différences:

Subject: [PATCH 3/3] api_views: only show filtering by OUs if relevant
 (#49670)

 src/authentic2/api_views.py | 16 +++++++-----
 tests/test_api.py           | 52 ++++++++++++++++++++++++++++---------
 2 files changed, 49 insertions(+), 19 deletions(-)
src/authentic2/api_views.py
1154 1154

  
1155 1155
    def list(self, request):
1156 1156
        statistics = []
1157
        ous = [{'id': ou.slug, 'label': ou.name} for ou in get_ou_model().objects.all()]
1157
        OU = get_ou_model()
1158
        services_ous = [{'id': ou.slug, 'label': ou.name} for ou in OU.objects.exclude(service__isnull=True)]
1159
        users_ous = [{'id': ou.slug, 'label': ou.name} for ou in OU.objects.exclude(user__isnull=True)]
1158 1160
        services = [
1159 1161
            {'id': '%s %s' % (service['slug'], service['ou__slug']), 'label': service['name']}
1160 1162
            for service in Service.objects.values('slug', 'name', 'ou__slug')
......
1173 1175
        for action in self.get_extra_actions():
1174 1176
            url = self.reverse_action(action.url_name)
1175 1177
            filters = common_filters.copy()
1176
            if 'services_ou' in action.filters:
1178
            if 'service' in action.filters:
1179
                filters.append({'id': 'service', 'label': _('Service'), 'options': services})
1180
            if 'services_ou' in action.filters and len(services_ous) > 1:
1177 1181
                filters.append(
1178
                    {'id': 'services_ou', 'label': _('Services organizational unit'), 'options': ous}
1182
                    {'id': 'services_ou', 'label': _('Services organizational unit'), 'options': services_ous}
1179 1183
                )
1180
            if 'users_ou' in action.filters:
1184
            if 'users_ou' in action.filters and len(users_ous) > 1:
1181 1185
                filters.append(
1182
                    {'id': 'users_ou', 'label': _('Users organizational unit'), 'options': ous}
1186
                    {'id': 'users_ou', 'label': _('Users organizational unit'), 'options': users_ous}
1183 1187
                )
1184
            if 'service' in action.filters:
1185
                filters.append({'id': 'service', 'label': _('Service'), 'options': services})
1186 1188
            data = {
1187 1189
                'name': action.kwargs['name'],
1188 1190
                'url': url,
tests/test_api.py
2032 2032

  
2033 2033
@pytest.mark.skipif(drf_version.startswith('3.4'), reason='no support for old django rest framework')
2034 2034
def test_api_statistics_list(app, admin):
2035
    OU = get_ou_model()
2035 2036
    headers = basic_authorization_header(admin)
2036 2037
    resp = app.get('/api/statistics/', headers=headers)
2037 2038
    assert len(resp.json['data']) == 6
......
2051 2052
                "required": True,
2052 2053
                "default": "month",
2053 2054
            },
2054
            {
2055
                'id': 'services_ou',
2056
                'label': 'Services organizational unit',
2057
                'options': [{'id': 'default', 'label': 'Default organizational unit'}],
2058
            },
2059
            {
2060
                'id': 'users_ou',
2061
                'label': 'Users organizational unit',
2062
                'options': [{'id': 'default', 'label': 'Default organizational unit'}],
2063
            },
2064 2055
            {'id': 'service', 'label': 'Service', 'options': []},
2065 2056
        ],
2066 2057
    }
......
2086 2077

  
2087 2078
    service = Service.objects.create(name='Service1', slug='service1', ou=get_default_ou())
2088 2079
    service = Service.objects.create(name='Service2', slug='service2', ou=get_default_ou())
2089
    login_stats['filters'][3]['options'].append({'id': 'service1 default', 'label': 'Service1'})
2090
    login_stats['filters'][3]['options'].append({'id': 'service2 default', 'label': 'Service2'})
2080
    login_stats['filters'][1]['options'].append({'id': 'service1 default', 'label': 'Service1'})
2081
    login_stats['filters'][1]['options'].append({'id': 'service2 default', 'label': 'Service2'})
2082

  
2083
    resp = app.get('/api/statistics/', headers=headers)
2084
    assert login_stats in resp.json['data']
2091 2085

  
2086
    # adding second ou doesn't change anything
2087
    ou = OU.objects.create(name='Second OU', slug='second')
2088
    resp = app.get('/api/statistics/', headers=headers)
2089
    assert login_stats in resp.json['data']
2090

  
2091
    # if there are services in two differents OUs, filter is shown
2092
    service.ou = ou
2093
    service.save()
2094
    login_stats['filters'][1]['options'][1]['id'] = 'service2 second'
2095
    login_stats['filters'].append(
2096
            {
2097
                'id': 'services_ou',
2098
                'label': 'Services organizational unit',
2099
                'options': [
2100
                    {'id': 'default', 'label': 'Default organizational unit'},
2101
                    {'id': 'second', 'label': 'Second OU'}
2102
                ],
2103
            }
2104
    )
2105
    resp = app.get('/api/statistics/', headers=headers)
2106
    assert login_stats in resp.json['data']
2107

  
2108
    # same goes with users
2109
    user = User.objects.create(username='john.doe', email='john.doe@example.com', ou=ou)
2110
    login_stats['filters'].append(
2111
            {
2112
                'id': 'users_ou',
2113
                'label': 'Users organizational unit',
2114
                'options': [
2115
                    {'id': 'default', 'label': 'Default organizational unit'},
2116
                    {'id': 'second', 'label': 'Second OU'}
2117
                ],
2118
            }
2119
    )
2092 2120
    resp = app.get('/api/statistics/', headers=headers)
2093 2121
    assert login_stats in resp.json['data']
2094 2122

  
2095
-