From a89f5bcf5c0c2c8a4d5df026fcfa6e44fae3f20f Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Fri, 6 Sep 2024 16:48:33 -0400 Subject: [PATCH] add institution-user serializer fields --- api/base/serializers.py | 17 +++++++++++++++++ api/institutions/serializers.py | 27 ++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/api/base/serializers.py b/api/base/serializers.py index 54b2b032291..b843e97888c 100644 --- a/api/base/serializers.py +++ b/api/base/serializers.py @@ -1,4 +1,5 @@ import collections +import datetime import re from urllib.parse import urlparse @@ -17,6 +18,7 @@ from api.base import utils from api.base.exceptions import EnumFieldMemberError +from osf.metrics.utils import YearMonth from osf.utils import permissions as osf_permissions from osf.utils import sanitize from osf.utils import functional @@ -2024,3 +2026,18 @@ def to_internal_value(self, data): return self._enum_class[data.upper()].value except KeyError: raise EnumFieldMemberError(self._enum_class, data) + + +class YearmonthField(ser.Field): + def to_representation(self, value: YearMonth | None) -> str | None: + if value is None: + return None + return str(value) + + def to_internal_value(self, data: str | None) -> YearMonth | None: + if data is None: + return None + try: + return YearMonth.from_str(data) + except ValueError as e: + raise ser.ValidationError(str(e)) diff --git a/api/institutions/serializers.py b/api/institutions/serializers.py index 7d84f734542..9e161fb5d52 100644 --- a/api/institutions/serializers.py +++ b/api/institutions/serializers.py @@ -15,6 +15,7 @@ ShowIfObjectPermission, ) +from api.base.serializers import YearmonthField from api.nodes.serializers import CompoundIDField from api.base.exceptions import RelationshipPostMakesNoChanges from api.base.utils import absolute_reverse @@ -313,4 +314,28 @@ class NewInstitutionUserMetricsSerializer(JSONAPISerializer): class Meta: type_ = 'institution-users' - ... # TODO: serializer fields + id = IDField(source='meta.id', read_only=True) + user_name = ser.CharField(read_only=True) + department = ser.CharField(read_only=True, source='department_name') + orcid_id = ser.CharField(read_only=True) + month_last_login = YearmonthField(read_only=True) + account_creation_date = YearmonthField(read_only=True) + + public_projects = ser.IntegerField(read_only=True, source='public_project_count') + private_projects = ser.IntegerField(read_only=True, source='private_project_count') + public_registration_count = ser.IntegerField(read_only=True) + embargoed_registration_count = ser.IntegerField(read_only=True) + published_preprint_count = ser.IntegerField(read_only=True) + public_file_count = ser.IntegerField(read_only=True) + storage_byte_count = ser.IntegerField(read_only=True) + + user = RelationshipField( + related_view='users:user-detail', + related_view_kwargs={'user_id': ''}, + ) + institution = RelationshipField( + related_view='institutions:institution-detail', + related_view_kwargs={'institution_id': ''}, + ) + + links = LinksField({})