diff --git a/tcms/signals.py b/tcms/signals.py index 6b25adc576..a934065e46 100644 --- a/tcms/signals.py +++ b/tcms/signals.py @@ -1,6 +1,6 @@ # pylint: disable=unused-argument """ -Defines custom signals sent throught out Kiwi TCMS. You can connect your own +Defines custom signals sent throughout Kiwi TCMS. You can connect your own handlers if you'd like to augment some of the default behavior! If you simply want to connect a signal handler add the following code to your @@ -14,7 +14,7 @@ a new Django app and connect your handler function(s) to the desired signals inside the `AppConfig.ready -`_ +`_ method. When you are done connect your Django app to the rest of Kiwi TCMS by altering the following setting:: @@ -29,6 +29,7 @@ 'notify_admins', 'pre_save_clean', + 'handle_comments_pre_delete', 'handle_emails_post_case_save', 'handle_emails_pre_case_delete', 'handle_emails_post_plan_save', @@ -158,3 +159,19 @@ def handle_emails_post_run_save(sender, *_args, **kwargs): subject, context = history_email_for(instance, instance.summary) mailto(template_name, subject, instance.get_notify_addrs(), context) + + +def handle_comments_pre_delete(sender, **kwargs): + """ + Delete comments attached to object which is about to be + deleted b/c django-comments' object_pk is not a FK relationship + and we can't rely on cascading delete! + """ + from tcms.core.helpers.comments import get_comments + + if kwargs.get('raw', False): + return + + instance = kwargs['instance'] + + get_comments(instance).delete() diff --git a/tcms/testcases/apps.py b/tcms/testcases/apps.py index 4596ba400f..0eacc28112 100644 --- a/tcms/testcases/apps.py +++ b/tcms/testcases/apps.py @@ -12,3 +12,4 @@ def ready(self): pre_save.connect(signals.pre_save_clean, TestCase) post_save.connect(signals.handle_emails_post_case_save, TestCase) pre_delete.connect(signals.handle_emails_pre_case_delete, TestCase) + pre_delete.connect(signals.handle_comments_pre_delete, TestCase) diff --git a/tcms/testruns/apps.py b/tcms/testruns/apps.py index c55967d95b..d1b98c1638 100644 --- a/tcms/testruns/apps.py +++ b/tcms/testruns/apps.py @@ -5,9 +5,10 @@ class AppConfig(DjangoAppConfig): name = 'tcms.testruns' def ready(self): - from django.db.models.signals import post_save, pre_save - from .models import TestRun + from django.db.models.signals import post_save, pre_save, pre_delete + from .models import TestExecution, TestRun from tcms import signals post_save.connect(signals.handle_emails_post_run_save, sender=TestRun) pre_save.connect(signals.pre_save_clean, sender=TestRun) + pre_delete.connect(signals.handle_comments_pre_delete, TestExecution)