combine all the result templates into one response_result.htm

standardize on the dictionary returned back from helpers, and to the template
This commit is contained in:
Jeffrey Forman 2012-10-28 20:32:37 -04:00
parent eb717e001a
commit 9f2bbdee0f
6 changed files with 54 additions and 85 deletions

View File

@ -75,8 +75,7 @@ def add_record(form_data):
keyring = None keyring = None
response = [] response = []
response.append({ "type" : "Forward Record: %s.%s" % (str(form_data["record_name"]), response.append({ "description" : "Forward Record Added: %(record_name)s.%(zone_name)s" % form_data,
str(form_data["zone_name"])),
"output" : add_forward_record(str(form_data["dns_server"]), "output" : add_forward_record(str(form_data["dns_server"]),
str(form_data["zone_name"]), str(form_data["zone_name"]),
str(form_data["record_name"]), str(form_data["record_name"]),
@ -86,7 +85,7 @@ def add_record(form_data):
keyring)}) keyring)})
if form_data["create_reverse"]: if form_data["create_reverse"]:
response.append({ "type" : "Reverse Record: %s" % form_data["record_data"], response.append({ "description" : "Reverse Record Added: %(record_data)s" % form_data,
"output" : add_reverse_record(str(form_data["dns_server"]), "output" : add_reverse_record(str(form_data["dns_server"]),
str(form_data["zone_name"]), str(form_data["zone_name"]),
str(form_data["record_name"]), str(form_data["record_name"]),
@ -108,7 +107,9 @@ def add_cname_record(dns_server, zone_name, originating_record, cname, ttl, key_
update.replace(cname, int(ttl), 'CNAME', originating_record + ".") update.replace(cname, int(ttl), 'CNAME', originating_record + ".")
response = dns.query.tcp(update, dns_server) response = dns.query.tcp(update, dns_server)
return response return [{ "description" : "CNAME %s.%s points to %s" % (cname, zone_name, originating_record),
"output" : response}]
def delete_record(form_data, rr_items): def delete_record(form_data, rr_items):
"""Delete a list of DNS records passed as strings in rr_items.""" """Delete a list of DNS records passed as strings in rr_items."""
@ -127,6 +128,6 @@ def delete_record(form_data, rr_items):
dns_update = dns.update.Update(domain, keyring = keyring) dns_update = dns.update.Update(domain, keyring = keyring)
dns_update.delete(record) dns_update.delete(record)
output = dns.query.tcp(dns_update, dns_server) output = dns.query.tcp(dns_update, dns_server)
delete_response.append({ "rr_item" : current_rr_item, "output" : output }) delete_response.append({ "description" : "Delete record %s" % current_rr_item, "output" : output })
return delete_response return delete_response

View File

@ -1,7 +1,7 @@
{% extends "base.htm" %} {% extends "base.htm" %}
{% block pageheader %} {% block pageheader %}
Add cname record for {{record_name}}.{{zone_name}} Add CNAME record for {{ originating_record }}
{% endblock pageheader %} {% endblock pageheader %}
{% block body %} {% block body %}
@ -18,8 +18,8 @@ Add cname record for {{record_name}}.{{zone_name}}
<div class="control-group"> <div class="control-group">
<label class="control-label">Originating Record: </label> <label class="control-label">Originating Record: </label>
<div class="controls"> <div class="controls">
<span class="input-xlarge uneditable-input">{{record_name}}.{{zone_name}}</span> <span class="input-xlarge uneditable-input">{{originating_record}}</span>
<input type="hidden" name="originating_record" value="{{record_name}}.{{zone_name}}"/> <input type="hidden" name="originating_record" value="{{originating_record}}"/>
</div> </div>
</div> </div>
@ -30,6 +30,11 @@ Add cname record for {{record_name}}.{{zone_name}}
<input class="span2" size="100" name="cname" type="text"><span class="add-on">.{{zone_name}}</span> <input class="span2" size="100" name="cname" type="text"><span class="add-on">.{{zone_name}}</span>
<input type="hidden" name="zone_name" value="{{zone_name}}"/> <input type="hidden" name="zone_name" value="{{zone_name}}"/>
</div> </div>
{% if form_errors.cname %}
<div class="alert alert-error">
CNAME: {{ form_errors.cname|stringformat:"s"|striptags }} Previous Value: {{ form_data.cname }}
</div>
{% endif %}
</div> </div>
</div> </div>

View File

@ -1,20 +0,0 @@
{% extends "base.htm" %}
{% block pageheader %}
Add CNAME Record Result
{% endblock pageheader %}
{% block body %}
<table class="table">
<tr>
<th>CNAME</th>
<th>Add CNAME Result</th>
</tr>
<tr>
<td>{{ rr_data.cname }}.{{rr_data.zone_name }} points to {{ rr_data.originating_record }}</td>
<td><pre>{{ response }}</pre></td>
</tr>
</table>
{% endblock body %}

View File

@ -1,23 +0,0 @@
{% extends "base.htm" %}
{% block pageheader %}
Delete Record(s) Result
{% endblock pageheader %}
{% block body %}
<table class="table">
{% for current_response in delete_result %}
<tr>
<th>Record</th>
<th>Delete Result</th>
</tr>
<tr>
<td>{{ current_response.rr_item }}</td>
<td><pre>{{ current_response.output }}</pre></td>
</tr>
{% endfor %}
</table>
{% endblock body %}

View File

@ -2,18 +2,18 @@
{% block pageheader %} {% block pageheader %}
Add Record Result Record Result
{% endblock pageheader %} {% endblock pageheader %}
{% block body %} {% block body %}
<table class="table"> <table class="table">
{% for current_response in response %} {% for current_response in response %}
<tr> <tr>
<th>Record:</th> <th>Record</th>
<th>Output</th> <th>Output</th>
</tr> </tr>
<tr> <tr>
<td>{{ current_response.type }}</td> <td>{{ current_response.description }}</td>
<td><pre>{{ current_response.output }}</pre></td> <td><pre>{{ current_response.output }}</pre></td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -6,6 +6,9 @@ import re
RE_UNICODEARRAY = re.compile(r"u'(.*?)'") RE_UNICODEARRAY = re.compile(r"u'(.*?)'")
class BinderException(Exception):
pass
def home_index(request): def home_index(request):
return render(request, 'index.htm') return render(request, 'index.htm')
@ -52,11 +55,11 @@ def view_add_record(request, dns_server, zone_name):
""" View to provide form to add a DNS record. """ """ View to provide form to add a DNS record. """
return render(request, 'bcommon/add_record_form.htm', return render(request, 'bcommon/add_record_form.htm',
{ "dns_server" : dns_server, { "dns_server" : dns_server,
"zone_name" : zone_name }) "zone_name" : zone_name,
"tsig_keys" : models.Key.objects.all() })
def view_add_record_result(request): def view_add_record_result(request):
""" Process the input given to add a DNS record. """ """ Process the input given to add a DNS record. """
errors = None
if request.method == "GET": if request.method == "GET":
return redirect('/') return redirect('/')
@ -65,10 +68,10 @@ def view_add_record_result(request):
cd = form.cleaned_data cd = form.cleaned_data
try: try:
add_record_response = helpers.add_record(cd) add_record_response = helpers.add_record(cd)
except BinderException, errors: except BinderException, error:
pass pass
return render(request, 'bcommon/add_record_result.htm', return render(request, 'bcommon/response_result.htm',
{ "errors" : errors, { "errors" : error,
"response" : add_record_response }) "response" : add_record_response })
return render(request, 'bcommon/add_record_form.htm', return render(request, 'bcommon/add_record_form.htm',
@ -81,22 +84,17 @@ def view_add_cname_record(request, dns_server, zone_name, record_name):
""" Process given input to add a CNAME pointer.""" """ Process given input to add a CNAME pointer."""
return render(request, "bcommon/add_cname_record_form.htm", return render(request, "bcommon/add_cname_record_form.htm",
{ "dns_server" : dns_server, { "dns_server" : dns_server,
"originating_record" : "%s.%s" % (record_name, zone_name),
"zone_name" : zone_name, "zone_name" : zone_name,
"record_name" : record_name,
"tsig_keys" : models.Key.objects.all() }) "tsig_keys" : models.Key.objects.all() })
def view_add_cname_result(request): def view_add_cname_result(request):
if request.method == "GET": if request.method == "GET":
# Return home. You shouldn't trying to directly access
# the url for deleting records.
return redirect('/') return redirect('/')
form = forms.FormAddCnameRecord(request.POST) form = forms.FormAddCnameRecord(request.POST)
if form.is_valid(): if form.is_valid():
cd = form.cleaned_data cd = form.cleaned_data
else:
return "bad form" # TODO: Send back the form pre-populated with the error
try: try:
add_cname_response = helpers.add_cname_record( add_cname_response = helpers.add_cname_record(
str(cd["dns_server"]), str(cd["dns_server"]),
@ -105,14 +103,22 @@ def view_add_cname_result(request):
str(cd["cname"]), str(cd["cname"]),
str(cd["ttl"]), str(cd["ttl"]),
str(cd["key_name"])) str(cd["key_name"]))
except Exception, err: except BinderException, error:
return render(request, 'bcommon/add_cname_result.htm', pass
{ 'errors' : err,
'rr_data' : cd }) return render(request, 'bcommon/response_result.htm',
{ 'errors' : error,
'response' : add_cname_response })
return render(request, "bcommon/add_cname_record_form.htm",
{ "dns_server" : request.POST["dns_server"],
"zone_name" : request.POST["zone_name"],
"record_name" : request.POST["cname"],
"originating_record" : request.POST["originating_record"],
"form_data" : request.POST,
"form_errors" : form.errors,
"tsig_keys" : models.Key.objects.all() })
return render(request, 'bcommon/add_cname_result.htm',
{ 'response' : add_cname_response,
'rr_data' : cd })
def view_delete_record(request): def view_delete_record(request):
if request.method == "GET": if request.method == "GET":
@ -145,8 +151,8 @@ def view_delete_result(request):
try: try:
delete_result = helpers.delete_record(request.POST, rr_items) delete_result = helpers.delete_record(request.POST, rr_items)
except Exception, err: except Exception, err:
return render(request, 'bcommon/delete_record_result.htm', return render(request, 'bcommon/response_result.htm.htm',
{ "errors" : err }), { "errors" : err })
return render(request, 'bcommon/delete_record_result.htm', return render(request, 'bcommon/response_result.htm',
{ 'delete_result' : delete_result }) { 'response' : delete_result })