Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions isic/engagement/api.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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(
Expand All @@ -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)
28 changes: 26 additions & 2 deletions isic/engagement/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down