Skip to content

Commit

Permalink
feat(patients): add individual phenopackets endpoint w/ attachment op…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
davidlougheed committed Sep 12, 2023
1 parent 1b0dfed commit 7639387
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion chord_metadata_service/patients/api_views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import re

from datetime import datetime

from rest_framework import viewsets, filters, mixins, serializers
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.settings import api_settings
from rest_framework.views import APIView
from django.conf import settings
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
Expand All @@ -22,6 +25,7 @@
from chord_metadata_service.logger import logger
from chord_metadata_service.phenopackets.api_views import BIOSAMPLE_PREFETCH, PHENOPACKET_PREFETCH
from chord_metadata_service.phenopackets.models import Phenopacket
from chord_metadata_service.phenopackets.serializers import PhenopacketSerializer
from chord_metadata_service.restapi.api_renderers import (
FHIRRenderer,
PhenopacketsRenderer,
Expand Down Expand Up @@ -97,6 +101,27 @@ def list(self, request, *args, **kwargs):

return super(IndividualViewSet, self).list(request, *args, **kwargs)

@action(detail=True, methods=["GET"])
def phenopackets(self, request, *_args, **_kwargs):
as_attachment = request.query_params.get("attachment", "") in ("1", "true", "yes")
individual = self.get_object()

Check warning on line 107 in chord_metadata_service/patients/api_views.py

View check run for this annotation

Codecov / codecov/patch

chord_metadata_service/patients/api_views.py#L106-L107

Added lines #L106 - L107 were not covered by tests

phenopackets = (

Check warning on line 109 in chord_metadata_service/patients/api_views.py

View check run for this annotation

Codecov / codecov/patch

chord_metadata_service/patients/api_views.py#L109

Added line #L109 was not covered by tests
Phenopacket.objects
.filter(subject=individual)
.prefetch_related(*PHENOPACKET_PREFETCH)
.order_by("id")
)

filename_safe_id = re.sub(r"[\\/:*?\"<>|]", "_", individual.id)
return Response(

Check warning on line 117 in chord_metadata_service/patients/api_views.py

View check run for this annotation

Codecov / codecov/patch

chord_metadata_service/patients/api_views.py#L116-L117

Added lines #L116 - L117 were not covered by tests
PhenopacketSerializer(phenopackets, many=True).data,
headers=(
{"Content-Disposition": f"attachment; filename=\"{filename_safe_id}_phenopackets.json\""}
if as_attachment else {}
),
)


class BatchViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
Expand Down

0 comments on commit 7639387

Please sign in to comment.