Bring things up to 1.4 standards. A LOT of file moves, simplifying of paths. Removed bcommon directory under binder

This commit is contained in:
Jeffrey Forman 2012-10-28 14:49:15 -04:00
parent 51da06474c
commit eb717e001a
25 changed files with 184 additions and 247 deletions

View File

@ -1,4 +1,4 @@
from bcommon.models import BindServer, Key
from models import BindServer, Key
from django.contrib import admin
class ZoneAdmin(admin.ModelAdmin):

View File

@ -1,23 +0,0 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

View File

@ -1,170 +0,0 @@
# bcommon views
from bcommon.models import BindServer, Key
from django.template import Context
from django.shortcuts import render_to_response, redirect, render
from bcommon.helpers import add_record, delete_record, add_cname_record
from bcommon.forms import FormAddRecord, FormAddCnameRecord
from django.template import RequestContext
from bcommon.keyutils import create_keyring
import re
RE_UNICODEARRAY = re.compile(r"u'(.*?)'")
def home_index(request):
return render_to_response('index.htm')
def view_server_list(request):
""" List the DNS servers configured in the Django DB. """
server_list = BindServer.objects.all().order_by('hostname')
return render_to_response('bcommon/list_servers.htm',
{ "server_list" : server_list},
context_instance=RequestContext(request))
def view_server_zones(request, dns_server):
""" Display the list of DNS zones a particular DNS host provides. """
errors = ""
zone_array = {}
try:
this_server = BindServer.objects.get(hostname=dns_server)
zone_array = this_server.list_zones()
except BindServer.DoesNotExist, err:
errors = err
if "errors" in zone_array:
errors = zone_array["errors"]
return render_to_response('bcommon/list_server_zones.htm',
{ "errors" : errors,
"dns_server" : dns_server,
"zone_array" : zone_array})
def view_zone_records(request, dns_server, zone_name):
""" Display the list of records for a a particular zone."""
try:
this_server = BindServer.objects.get(hostname=dns_server)
zone_array = this_server.list_zone_records(zone_name)
except Exception, err:
# TODO: Use a custom exception here.
return render_to_response('bcommon/list_zone.htm',
{ 'errors' : err},
context_instance=RequestContext(request))
return render_to_response('bcommon/list_zone.htm',
{ 'zone_array' : zone_array,
'dns_server' : dns_server,
'zone_name' : zone_name},
context_instance=RequestContext(request))
def view_add_record(request, dns_server, zone_name):
""" View to provide form to add a DNS record. """
return render(request, 'bcommon/add_record_form.htm',
{ "dns_server" : dns_server,
"zone_name" : zone_name })
def view_add_record_result(request):
""" Process the input given to add a DNS record. """
errors = None
if request.method == "GET":
return redirect('/')
form = FormAddRecord(request.POST)
if form.is_valid():
cd = form.cleaned_data
try:
add_record_response = add_record(cd)
except BinderException, errors:
pass
return render_to_response('bcommon/add_record_result.htm',
{ "errors" : errors,
"response" : add_record_response },
context_instance=RequestContext(request))
return render(request, 'bcommon/add_record_form.htm',
{ "dns_server" : request.POST["dns_server"],
"zone_name" : request.POST["zone_name"],
"form_errors" : form.errors,
"form_data" : request.POST })
def view_add_cname_record(request, dns_server, zone_name, record_name):
""" Process given input to add a CNAME pointer."""
return render_to_response("bcommon/add_cname_record_form.htm",
{ "dns_server" : dns_server,
"zone_name" : zone_name,
"record_name" : record_name,
"tsig_keys" : Key.objects.all() },
context_instance=RequestContext(request))
def view_add_cname_result(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly access
# the url for deleting records.
return redirect('/')
form = FormAddCnameRecord(request.POST)
if form.is_valid():
cd = form.cleaned_data
else:
return "bad form" # TODO: Send back the form pre-populated with the error
try:
add_cname_response = add_cname_record(str(cd["dns_server"]),
str(cd["zone_name"]),
str(cd["originating_record"]),
str(cd["cname"]),
str(cd["ttl"]),
str(cd["key_name"]))
except Exception, err:
return render_to_response('bcommon/add_cname_result.htm',
{ 'errors' : err,
'rr_data' : cd },)
return render_to_response('bcommon/add_cname_result.htm',
{ 'response' : add_cname_response,
'rr_data' : cd },
context_instance=RequestContext(request))
def view_delete_record(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly acces
# the url for deleting records.
return redirect('/')
dns_server = request.POST['dns_server']
zone_name = request.POST['zone_name']
rr_array = request.POST.getlist('rr_array')
return render_to_response('bcommon/delete_record_initial.htm',
{ 'dns_server' : dns_server,
'zone_name' : zone_name,
'rr_array' : rr_array,
'tsig_keys' : Key.objects.all() },
context_instance=RequestContext(request))
def view_delete_result(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly access
# the url for deleting records.
return redirect('/')
# What seems like an ugly hack to get around the fact
# that the array comes back as Unicode values.
rr_unicode_array = request.POST.getlist('rr_array')[0]
rr_items = RE_UNICODEARRAY.findall(rr_unicode_array)
try:
delete_result = delete_record(request.POST, rr_items)
except Exception, err:
return render_to_response('bcommon/delete_record_result.htm',
{ "errors" : err },
context_instance=RequestContext(request))
return render_to_response('bcommon/delete_record_result.htm',
{ 'delete_result' : delete_result },
context_instance=RequestContext(request))

View File

@ -1,6 +1,6 @@
from django import forms
from bcommon.models import Key
from models import Key
RECORD_TYPE_CHOICES = (("A", "A"), ("AAAA", "AAAA"), ("CNAME", "CNAME"))
TTL_CHOICES = ((300, "5 minutes"),

View File

@ -1,4 +1,4 @@
from bcommon.keyutils import create_keyring
import keyutils
import re
import dns.query
@ -100,7 +100,7 @@ def add_cname_record(dns_server, zone_name, originating_record, cname, ttl, key_
"""Add a Cname record."""
if key_name is None:
keyring = create_keyring(key_name)
keyring = keyutils.create_keyring(key_name)
else:
keyring = None
@ -114,7 +114,7 @@ def delete_record(form_data, rr_items):
"""Delete a list of DNS records passed as strings in rr_items."""
if form_data["key_name"]:
keyring = create_keyring(form_data["key_name"])
keyring = keyutils.create_keyring(form_data["key_name"])
else:
keyring = None

View File

@ -1,6 +1,6 @@
import dns.tsigkeyring
import sys
from bcommon.models import Key
from models import Key
def create_keyring(key_name):
"""Accept a TSIG keyfile and a key name to retrieve.

View File

@ -1,11 +0,0 @@
#!/usr/bin/python
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)

View File

@ -21,13 +21,6 @@ DATABASES = {
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/New_York'
# Language code for this installation. All choices can be found here:
@ -36,12 +29,7 @@ LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
# Absolute path to the directory that holds media.
@ -52,10 +40,8 @@ MEDIA_ROOT = os.path.join(SITE_ROOT, "files")
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = "/files/"
STATIC_URL= "/static/"
STATIC_URL = '/media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'iuo-zka8nnv0o+b*7#_*fcep$@f^35=)c#7_20z6i8g0oc&r!g'
# List of callables that know how to import templates from various sources.
@ -86,5 +72,5 @@ INSTALLED_APPS = (
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.staticfiles',
'bcommon',
'binder',
)

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -3,7 +3,7 @@
{% block header %}
<head>
<title>Binder DNS Admin</title>
<link rel="stylesheet" type="text/css" href="/files/static/bootstrap/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}bootstrap/css/bootstrap.css" />
</head>
{% endblock header %}
@ -22,7 +22,7 @@
<div class="span10">
<body>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="/files/static/bootstrap/js/bootstrap.min.js"></script>
<script src="{{ STATIC_URL }}bootstrap/js/bootstrap.min.js"></script>
<div class="navbar">
<div class="navbar-inner">

View File

@ -21,7 +21,7 @@ Delete record(s) in {{ zone_name }}
<td>{{ dns_server }}</td>
</tr>
<tr>
<td>Domain</td>
<td>Zone</td>
<td> {{ zone_name }} </td>
</tr>
<tr>

View File

@ -1,30 +1,23 @@
from django.conf.urls.defaults import *
from django.conf import settings
import os
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'bcommon.views.home_index', name="index"),
url(r'^server_list/$', 'bcommon.views.view_server_list', name="server_list"),
url(r'^$', 'binder.views.home_index', name="index"),
url(r'^server_list/$', 'binder.views.view_server_list', name="server_list"),
url(r'^info/(?P<dns_server>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_server_zones', name="server_zones"),
url(r'^info/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_zone_records', name="zone_records"),
url(r'^info/(?P<dns_server>[a-zA-Z0-9.-]+)/$', 'binder.views.view_server_zones', name="server_zones"),
url(r'^info/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/$', 'binder.views.view_zone_records', name="zone_records"),
url(r'^add_record/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/$', 'bcommon.views.view_add_record', name="add_record"),
url(r'^add_record/result/$', 'bcommon.views.view_add_record_result'),
url(r'^add_record/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/$', 'binder.views.view_add_record', name="add_record"),
url(r'^add_record/result/$', 'binder.views.view_add_record_result'),
url(r'^delete_record/$', 'bcommon.views.view_delete_record', name="delete_record"),
url(r'^delete_record/result/$', 'bcommon.views.view_delete_result'),
url(r'^delete_record/$', 'binder.views.view_delete_record', name="delete_record"),
url(r'^delete_record/result/$', 'binder.views.view_delete_result'),
url(r'^add_cname/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/(?P<record_name>[a-zA-Z0-9-]+)/$', 'bcommon.views.view_add_cname_record'),
url(r'^add_cname_record/result/$', 'bcommon.views.view_add_cname_result'),
url(r'^add_cname/(?P<dns_server>[a-zA-Z0-9.-]+)/(?P<zone_name>[a-zA-Z0-9.-]+)/(?P<record_name>[a-zA-Z0-9-]+)/$', 'binder.views.view_add_cname_record'),
url(r'^add_cname_record/result/$', 'binder.views.view_add_cname_result'),
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^files/(?P<path>.*)$', 'django.views.static.serve',
{'document_root' : settings.MEDIA_ROOT}
))

152
binder/views.py Normal file
View File

@ -0,0 +1,152 @@
from django.template import Context
from django.shortcuts import redirect, render
from binder import forms, helpers, keyutils, models
import re
RE_UNICODEARRAY = re.compile(r"u'(.*?)'")
def home_index(request):
return render(request, 'index.htm')
def view_server_list(request):
""" List the DNS servers configured in the Django DB. """
server_list = models.BindServer.objects.all().order_by('hostname')
return render(request, 'bcommon/list_servers.htm',
{ "server_list" : server_list})
def view_server_zones(request, dns_server):
""" Display the list of DNS zones a particular DNS host provides. """
errors = ""
zone_array = {}
try:
this_server = models.BindServer.objects.get(hostname=dns_server)
zone_array = this_server.list_zones()
except BindServer.DoesNotExist, err:
errors = err
if "errors" in zone_array:
errors = zone_array["errors"]
return render(request, 'bcommon/list_server_zones.htm',
{ "errors" : errors,
"dns_server" : dns_server,
"zone_array" : zone_array})
def view_zone_records(request, dns_server, zone_name):
""" Display the list of records for a a particular zone."""
try:
this_server = models.BindServer.objects.get(hostname=dns_server)
zone_array = this_server.list_zone_records(zone_name)
except Exception, err:
# TODO: Use a custom exception here.
return render(request, 'bcommon/list_zone.htm',
{ 'errors' : err})
return render(request, 'bcommon/list_zone.htm',
{ 'zone_array' : zone_array,
'dns_server' : dns_server,
'zone_name' : zone_name})
def view_add_record(request, dns_server, zone_name):
""" View to provide form to add a DNS record. """
return render(request, 'bcommon/add_record_form.htm',
{ "dns_server" : dns_server,
"zone_name" : zone_name })
def view_add_record_result(request):
""" Process the input given to add a DNS record. """
errors = None
if request.method == "GET":
return redirect('/')
form = forms.FormAddRecord(request.POST)
if form.is_valid():
cd = form.cleaned_data
try:
add_record_response = helpers.add_record(cd)
except BinderException, errors:
pass
return render(request, 'bcommon/add_record_result.htm',
{ "errors" : errors,
"response" : add_record_response })
return render(request, 'bcommon/add_record_form.htm',
{ "dns_server" : request.POST["dns_server"],
"zone_name" : request.POST["zone_name"],
"form_errors" : form.errors,
"form_data" : request.POST })
def view_add_cname_record(request, dns_server, zone_name, record_name):
""" Process given input to add a CNAME pointer."""
return render(request, "bcommon/add_cname_record_form.htm",
{ "dns_server" : dns_server,
"zone_name" : zone_name,
"record_name" : record_name,
"tsig_keys" : models.Key.objects.all() })
def view_add_cname_result(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly access
# the url for deleting records.
return redirect('/')
form = forms.FormAddCnameRecord(request.POST)
if form.is_valid():
cd = form.cleaned_data
else:
return "bad form" # TODO: Send back the form pre-populated with the error
try:
add_cname_response = helpers.add_cname_record(
str(cd["dns_server"]),
str(cd["zone_name"]),
str(cd["originating_record"]),
str(cd["cname"]),
str(cd["ttl"]),
str(cd["key_name"]))
except Exception, err:
return render(request, 'bcommon/add_cname_result.htm',
{ 'errors' : err,
'rr_data' : cd })
return render(request, 'bcommon/add_cname_result.htm',
{ 'response' : add_cname_response,
'rr_data' : cd })
def view_delete_record(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly acces
# the url for deleting records.
return redirect('/')
dns_server = request.POST['dns_server']
zone_name = request.POST['zone_name']
rr_array = request.POST.getlist('rr_array')
return render(request, 'bcommon/delete_record_initial.htm',
{ 'dns_server' : dns_server,
'zone_name' : zone_name,
'rr_array' : rr_array,
'tsig_keys' : models.Key.objects.all() })
def view_delete_result(request):
if request.method == "GET":
# Return home. You shouldn't trying to directly access
# the url for deleting records.
return redirect('/')
# What seems like an ugly hack to get around the fact
# that the array comes back as Unicode values.
rr_unicode_array = request.POST.getlist('rr_array')[0]
rr_items = RE_UNICODEARRAY.findall(rr_unicode_array)
try:
delete_result = helpers.delete_record(request.POST, rr_items)
except Exception, err:
return render(request, 'bcommon/delete_record_result.htm',
{ "errors" : err }),
return render(request, 'bcommon/delete_record_result.htm',
{ 'delete_result' : delete_result })

10
manage.py Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "binder.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)