From db1a669c689d11853edee1d9de421567e10a08d9 Mon Sep 17 00:00:00 2001 From: Ryan Petrello Date: Tue, 15 May 2018 16:51:58 -0400 Subject: [PATCH] work around a django-polymorphic bug that breaks certain cascades see: #1794 --- awx/main/models/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/awx/main/models/__init__.py b/awx/main/models/__init__.py index 7764655419..6541a8f9ed 100644 --- a/awx/main/models/__init__.py +++ b/awx/main/models/__init__.py @@ -3,6 +3,7 @@ # Django from django.conf import settings # noqa +from django.db.models.signals import pre_delete # noqa # AWX from awx.main.models.base import * # noqa @@ -58,6 +59,18 @@ User.add_to_class('can_access_with_errors', check_user_access_with_errors) User.add_to_class('accessible_objects', user_accessible_objects) +def cleanup_created_modified_by(sender, **kwargs): + # work around a bug in django-polymorphic that doesn't properly + # handle cascades for reverse foreign keys on the polymorphic base model + # https://github.com/django-polymorphic/django-polymorphic/issues/229 + for cls in (UnifiedJobTemplate, UnifiedJob): + cls.objects.filter(created_by=kwargs['instance']).update(created_by=None) + cls.objects.filter(modified_by=kwargs['instance']).update(modified_by=None) + + +pre_delete.connect(cleanup_created_modified_by, sender=User) + + @property def user_get_organizations(user): return Organization.objects.filter(member_role__members=user)