From 32d9f3883721fe2c933fb890535cabecc677a3af Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 1 Nov 2013 22:26:06 +0100 Subject: [PATCH] when adding host, deal with Timeout and NameServerNotAvailable exceptions don't add the host, in case of dns exceptions --- nsupdate/main/dnstools.py | 3 +++ nsupdate/main/views.py | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py index 07da15c..2c6116c 100644 --- a/nsupdate/main/dnstools.py +++ b/nsupdate/main/dnstools.py @@ -30,6 +30,9 @@ import dns.tsigkeyring from django.utils.timezone import now +Timeout = dns.resolver.Timeout + + class SameIpError(ValueError): """ raised if an IP address is already present in DNS and and update was diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index 2854343..00802f1 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -103,11 +103,21 @@ class OverviewView(CreateView): def form_valid(self, form): self.object = form.save(commit=False) - self.object.created_by = self.request.user - self.object.save() - dnstools.add(self.object.get_fqdn(), self.request.META['REMOTE_ADDR'], origin=self.object.domain.domain) - messages.add_message(self.request, messages.SUCCESS, 'Host added.') - return HttpResponseRedirect(self.get_success_url()) + try: + dnstools.add(self.object.get_fqdn(), self.request.META['REMOTE_ADDR'], origin=self.object.domain.domain) + except dnstools.Timeout: + # XXX should be ERROR, but ERROR is white on web ui!? + success, level, msg = False, messages.WARNING, 'Timeout - communicating to name server failed.' + except dnstools.NameServerNotAvailable: + # XXX should be ERROR, but ERROR is white on web ui!? + success, level, msg = False, messages.WARNING, 'Name server unavailable.' + else: + self.object.created_by = self.request.user + self.object.save() + success, level, msg = True, messages.SUCCESS, 'Host added.' + messages.add_message(self.request, level, msg) + url = self.get_success_url() if success else reverse('overview') + return HttpResponseRedirect(url) def get_context_data(self, *args, **kwargs): context = super(OverviewView, self).get_context_data(*args, **kwargs)