Skip to content

Commit

Permalink
factor out computed extra_property code
Browse files Browse the repository at this point in the history
  • Loading branch information
v-rocheleau committed Jan 4, 2024
1 parent 0d161af commit 3b5a27f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions chord_metadata_service/chord/ingest/phenopackets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from chord_metadata_service.patients.values import KaryotypicSex
from chord_metadata_service.restapi.schema_utils import patch_project_schemas
from chord_metadata_service.restapi.types import ExtensionSchemaDict
from chord_metadata_service.restapi.utils import time_element_to_years
from chord_metadata_service.restapi.utils import COMPUTED_PROPERTY_PREFIX, time_element_to_years

from .exceptions import IngestError
from .resources import ingest_resource
Expand Down Expand Up @@ -40,7 +40,7 @@ def _clean_extra_properties(extra_properties: dict) -> Dict:
Computed extra_properties start with "__" and should never be ingested.
"""
if extra_properties:
return {k: v for k, v in extra_properties.items() if not k.startswith("__")}
return {k: v for k, v in extra_properties.items() if not k.startswith(COMPUTED_PROPERTY_PREFIX)}
return extra_properties


Expand Down
7 changes: 5 additions & 2 deletions chord_metadata_service/phenopackets/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from rest_framework import serializers

from chord_metadata_service.restapi.utils import computed_property
from .models import (
MetaData,
PhenotypicFeature,
Expand Down Expand Up @@ -163,12 +165,13 @@ def to_representation(self, instance):
# The '__related_type' property is added to extra_properties as a computed value ("__" prefix)
# This allows us to disambiguate on the client side for links
extra_properties = response.get("extra_properties", {})
computed_related_type = computed_property("related_type")
if instance.subject:
response["subject_or_biosample_id"] = instance.subject.id
extra_properties["__related_type"] = "subject"
extra_properties[computed_related_type] = "subject"
elif instance.biosample:
response["subject_or_biosample_id"] = instance.biosample.id
extra_properties["__related_type"] = "biosample"
extra_properties[computed_related_type] = "biosample"

response["extra_properties"] = extra_properties
return response
Expand Down
9 changes: 9 additions & 0 deletions chord_metadata_service/restapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"biosample": pheno_models.Biosample,
}

COMPUTED_PROPERTY_PREFIX = "__"


class BinWithValue(TypedDict):
label: str
Expand Down Expand Up @@ -691,3 +693,10 @@ def bento_public_format_count_and_stats_list(annotated_queryset) -> tuple[int, l
stats_list.append({"label": label, "value": value})

return total, stats_list


def computed_property(name: str):
"""
Takes a name and returns it prefixed with "__"
"""
return COMPUTED_PROPERTY_PREFIX + name

0 comments on commit 3b5a27f

Please sign in to comment.