Skip to content

Commit

Permalink
Merge pull request #394 from MetaCell/feature/SCKAN-373
Browse files Browse the repository at this point in the history
Updates from_entities via deletions, anatomical entities removals or origin removals
  • Loading branch information
ddelpiano authored Dec 17, 2024
2 parents d9ec15b + 9d48c2e commit 269aa58
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
16 changes: 13 additions & 3 deletions backend/composer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.forms.widgets import Input as InputWidget
from django_fsm import FSMField, transition

from composer.services.layers_service import update_from_entities_on_deletion
from composer.services.state_services import (
ConnectivityStatementStateService,
SentenceStateService,
Expand Down Expand Up @@ -855,12 +856,21 @@ def _update_order_for_other_vias(self, old_order):

def delete(self, *args, **kwargs):
with transaction.atomic():
# Collect the IDs of the anatomical entities from 'from_entities'
anatomical_entities_ids = list(self.anatomical_entities.values_list("id", flat=True))

# Call update_from_entities_on_deletion for each entity ID
for entity_id in anatomical_entities_ids:
update_from_entities_on_deletion(self.connectivity_statement, entity_id)

# Proceed with the deletion
super().delete(*args, **kwargs)

# Update the order of remaining 'Via' instances
vias_to_update = Via.objects.filter(
connectivity_statement=self.connectivity_statement,
order__gt=self.order
connectivity_statement=self.connectivity_statement, order__gt=self.order
)
vias_to_update.update(order=F('order') - 1)
vias_to_update.update(order=F("order") - 1)

class Meta:
ordering = ["order"]
Expand Down
25 changes: 21 additions & 4 deletions backend/composer/signals.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.core.exceptions import ValidationError
from django.dispatch import receiver
from django.db.models.signals import post_save, m2m_changed, pre_save, post_delete
from django.db.models.signals import post_save, m2m_changed, post_delete
from django.contrib.auth import get_user_model
from django_fsm.signals import post_transition

from composer.services.layers_service import update_from_entities_on_deletion
from .utils import update_modified_date
from .enums import CSState, NoteType
from .models import (
Expand Down Expand Up @@ -86,7 +87,13 @@ def delete_associated_entities(sender, instance, **kwargs):

# Signals for ConnectivityStatement origins
@receiver(m2m_changed, sender=ConnectivityStatement.origins.through)
def connectivity_statement_origins_changed(sender, instance, action, **kwargs):
def connectivity_statement_origins_changed(sender, instance, action, pk_set, **kwargs):
"""
Signal handler for changes in the origins ManyToMany relationship.
- Deletes the graph_rendering_state on 'post_add', 'post_remove', or 'post_clear'.
- Calls `update_from_entities_on_deletion` for each deleted entity on 'post_remove'.
"""
if action in ["post_add", "post_remove", "post_clear"]:
try:
instance.graph_rendering_state.delete()
Expand All @@ -95,10 +102,15 @@ def connectivity_statement_origins_changed(sender, instance, action, **kwargs):
except ValueError:
pass

# Call `update_from_entities_on_deletion` for each removed entity
if action == "post_remove" and pk_set:
for deleted_entity_id in pk_set:
update_from_entities_on_deletion(instance, deleted_entity_id)


# Signals for Via anatomical_entities
@receiver(m2m_changed, sender=Via.anatomical_entities.through)
def via_anatomical_entities_changed(sender, instance, action, **kwargs):
def via_anatomical_entities_changed(sender, instance, action, pk_set, **kwargs):
if action in ["post_add", "post_remove", "post_clear"]:
try:
instance.connectivity_statement.graph_rendering_state.delete()
Expand All @@ -107,6 +119,11 @@ def via_anatomical_entities_changed(sender, instance, action, **kwargs):
except ValueError:
pass

# Call `update_from_entities_on_deletion` for each removed entity
if action == "post_remove" and pk_set:
for deleted_entity_id in pk_set:
update_from_entities_on_deletion(instance.connectivity_statement, deleted_entity_id)


# Signals for Via from_entities
@receiver(m2m_changed, sender=Via.from_entities.through)
Expand Down

0 comments on commit 269aa58

Please sign in to comment.