From f94007aeacd0ae5ea02104d2fa07cded3c2632a8 Mon Sep 17 00:00:00 2001 From: Dan LaManna Date: Thu, 6 Aug 2026 13:09:28 -0400 Subject: [PATCH] Populate engagement profile details --- isic/engagement/api.py | 19 +++++++++++++------ isic/engagement/tests/test_api.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/isic/engagement/api.py b/isic/engagement/api.py index ef05fbfb..e10ac179 100644 --- a/isic/engagement/api.py +++ b/isic/engagement/api.py @@ -1,8 +1,10 @@ +from django.db.models import Prefetch from django.shortcuts import get_object_or_404 from ninja import ModelSchema, Router from isic.auth import is_authenticated from isic.engagement.models import EngagementProfile +from isic.ingest.api import CohortOut, ContributorOut, default_cohort_qs from isic.types import AuthenticatedHttpRequest router = Router() @@ -11,11 +13,10 @@ class EngagementProfileOut(ModelSchema): class Meta: model = EngagementProfile - fields = [ - "created", - "default_contributor", - "default_cohort", - ] + fields = ["created"] + + default_contributor: ContributorOut | None + default_cohort: CohortOut | None @router.get( @@ -28,4 +29,10 @@ class Meta: auth=is_authenticated, ) def engagement_profile(request: AuthenticatedHttpRequest): - return get_object_or_404(EngagementProfile, user=request.user) + qs = EngagementProfile.objects.prefetch_related( + "default_contributor__owners", + # the cohort is prefetched rather than select_related so it carries the accession_count + # annotation CohortOut expects. + Prefetch("default_cohort", queryset=default_cohort_qs), + ) + return get_object_or_404(qs, user=request.user) diff --git a/isic/engagement/tests/test_api.py b/isic/engagement/tests/test_api.py index 602064dc..e5fab948 100644 --- a/isic/engagement/tests/test_api.py +++ b/isic/engagement/tests/test_api.py @@ -9,8 +9,32 @@ def test_engagement_api_profile(authenticated_client, user, engagement_profile_f r = authenticated_client.get(reverse("api:engagement_profile")) assert r.status_code == 200, r.json() - assert r.json()["default_contributor"] == profile.default_contributor.pk - assert r.json()["default_cohort"] == profile.default_cohort.pk + + contributor_json = r.json()["default_contributor"] + assert contributor_json.pop("created") + assert contributor_json == { + "id": profile.default_contributor.pk, + "creator": profile.default_contributor.creator_id, + "owners": [owner.pk for owner in profile.default_contributor.owners.all()], + "institution_name": profile.default_contributor.institution_name, + "institution_url": profile.default_contributor.institution_url, + "legal_contact_info": profile.default_contributor.legal_contact_info, + "default_copyright_license": profile.default_contributor.default_copyright_license, + "default_attribution": profile.default_contributor.default_attribution, + } + + cohort_json = r.json()["default_cohort"] + assert cohort_json.pop("created") + assert cohort_json == { + "id": profile.default_cohort.pk, + "creator": profile.default_cohort.creator_id, + "contributor": profile.default_cohort.contributor_id, + "name": profile.default_cohort.name, + "description": profile.default_cohort.description, + "default_copyright_license": profile.default_cohort.default_copyright_license, + "default_attribution": profile.default_cohort.default_attribution, + "accession_count": profile.default_cohort.accessions.count(), + } @pytest.mark.django_db