Skip to content

Commit

Permalink
SCKAN-275 feat: Add basic serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Mar 20, 2024
1 parent 40f20b9 commit 75a980b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
59 changes: 52 additions & 7 deletions backend/composer/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Sentence,
Specie,
Tag,
Via, Destination,
Via, Destination, AnatomicalEntityIntersection, Region, Layer, AnatomicalEntityMeta,
)
from ..services.connections_service import get_complete_from_entities_for_destination, \
get_complete_from_entities_for_via
Expand Down Expand Up @@ -80,17 +80,63 @@ class Meta:
dept = 2


class AnatomicalEntitySerializer(UniqueFieldsMixin, serializers.ModelSerializer):
"""Anatomical Entity"""
class LayerSerializer(serializers.ModelSerializer):
class Meta:
model = Layer
fields = (
"id",
"name",
"ontology_uri",
)


class RegionSerializer(serializers.ModelSerializer):
layers = LayerSerializer(many=True, read_only=True)

class Meta:
model = AnatomicalEntity
model = Region
fields = (
"id",
"name",
"ontology_uri",
"layers",
)


class AnatomicalEntityMetaSerializer(serializers.ModelSerializer):
class Meta:
model = AnatomicalEntityMeta
fields = (
"id",
"name",
"ontology_uri",
)
read_only_fields = ("ontology_uri",)


class AnatomicalEntityIntersectionSerializer(serializers.ModelSerializer):
layer = LayerSerializer(read_only=True)
region = RegionSerializer(read_only=True)

class Meta:
model = AnatomicalEntityIntersection
fields = (
"id",
"layer",
"region",
)


class AnatomicalEntitySerializer(serializers.ModelSerializer):
simple_entity = AnatomicalEntityMetaSerializer(read_only=True)
region_layer = AnatomicalEntityIntersectionSerializer(read_only=True)

class Meta:
model = AnatomicalEntity
fields = (
"id",
"simple_entity",
"region_layer",
)


class NoteSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -486,7 +532,6 @@ def get_statement_preview(self, instance):
self.context['journey'] = instance.get_journey()
return self.create_statement_preview(instance, self.context['journey'])


def create_statement_preview(self, instance, journey):
sex = instance.sex.sex_str if instance.sex else None

Expand Down Expand Up @@ -514,7 +559,7 @@ def create_statement_preview(self, instance, journey):
statement = f"In {sex or ''} {species}, the {phenotype.lower()} connection goes {journey_sentence}.\n"
else:
statement = f"A {phenotype.lower()} connection goes {journey_sentence}.\n"

statement += f"This "
if projection:
statement += f"{projection.lower()} "
Expand Down
11 changes: 11 additions & 0 deletions backend/composer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ class AnatomicalEntity(models.Model):
class Meta:
verbose_name_plural = "Anatomical Entities"

@property
def name(self):
if self.simple_entity:
return self.simple_entity.name
elif self.region_layer:
layer_name = self.region_layer.layer.name if self.region_layer.layer else "No Layer"
region_name = self.region_layer.region.name if self.region_layer.region else "No Region"
return f"{region_name},{layer_name}"
else:
return "Unnamed Entity"


class Synonym(models.Model):
anatomical_entity = models.ForeignKey(AnatomicalEntity, on_delete=models.CASCADE,
Expand Down

0 comments on commit 75a980b

Please sign in to comment.