Skip to content

Commit

Permalink
add institution-user serializer fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Sep 9, 2024
1 parent df124f9 commit a89f5bc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/base/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections
import datetime
import re
from urllib.parse import urlparse

Expand All @@ -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
Expand Down Expand Up @@ -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))
27 changes: 26 additions & 1 deletion api/institutions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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': '<user_id>'},
)
institution = RelationshipField(
related_view='institutions:institution-detail',
related_view_kwargs={'institution_id': '<institution_id>'},
)

links = LinksField({})

0 comments on commit a89f5bc

Please sign in to comment.