From d0e93df7aac6442c8a6ac520029a7ace2d6b4dc4 Mon Sep 17 00:00:00 2001 From: Jacky Zhang Date: Tue, 4 Jul 2023 14:49:06 -0400 Subject: [PATCH] Delete generated resources when workflow run is deleted --- rodan-main/code/rodan/models/resource.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rodan-main/code/rodan/models/resource.py b/rodan-main/code/rodan/models/resource.py index 422cf1547..b2de6f97e 100644 --- a/rodan-main/code/rodan/models/resource.py +++ b/rodan-main/code/rodan/models/resource.py @@ -6,7 +6,10 @@ from django.urls import reverse from rodan.constants import task_status from rodan.models.resourcelabel import ResourceLabel +from rodan.models.output import Output from rodan.models.user import User +from django.db.models.signals import post_delete +from django.dispatch import receiver import logging @@ -298,3 +301,12 @@ def get_viewer(self): def viewer_relurl(self): if self.get_viewer() is not None: return reverse("resource-viewer-acquire", args=(self.uuid,)) + +@receiver(post_delete, sender=Output) +def post_output_delete(sender, instance, **kwargs): + """ + Deletes resource when associated output is deleted. Cascade delete does not work due to on_delete=PROTECT in Output model. + In newer Django versions, we can use on_delete=models.RESTRICT in Output model to allow cascade delete to work. + """ + if instance.resource: + instance.resource.delete()