diff --git a/api/institutions/views.py b/api/institutions/views.py index f5602bafcb2..2cfd357d9d5 100644 --- a/api/institutions/views.py +++ b/api/institutions/views.py @@ -34,6 +34,7 @@ ) from api.base.settings import DEFAULT_ES_NULL_VALUE from api.metrics.permissions import IsInstitutionalMetricsUser +from api.metrics.renderers import MetricsReportsCsvRenderer, MetricsReportsTsvRenderer from api.nodes.serializers import NodeSerializer from api.nodes.filters import NodesFilterMixin from api.users.serializers import UserSerializer @@ -553,6 +554,11 @@ class _NewInstitutionUserMetricsList(InstitutionMixin, ElasticsearchListView): view_category = 'institutions' view_name = 'institution-user-metrics' + renderer_classes = ( + *api_settings.DEFAULT_RENDERER_CLASSES, + MetricsReportsCsvRenderer, + MetricsReportsTsvRenderer, + ) serializer_class = NewInstitutionUserMetricsSerializer 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 29881e2e292..af8172a63b7 100644 --- a/api_tests/institutions/views/test_institution_user_metric_list.py +++ b/api_tests/institutions/views/test_institution_user_metric_list.py @@ -404,10 +404,82 @@ def test_paginate_reports(self, app, url, institutional_admin, institution, repo assert _resp.status_code == 200 assert list(_user_ids(_resp)) == _expected_user_id_list + def test_get_csv(self, app, url, institutional_admin, rando, institution): + _report_factory( + '2024-08', + institution, + user_id='u_orcomma', + account_creation_date='2018-02', + user_name='Jason Kelce', + orcid_id='4444-3333-2222-1111', + department_name='Offensive Line', + storage_byte_count=736662999298, + embargoed_registration_count=1, + published_preprint_count=1, + public_registration_count=2, + public_project_count=3, + public_file_count=4, + private_project_count=5, + month_last_active='2018-02', + month_last_login='2018-02', + ), + + resp = app.get( + url, + auth=institutional_admin.auth, + headers={ + 'accept': 'text/csv' + } + ) + + assert resp.status_code == 200 + assert resp.headers['Content-Type'] == 'text/csv; charset=utf-8' + + response_body = resp.text + expected_response = [ + [ # Column Headers + 'account_creation_date', + 'department', + 'embargoed_registration_count', + 'month_last_active', + 'month_last_login', + 'orcid_id', + 'private_projects', + 'public_file_count', + 'public_projects', + 'public_registration_count', + 'published_preprint_count', + 'storage_byte_count', + 'user_name' + ], + [ + '2018-02', + 'Offensive Line', + '1', + '2018-02', + '2018-02', + '4444-3333-2222-1111', + '5', + '4', + '3', + '2', + '1', + '736662999298', + 'Jason Kelce' + ] + ] + + with StringIO(response_body) as csv_file: + csvreader = csv.reader(csv_file, delimiter=',') + for index, row in enumerate(csvreader): + assert row == expected_response[index] + + def _user_ids(api_response): for _datum in api_response.json['data']: yield _datum['relationships']['user']['data']['id'] + def _report_factory(yearmonth, institution, **kwargs): _report = InstitutionalUserReport( report_yearmonth=yearmonth,