Skip to content

Commit

Permalink
Ensure part tiles are removed post deletion (#1588)
Browse files Browse the repository at this point in the history
* Move signals out of models.py per django docs recommendation

* Delete part tile after relationship is deleted

* nit

* Add changelog entry

* No need to delete tile as its now handled in a reciever

* nit
  • Loading branch information
chiatt authored Dec 3, 2024
1 parent 49b0917 commit 34795d5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
1 change: 1 addition & 0 deletions arches_for_science/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ class ArchesForScienceConfig(AppConfig):

def ready(self):
generate_frontend_configuration()
from . import signals
19 changes: 1 addition & 18 deletions arches_for_science/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import uuid

from arches.app.models.models import IIIFManifest, TileModel, FunctionXGraph
from arches.app.models.models import TileModel, FunctionXGraph
from django.db import models
from django.db.models import JSONField
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
import pgtrigger

from .trigger_functions import CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_SINGLE, CALCULATE_MULTICARD_PRIMARY_DESCRIPTOR_ALL
Expand Down Expand Up @@ -82,17 +79,3 @@ class ManifestXCanvas(models.Model):
class Meta:
managed = True
db_table = "manifest_x_canvas"


@receiver(post_save, sender=IIIFManifest)
def create_digital_resources(sender, instance, created, **kwargs):
from arches_for_science.utils.digital_resource_for_manifest import digital_resources_for_manifest, digital_resources_for_canvases

digital_resources_for_manifest(instance, created)
digital_resources_for_canvases(instance)


@receiver(post_delete, sender=IIIFManifest)
def delete_manifest_x_canvas(sender, instance, **kwargs):
ManifestXCanvas.objects.filter(manifest=instance.manifest["@id"]).delete()
ManifestXDigitalResource.objects.filter(manifest=instance.manifest["@id"]).delete()
30 changes: 30 additions & 0 deletions arches_for_science/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from arches.app.models.models import IIIFManifest, ResourceXResource
from arches.app.models.resource import Resource
from .models import ManifestXCanvas, ManifestXDigitalResource
import uuid


@receiver(post_save, sender=IIIFManifest)
def create_digital_resources(sender, instance, created, **kwargs):
from arches_for_science.utils.digital_resource_for_manifest import digital_resources_for_manifest, digital_resources_for_canvases

digital_resources_for_manifest(instance, created)
digital_resources_for_canvases(instance)


@receiver(post_delete, sender=IIIFManifest)
def delete_manifest_x_canvas(sender, instance, **kwargs):
ManifestXCanvas.objects.filter(manifest=instance.manifest["@id"]).delete()
ManifestXDigitalResource.objects.filter(manifest=instance.manifest["@id"]).delete()


@receiver(post_delete, sender=ResourceXResource)
def ensure_part_tile_is_deleted(sender, instance, **kwargs):
physical_thing_parts_node_id = uuid.UUID("b240c366-8594-11ea-97eb-acde48001122")
if instance.nodeid_id == physical_thing_parts_node_id:
instance.tileid.delete()
resourceid = instance.tileid.resourceinstance_id
resource = Resource.objects.get(pk=resourceid)
resource.index()
20 changes: 1 addition & 19 deletions arches_for_science/views/analysis_area_and_sample_taking.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ def post(self, request):
# need to delete:
# the "sample" physical thing
# the "sample area" physical thing
# the tile from the parent physical thing that references the "sample area"
# the tile from the sampling activity that references the "sample area"

data = JSONDeserializer().deserialize(request.body)
Expand All @@ -540,7 +539,6 @@ def post(self, request):
sampling_area_sample_created_nodeid = "b3e171ab-1d9d-11eb-a29f-024e0d439fdb"
sampling_unit_nodegroupid = "b3e171a7-1d9d-11eb-a29f-024e0d439fdb"

parent_physical_thing_resourceid = data.get("parentPhysicalThingResourceid")
sampling_activity_resourceid = data.get("samplingActivityResourceId")
collection_resourceid = data.get("collectionResourceid")
sample_motivation = data.get("sampleMotivation")
Expand All @@ -561,12 +559,6 @@ def post(self, request):
if sampling_unit_tile.data[sampling_area_nodeid][0]["resourceId"] == sample_area_physical_thing_resourceid:
sample_physical_thing_resourceid = sampling_unit_tile.data[sampling_area_sample_created_nodeid][0]["resourceId"]

parentPhysicalThingSampleTile = ResourceXResource.objects.get(
nodeid=physical_part_of_object_nodeid,
resourceinstanceidfrom_id=parent_physical_thing_resourceid,
resourceinstanceidto_id=sample_area_physical_thing_resourceid,
)

samplingActivitySampleTile = ResourceXResource.objects.get(
nodeid=sampling_area_nodeid,
resourceinstanceidfrom_id=sampling_activity_resourceid,
Expand All @@ -580,7 +572,7 @@ def post(self, request):
Resource.objects.get(resourceinstanceid=sample_physical_thing_resourceid).delete(
transaction_id=transaction_id, user=request.user
)
Tile.objects.get(tileid=parentPhysicalThingSampleTile.tileid_id).delete(transaction_id=transaction_id, request=request)

Tile.objects.get(tileid=samplingActivitySampleTile.tileid_id).delete(transaction_id=transaction_id, request=request)
return JSONResponse(status=200)
except:
Expand All @@ -593,11 +585,8 @@ class DeleteAnalysisAreaView(View):
def post(self, request):
# need to delete:
# the "analysis" physical thing
# the tile from the parent physical thing that references the "sample area"

data = JSONDeserializer().deserialize(request.body)

parent_physical_thing_resourceid = data.get("parentPhysicalThingResourceId")
part_identifier_assignment_tile_data = JSONDeserializer().deserialize(data.get("parentPhysicalThingTileData"))
transaction_id = data.get("transactionId")

Expand All @@ -608,17 +597,10 @@ def post(self, request):
analysis_area_physical_thing_resourceid = part_identifier_assignment_tile_data[physical_part_of_object_nodeid][0]["resourceId"]

try:
parentPhysicalThingAnalysisTile = ResourceXResource.objects.get(
nodeid=physical_part_of_object_nodeid,
resourceinstanceidfrom_id=parent_physical_thing_resourceid,
resourceinstanceidto_id=analysis_area_physical_thing_resourceid,
)

with transaction.atomic():
Resource.objects.get(resourceinstanceid=analysis_area_physical_thing_resourceid).delete(
transaction_id=transaction_id, user=request.user
)
Tile.objects.get(tileid=parentPhysicalThingAnalysisTile.tileid_id).delete(transaction_id=transaction_id, request=request)
return JSONResponse(status=200)
except:
response = {"message": _("Unable to delete"), "title": _("Delete Failed")}
Expand Down
3 changes: 2 additions & 1 deletion releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

### Bug Fixes and Enhancements

- Upgrades Arches for Science to work with the latest Arches 7.6.x release #[1570](https://github.com/archesproject/arches-for-science/pull/1570)
- Upgrades Arches for Science to work with the latest Arches 7.6.x release [#1570](https://github.com/archesproject/arches-for-science/pull/1570)
- Automatically delete part tiles when analysis and sample areas are deleted [#1588](https://github.com/archesproject/arches-for-science/pull/1588)

### Dependency changes:

Expand Down
1 change: 0 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def make_tile(self, parent_phys_thing, data, transaction_id):
new_tile.data = data
new_tile.save(transaction_id=transaction_id)

self.addCleanup(new_tile.delete)
return new_tile

def test_create_delete_analysis_area(self):
Expand Down

0 comments on commit 34795d5

Please sign in to comment.