From 532862983b939e0b8af9e1ebf13898d29870d517 Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Tue, 10 Sep 2024 13:45:00 -0400 Subject: [PATCH] paginated institution-users --- api/base/elasticsearch_dsl_views.py | 8 +++++--- api/institutions/views.py | 7 +++---- .../views/test_institution_user_metric_list.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/api/base/elasticsearch_dsl_views.py b/api/base/elasticsearch_dsl_views.py index 0c9388be126..6d127aff419 100644 --- a/api/base/elasticsearch_dsl_views.py +++ b/api/base/elasticsearch_dsl_views.py @@ -17,7 +17,7 @@ class ElasticsearchListView(FilterMixin, JSONAPIBaseView, generics.ListAPIView, ordering_fields: frozenset[str] = frozenset() @abc.abstractmethod - def get_default_search(self) -> edsl.Search: + def get_default_search(self) -> edsl.Search | None: ... ### @@ -40,7 +40,9 @@ def get_default_search(self) -> edsl.Search: # override rest_framework.generics.GenericAPIView def get_queryset(self): - _search = self.__add_sort(self.get_default_search()) + _search = self.get_default_search() + if _search is None: + return [] # using parsing logic from FilterMixin (oddly nested dict and all) for _parsed_param in self.parse_query_params(self.request.query_params).values(): for _parsed_filter in _parsed_param.values(): @@ -50,7 +52,7 @@ def get_queryset(self): operator=_parsed_filter['op'], value=_parsed_filter['value'], ) - return _search + return self.__add_sort(_search) ### # private methods diff --git a/api/institutions/views.py b/api/institutions/views.py index d2ed75df937..ae6de06ca1a 100644 --- a/api/institutions/views.py +++ b/api/institutions/views.py @@ -545,7 +545,7 @@ class _NewInstitutionUserMetricsList(InstitutionMixin, ElasticsearchListView): serializer_class = NewInstitutionUserMetricsSerializer - default_ordering = '-storage_usage_bytes' + default_ordering = '-storage_byte_count' ordering_fields = frozenset(( 'user_name', 'department', @@ -563,13 +563,12 @@ class _NewInstitutionUserMetricsList(InstitutionMixin, ElasticsearchListView): def get_default_search(self): _yearmonth = InstitutionalUserReport.most_recent_yearmonth() if _yearmonth is None: - return [] - _search = ( + return None + return ( InstitutionalUserReport.search() .filter('term', report_yearmonth=str(_yearmonth)) .filter('term', institution_id=self.get_institution()._id) ) - return _search institution_user_metrics_list_view = view_toggled_by_feature_flag( diff --git a/api_tests/institutions/views/test_institution_user_metric_list.py b/api_tests/institutions/views/test_institution_user_metric_list.py index f3d20a3ba66..e6e0fd4c6df 100644 --- a/api_tests/institutions/views/test_institution_user_metric_list.py +++ b/api_tests/institutions/views/test_institution_user_metric_list.py @@ -396,6 +396,16 @@ def test_sort_reports(self, app, url, institutional_admin, institution, reports, assert _resp.status_code == 200 assert list(_user_ids(_resp)) == _expected_user_id_list + @pytest.mark.es + def test_paginate_reports(self, app, url, institutional_admin, institution, reports, unshown_reports): + for _query, _expected_user_id_list in ( + ({'sort': 'storage_byte_count', 'page[size]': 2}, ['u_sparse', 'u_orc']), + ({'sort': 'storage_byte_count', 'page[size]': 2, 'page': 2}, ['u_blargl', 'u_orcomma']), + ): + _resp = app.get(f'{url}?{urlencode(_query)}', auth=institutional_admin.auth) + assert _resp.status_code == 200 + assert list(_user_ids(_resp)) == _expected_user_id_list + def _user_ids(api_response): for _datum in api_response.json['data']: yield _datum['relationships']['user']['data']['id']