Skip to content

Commit

Permalink
Delete comments when TC and TE are removed. Closes #1028
Browse files Browse the repository at this point in the history
we do this with a pre-delete signal b/c the object_pk field
isn't a FK relationship but rather an integer field so we can't
rely on a cascading delete!
  • Loading branch information
atodorov committed Nov 24, 2019
1 parent e6c6916 commit dbd8435
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
21 changes: 19 additions & 2 deletions 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
Expand All @@ -14,7 +14,7 @@
a new Django app and connect your handler function(s) to the desired signals
inside the
`AppConfig.ready
<https://docs.djangoproject.com/en/2.0/ref/applications/#django.apps.AppConfig.ready>`_
<https://docs.djangoproject.com/en/2.2/ref/applications/#django.apps.AppConfig.ready>`_
method. When you are done connect your Django app to the rest of Kiwi TCMS by
altering the following setting::
Expand All @@ -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',
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions tcms/testcases/apps.py
Expand Up @@ -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)
5 changes: 3 additions & 2 deletions tcms/testruns/apps.py
Expand Up @@ -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)

0 comments on commit dbd8435

Please sign in to comment.