Skip to content

Commit 9e9f1e6

Browse files
committed
Clean orphaned permissions after target delete
Cleans all orphaned Django-Guardian permissions after a Target is deleted. This makes sense to do here as Targets can have row level (private) permissions. We could potentially run this as well for dataproducts, though I'm less versed on how it works there. Either way, there's no reason to keep these objects around for any model, so it's fine to delete permissions that aren't associated with Targets. Alternatively, we don't do any cleanup and leave it to the TOM developer to run the management command manually: https://django-guardian.readthedocs.io/en/stable/api/management/
1 parent f25c323 commit 9e9f1e6

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

tom_targets/signals/handlers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django.dispatch import receiver
2-
from django.db.models.signals import post_save
2+
from django.db.models.signals import post_save, post_delete
3+
from guardian.utils import clean_orphan_obj_perms
34

45
from tom_dataproducts.models import ReducedDatum
56
from tom_targets.sharing import continuous_share_data
7+
from tom_targets.models import Target
68

79

810
@receiver(post_save, sender=ReducedDatum)
@@ -11,3 +13,11 @@ def cb_reduceddatum_post_save(sender, instance, *args, **kwargs):
1113
# and if they exist, attempt to share the new data
1214
target = instance.target
1315
continuous_share_data(target, reduced_datums=[instance])
16+
17+
18+
@receiver(post_delete, sender=Target)
19+
def cb_target_post_delete(sender, instance, *args, **kwargs):
20+
# When a Target is deleted, clean up orphaned permissions.
21+
# Note that this removes ALL orphaned permissions, not just those
22+
# associated with this target.
23+
clean_orphan_obj_perms()

0 commit comments

Comments
 (0)