From 87b3711fd599cbf2ef195988a82f4cd804cedabb Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 2 Nov 2013 00:12:36 +0100 Subject: [PATCH] handle deletion of a Domain as we reference the Domain as ForeignKey in the Host, it'll kill all Hosts that reference the deleted Domain also. needs to be the PRE_delete signal as some of the data we need is already gone when using POST. --- nsupdate/main/models.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nsupdate/main/models.py b/nsupdate/main/models.py index c52e9f2..64c2641 100644 --- a/nsupdate/main/models.py +++ b/nsupdate/main/models.py @@ -8,7 +8,7 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.conf import settings -from django.db.models.signals import post_delete +from django.db.models.signals import pre_delete from django.contrib.auth.hashers import make_password from . import dnstools @@ -82,7 +82,7 @@ class Host(models.Model): message='Invalid subdomain: only "a-z", "0-9" and "-" is allowed' ), domain_blacklist_validator]) - domain = models.ForeignKey(Domain) + domain = models.ForeignKey(Domain, on_delete=models.CASCADE) update_secret = models.CharField(max_length=256) # gets hashed on save comment = models.CharField( max_length=256, default='', blank=True, null=True) @@ -148,8 +148,12 @@ class Host(models.Model): return secret -def post_delete_host(sender, **kwargs): +def pre_delete_host(sender, **kwargs): obj = kwargs['instance'] - dnstools.delete(obj.get_fqdn(), origin=obj.domain.domain) + try: + dnstools.delete(obj.get_fqdn(), origin=obj.domain.domain) + except (dnstools.Timeout, dnstools.NameServerNotAvailable): + # well, we tried to clean up, but we didn't reach the nameserver + pass -post_delete.connect(post_delete_host, sender=Host) +pre_delete.connect(pre_delete_host, sender=Host)