From 3b5a27f3e8481fd9a11f7264f7ba3d5efa2476d4 Mon Sep 17 00:00:00 2001 From: Victor Rocheleau Date: Thu, 4 Jan 2024 21:56:12 +0000 Subject: [PATCH] factor out computed extra_property code --- chord_metadata_service/chord/ingest/phenopackets.py | 4 ++-- chord_metadata_service/phenopackets/serializers.py | 7 +++++-- chord_metadata_service/restapi/utils.py | 9 +++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/chord_metadata_service/chord/ingest/phenopackets.py b/chord_metadata_service/chord/ingest/phenopackets.py index b2ecf0848..03353dcd8 100644 --- a/chord_metadata_service/chord/ingest/phenopackets.py +++ b/chord_metadata_service/chord/ingest/phenopackets.py @@ -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 @@ -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 diff --git a/chord_metadata_service/phenopackets/serializers.py b/chord_metadata_service/phenopackets/serializers.py index 868f44d89..04ad56408 100644 --- a/chord_metadata_service/phenopackets/serializers.py +++ b/chord_metadata_service/phenopackets/serializers.py @@ -1,4 +1,6 @@ from rest_framework import serializers + +from chord_metadata_service.restapi.utils import computed_property from .models import ( MetaData, PhenotypicFeature, @@ -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 diff --git a/chord_metadata_service/restapi/utils.py b/chord_metadata_service/restapi/utils.py index 28be837a9..010b8fa0b 100644 --- a/chord_metadata_service/restapi/utils.py +++ b/chord_metadata_service/restapi/utils.py @@ -25,6 +25,8 @@ "biosample": pheno_models.Biosample, } +COMPUTED_PROPERTY_PREFIX = "__" + class BinWithValue(TypedDict): label: str @@ -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