Skip to content

Commit

Permalink
Merge pull request #288 from MetaCell/feature/CSCKAN-290
Browse files Browse the repository at this point in the history
Feature/csckan-290 - change region/layer model to be independent models from AEMeta - and related changes
  • Loading branch information
ddelpiano authored Jul 18, 2024
2 parents 4b3e745 + 6633c8a commit 82c22ad
Show file tree
Hide file tree
Showing 16 changed files with 603 additions and 89 deletions.
70 changes: 59 additions & 11 deletions backend/composer/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ class SentenceAdmin(
)

class AnatomicalEntityAdmin(admin.ModelAdmin):
search_fields = ('simple_entity__name', 'region_layer__layer__name', 'region_layer__region__name')
autocomplete_fields = ('simple_entity', 'region_layer')
list_display = ('simple_entity', 'region_layer', "synonyms", "ontology_uri")
list_display = ('simple_entity', 'region_layer', 'synonyms', 'ontology_uri' )
list_display_links = ('simple_entity', 'region_layer')
search_fields = (
'simple_entity__name', 'simple_entity__ontology_uri',
'region_layer__layer__name', 'region_layer__region__name',
'region_layer__layer__ontology_uri', 'region_layer__region__ontology_uri',
)
inlines = (SynonymInline,)
autocomplete_fields = ('simple_entity', 'region_layer')

# we need to make efficient queries to the database to get the list of anatomical entities
def get_queryset(self, request: HttpRequest) -> QuerySet[Any]:
Expand Down Expand Up @@ -130,22 +134,66 @@ def get_model_perms(self, request):
return {}


class LayerAdminForm(forms.ModelForm):
class Meta:
model = Layer
fields = '__all__'
labels = {
'ae_meta': 'layer',
}


class RegionAdminForm(forms.ModelForm):
class Meta:
model = Region
fields = '__all__'
labels = {
'ae_meta': 'region',
}


class LayerAdmin(admin.ModelAdmin):
list_display = ('name', 'ontology_uri',)
search_fields = ('name',)
form = LayerAdminForm
list_display = ('layer_name', 'ontology_uri')
list_display_links = ('layer_name', 'ontology_uri')
search_fields = ('ae_meta__name', 'ae_meta__ontology_uri')
autocomplete_fields = ('ae_meta',)

@admin.display(description="Layer Name")
def layer_name(self, obj):
return obj.ae_meta.name

@admin.display(description="Ontology URI")
def ontology_uri(self, obj):
return obj.ae_meta.ontology_uri



class RegionAdmin(admin.ModelAdmin):
list_display = ('name', 'ontology_uri',)
search_fields = ('name',)
filter_horizontal = ('layers',)
form = RegionAdminForm
list_display = ('region_name', 'ontology_uri')
list_display_links = ('region_name', 'ontology_uri')
search_fields = ('ae_meta__name', 'ae_meta__ontology_uri')
autocomplete_fields = ('ae_meta',)

@admin.display(description="Region Name")
def region_name(self, obj):
return obj.ae_meta.name

@admin.display(description="Ontology URI")
def ontology_uri(self, obj):
return obj.ae_meta.ontology_uri



class AnatomicalEntityIntersectionAdmin(nested_admin.NestedModelAdmin, admin.ModelAdmin):
list_display = ('layer', 'region')
search_fields = ("region__name", "layer__name")
list_filter = ('layer', 'region',)
raw_id_fields = ('layer', 'region',)
raw_id_fields = ('layer', 'region')
list_filter = ('layer', 'region')
search_fields = (
'layer__ae_meta__name', 'region__ae_meta__name',
'layer__ae_meta__ontology_uri', 'region__ae_meta__ontology_uri'
)

def get_model_perms(self, request):
return {}
Expand Down
27 changes: 2 additions & 25 deletions backend/composer/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,6 @@ class Meta:
dept = 2


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 = Region
fields = (
"id",
"name",
"ontology_uri",
"layers",
)


class AnatomicalEntityMetaSerializer(serializers.ModelSerializer):
class Meta:
model = AnatomicalEntityMeta
Expand All @@ -115,8 +92,8 @@ class Meta:


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

class Meta:
model = AnatomicalEntityIntersection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Generated by Django 4.1.4 on 2024-07-08 13:18

from django.db import migrations, models
import django.db.models.deletion


def populate_intersection_anatomical_entity_meta_id_from_layer_region(apps, schema_editor):
"""
populating the new fields from AnatomicalEntityIntersection
layer_id -> layer_meta_id
region_id -> region_meta_id
"""
AnatomicalEntityIntersection = apps.get_model("composer", "AnatomicalEntityIntersection")
for intersection in AnatomicalEntityIntersection.objects.all():
intersection.layer_meta_id = intersection.layer_id
intersection.region_meta_id = intersection.region_id
intersection.save()


def populate_layer_region_ae_meta_ids_from_anatomicalentitymeta_ptr_id(apps, schema_editor):
"""
populating the new fields from Layer and Region
anatomicalentitymeta_ptr_id -> layer_ae_meta_id
anatomicalentitymeta_ptr_id -> region_ae_meta_id
"""
Layer = apps.get_model("composer", "Layer")
Region = apps.get_model("composer", "Region")

for layer in Layer.objects.all():
layer.layer_ae_meta_id = layer.anatomicalentitymeta_ptr_id
layer.save()

for region in Region.objects.all():
region.region_ae_meta_id = region.anatomicalentitymeta_ptr_id
region.save()


class Migration(migrations.Migration):
dependencies = [
("composer", "0052_alter_anatomicalentity_options_and_more"),
]

# Steps taken in migration:
# 1. create new fields for layer and region - layer_ae_meta, region_ae_meta
# 2. create new fields for AnatomicalEntityIntersection - layer_meta, region_meta
# 3. populate the new fields in AnatomicalEntityIntersection from the old fields - layer, region
# 4. populate the new fields in Layer and Region from the old fields - anatomicalentitymeta_ptr_id
operations = [
migrations.AlterModelOptions(
name="anatomicalentity",
options={
"verbose_name": "Anatomical Entity",
"verbose_name_plural": "Anatomical Entities",
},
),

migrations.AddField(
model_name="layer",
name="layer_ae_meta",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="layer_meta",
to="composer.anatomicalentitymeta",
),
),
migrations.AddField(
model_name="region",
name="region_ae_meta",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="region_meta",
to="composer.anatomicalentitymeta",
),
),

# ------------------------
migrations.AddField(
model_name="anatomicalentityintersection",
name="layer_meta",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="layer_intersection",
to="composer.anatomicalentitymeta",
),
),
migrations.AddField(
model_name="anatomicalentityintersection",
name="region_meta",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="region_intersection",
to="composer.anatomicalentitymeta",
),
),

# ------------------------

migrations.RunPython(
code=populate_intersection_anatomical_entity_meta_id_from_layer_region,
reverse_code=migrations.RunPython.noop,
),

# ------------------------

migrations.RunPython(
code=populate_layer_region_ae_meta_ids_from_anatomicalentitymeta_ptr_id,
reverse_code=migrations.RunPython.noop,
),

]


104 changes: 104 additions & 0 deletions backend/composer/migrations/0054_auto_20240709_0909.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Generated by Django 4.1.4 on 2024-07-09 07:09

from django.db import migrations, models
import django.db.models.deletion





class Migration(migrations.Migration):

dependencies = [
("composer", "0053_alter_anatomicalentity_options_and_more"),
]

# Steps taken in migration:
# 1. create new models for layer and region - NewLayer, NewRegion
# 2. copy the necessary columns from the old models to the new models - layer_ae_meta, region_ae_meta
# 3. remove the old models - Layer, Region and it's fk from AnatomicalEntityIntersection
# 4. rename the new models to the old models - Layer, Region

operations = [
migrations.CreateModel(
name="NewLayer",
fields=[
(
"layer_id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
),
),
(
"layer_ae_meta",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="layer_meta",
to="composer.anatomicalentitymeta",
),
)
]
),
migrations.RunSQL(
sql="INSERT INTO composer_newlayer (layer_ae_meta_id) SELECT anatomicalentitymeta_ptr_id FROM composer_layer",
reverse_sql="",
),
migrations.RemoveField(
model_name="anatomicalentityintersection",
name="layer",
),
migrations.DeleteModel(
name="Layer",
),
migrations.RenameModel(
old_name="NewLayer",
new_name="Layer",
),


# ----------------------------
migrations.CreateModel(
name="NewRegion",
fields=[
(
"region_id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
),
),
(
"region_ae_meta",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="region_meta",
to="composer.anatomicalentitymeta",
),
)
]
),
migrations.RunSQL(
sql="INSERT INTO composer_newregion (region_ae_meta_id) SELECT anatomicalentitymeta_ptr_id FROM composer_region",
reverse_sql="",
),
migrations.RemoveField(
model_name="anatomicalentityintersection",
name="region",
),
migrations.DeleteModel(
name="Region",
),
migrations.RenameModel(
old_name="NewRegion",
new_name="Region",
),




]
31 changes: 31 additions & 0 deletions backend/composer/migrations/0055_auto_20240708_1540.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.1.4 on 2024-07-08 13:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("composer", "0054_auto_20240709_0909"),
]

operations = [
migrations.AlterModelOptions(
name="layer",
options={"verbose_name": "Layer", "verbose_name_plural": "Layers"},
),
migrations.AlterModelOptions(
name="region",
options={"verbose_name": "Region", "verbose_name_plural": "Regions"},
),
migrations.RenameField(
model_name="anatomicalentityintersection",
old_name="layer_meta",
new_name="layer",
),
migrations.RenameField(
model_name="anatomicalentityintersection",
old_name="region_meta",
new_name="region",
),
]
Loading

0 comments on commit 82c22ad

Please sign in to comment.