diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py index c128ded..4ef7d91 100644 --- a/nsupdate/main/_tests/test_dnstools.py +++ b/nsupdate/main/_tests/test_dnstools.py @@ -4,16 +4,16 @@ Tests for dnstools module. import pytest -from ..dnstools import (update, query_ns, parse_name, update_ns, SameIpError, - BASEDOMAIN, NONEXISTING_HOST, - WWW_HOST, WWW_IPV4_HOST, WWW_IPV4_IP, WWW_IPV6_HOST, WWW_IPV6_IP, ) +from ..dnstools import update, query_ns, parse_name, update_ns, SameIpError +from django.conf import settings +BASEDOMAIN = settings.BASEDOMAIN from dns.resolver import NXDOMAIN class TestIntelligentUpdater(object): def test_double_update(self): - host, ip = 'test0.' + BASEDOMAIN, '1.2.3.4' + host, ip = 'test0.' + settings.BASEDOMAIN, '1.2.3.4' # make sure the host is not there try: update_ns(host, 'A', action='del') @@ -29,16 +29,16 @@ class TestIntelligentUpdater(object): class TestQuery(object): def test_queries_ok(self): - assert query_ns(WWW_IPV4_HOST, 'A') == WWW_IPV4_IP # v4 ONLY - assert query_ns(WWW_IPV6_HOST, 'AAAA') == WWW_IPV6_IP # v6 ONLY - assert query_ns(WWW_HOST, 'A') == WWW_IPV4_IP # v4 and v6, query v4 - assert query_ns(WWW_HOST, 'AAAA') == WWW_IPV6_IP # v4 and v6, query v6 + assert query_ns(settings.WWW_IPV4_HOST, 'A') == settings.WWW_IPV4_IP # v4 ONLY + assert query_ns(settings.WWW_IPV6_HOST, 'AAAA') == settings.WWW_IPV6_IP # v6 ONLY + assert query_ns(settings.WWW_HOST, 'A') == settings.WWW_IPV4_IP # v4 and v6, query v4 + assert query_ns(settings.WWW_HOST, 'AAAA') == settings.WWW_IPV6_IP # v4 and v6, query v6 def test_queries_failing(self): with pytest.raises(NXDOMAIN): - query_ns(NONEXISTING_HOST, 'A') + query_ns(settings.NONEXISTING_HOST, 'A') with pytest.raises(NXDOMAIN): - query_ns(NONEXISTING_HOST, 'AAAA') + query_ns(settings.NONEXISTING_HOST, 'AAAA') class TestUpdate(object): diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py index 78ff67e..8c2f74d 100644 --- a/nsupdate/main/dnstools.py +++ b/nsupdate/main/dnstools.py @@ -13,20 +13,6 @@ import dns.tsig import dns.tsigkeyring -SERVER = settings.SERVER -BASEDOMAIN = settings.BASEDOMAIN - -NONEXISTING_HOST = settings.NONEXISTING_HOST -WWW_HOST = settings.WWW_HOST -WWW_IPV4_HOST = settings.WWW_IPV4_HOST -WWW_IPV6_HOST = settings.WWW_IPV6_HOST -WWW_IPV4_IP = settings.WWW_IPV4_IP -WWW_IPV6_IP = settings.WWW_IPV6_IP - -UPDATE_ALGO = settings.UPDATE_ALGO -UPDATE_KEY = settings.UPDATE_KEY - - class SameIpError(ValueError): """ raised if an IP address is already present in DNS and and update was @@ -66,8 +52,8 @@ def query_ns(qname, rdtype): resolver = dns.resolver.Resolver(configure=False) # we do not configure it from resolv.conf, but patch in the values we # want into the documented attributes: - resolver.nameservers = [SERVER, ] - resolver.search = [dns.name.from_text(BASEDOMAIN), ] + resolver.nameservers = [settings.SERVER, ] + resolver.search = [dns.name.from_text(settings.BASEDOMAIN), ] answer = resolver.query(qname, rdtype) return str(list(answer)[0]) @@ -107,8 +93,8 @@ def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60): assert action in ['add', 'del', 'upd', ] origin, name = parse_name(fqdn, origin) upd = dns.update.Update(origin, - keyring=dns.tsigkeyring.from_text({BASEDOMAIN+'.': UPDATE_KEY}), - keyalgorithm=UPDATE_ALGO) + keyring=dns.tsigkeyring.from_text({settings.BASEDOMAIN+'.': settings.UPDATE_KEY}), + keyalgorithm=settings.UPDATE_ALGO) if action == 'add': assert ipaddr is not None upd.add(name, ttl, rdtype, ipaddr) @@ -117,5 +103,5 @@ def update_ns(fqdn, rdtype='A', ipaddr=None, origin=None, action='upd', ttl=60): elif action == 'upd': assert ipaddr is not None upd.replace(name, ttl, rdtype, ipaddr) - response = dns.query.tcp(upd, SERVER) + response = dns.query.tcp(upd, settings.SERVER) return response diff --git a/nsupdate/main/forms.py b/nsupdate/main/forms.py index ab602f8..baf274d 100644 --- a/nsupdate/main/forms.py +++ b/nsupdate/main/forms.py @@ -13,6 +13,13 @@ class HostForm(forms.ModelForm): super(HostForm, self).__init__(*args, **kwargs) self.created_by = user + def save(self, user, commit=True): + instance = super(HostForm, self).save(commit=False) + instance.created_by = user + if commit: + instance.save() + return instance + def create_host(self, user): self.clean() host = Host(fqdn=self.cleaned_data['fqdn'], diff --git a/nsupdate/main/models.py b/nsupdate/main/models.py index 85cb220..889b8ab 100644 --- a/nsupdate/main/models.py +++ b/nsupdate/main/models.py @@ -4,6 +4,7 @@ from django.forms import ModelForm class Host(models.Model): + """TODO: hash update_secret""" fqdn = models.CharField(max_length=256) update_secret = models.CharField(max_length=256) comment = models.CharField(max_length=256,default='') @@ -16,6 +17,7 @@ class Host(models.Model): return u"%s (%s)" % (self.fqdn, self.created_by) + class HostForm(ModelForm): class Meta: model = Host diff --git a/nsupdate/main/templates/main/host.html b/nsupdate/main/templates/main/host.html new file mode 100644 index 0000000..0800fba --- /dev/null +++ b/nsupdate/main/templates/main/host.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% load bootstrap %} + +{% block content %} + +
+

Edit Host

+
+
+ {% csrf_token %} + {{ HostForm|bootstrap }} + +
+
+ + +{% endblock %} \ No newline at end of file diff --git a/nsupdate/main/templates/main/overview.html b/nsupdate/main/templates/main/overview.html index bb4c899..e5dc9e5 100644 --- a/nsupdate/main/templates/main/overview.html +++ b/nsupdate/main/templates/main/overview.html @@ -5,33 +5,38 @@
-

Host List

+

Host List

- + {% for host in Hosts %} - + + + + + {% empty %} No hosts yet. {% endfor %}
domainlast updatecomment
domainlast updatecommentaction
{{ host.fqdn }}.nsupdate.info {{ host.last_update|date }} {{ host.comment }}
{{ host.fqdn }}{{ host.last_update|date }}{{ host.comment }} +
- -
- - -
-

New host

+

New host

{% csrf_token %} {{ HostForm|bootstrap }} - -
- -
- +
+
+

Information

+ your IPv4: {{session.ipv4}}
+ your IPv6: {{session.ipv6}} +
+
+ + +
{% endblock %} \ No newline at end of file diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index 77ece4e..f74f917 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -1,6 +1,6 @@ from django.conf.urls import patterns, include, url from main.views import ( - HomeView, OverviewView, + HomeView, OverviewView, HostView, ) from api.views import ( MyIpView, UpdateIpView, NicUpdateView @@ -9,6 +9,7 @@ from api.views import ( urlpatterns = patterns('', url(r'^$', HomeView.as_view(), name="home"), url(r'^overview/$', OverviewView, name='overview'), + url(r'^host/(?P\w+)$', HostView, name='host-view'), url(r'^myip$', MyIpView), url(r'^updateip$', UpdateIpView), url(r'^nic/update$', NicUpdateView), diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index 2ca7884..ea13a33 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -3,11 +3,19 @@ from django.views.generic import TemplateView from django.views.generic.list import ListView from django.http import HttpResponse from django.conf import settings -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 from django.contrib.auth.decorators import login_required from main.forms import HostForm from main.models import Host +def create_context(request): + context = {} + context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST + context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST + context['session'] = request.session + return context + + class HomeView(TemplateView): template_name = "base.html" @@ -18,21 +26,36 @@ class HomeView(TemplateView): @login_required def OverviewView(request): - context = {} + context = create_context(request) context['nav_overview'] = True - context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST - context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST - context['session'] = request.session context['HostForm'] = HostForm(request.user) context['Hosts'] = Host.objects.filter(created_by=request.user) + if request.method == "POST": print "POST" form = HostForm(request.user,request.POST) print form if form.is_valid(): print "valid" - host = form.create_host(request.user) host.save() context['HostForm'] = form return render(request, "main/overview.html", context) +<<<<<<< HEAD +======= + +@login_required +def HostView(request,pk=None): + context = create_context(request) + context['nav_overview'] = True + context['HostForm'] = HostForm(request.user,instance=get_object_or_404(Host, pk=pk, created_by=request.user)) + if request.method == "POST": + print "POST" + form = HostForm(request.user, request.POST,) + print form + if form.is_valid(): + print "valid" + host = form.save(request.user) + context['HostForm'] = form + return render(request, "main/host.html", context) +>>>>>>> 3b2fa9008fe6aa3d20e7b3ae9504fadedb4165c4