Skip to content

Commit

Permalink
paginated institution-users
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Sep 10, 2024
1 parent 9d13299 commit 5328629
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 5 additions & 3 deletions api/base/elasticsearch_dsl_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
...

###
Expand All @@ -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():
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions api/institutions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions api_tests/institutions/views/test_institution_user_metric_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 5328629

Please sign in to comment.