summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKen Raffenetti <raffenet@mcs.anl.gov>2006-11-16 22:44:58 +0000
committerKen Raffenetti <raffenet@mcs.anl.gov>2006-11-16 22:44:58 +0000
commit60d1e22e17b0065daeebe446e58e2c023e650bb7 (patch)
treec172b0aa25245b966e4660ae42b8768b7691a8ff
parent71f47d076cde79880f895616f12c2f8dd009b515 (diff)
downloadbcfg2-60d1e22e17b0065daeebe446e58e2c023e650bb7.tar.gz
bcfg2-60d1e22e17b0065daeebe446e58e2c023e650bb7.tar.bz2
bcfg2-60d1e22e17b0065daeebe446e58e2c023e650bb7.zip
updated backend code for zone views in Hostbase
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@2501 ce84e21b-d406-0410-9b95-82705330c041
-rw-r--r--src/lib/Server/Hostbase/hostbase/views.py192
-rw-r--r--src/lib/Server/Hostbase/hostbase/webtemplates/navbar4
-rw-r--r--src/lib/Server/Hostbase/hostbase/webtemplates/zoneedit.html169
-rw-r--r--src/lib/Server/Hostbase/hostbase/webtemplates/zonenew.html60
4 files changed, 179 insertions, 246 deletions
diff --git a/src/lib/Server/Hostbase/hostbase/views.py b/src/lib/Server/Hostbase/hostbase/views.py
index 6b228a805..6349f88d7 100644
--- a/src/lib/Server/Hostbase/hostbase/views.py
+++ b/src/lib/Server/Hostbase/hostbase/views.py
@@ -13,6 +13,7 @@ from Hostbase.hostbase.models import *
from datetime import date
from django.db import connection
from django.shortcuts import render_to_response
+from django import forms
from Hostbase import settings, regex
import re
@@ -770,107 +771,102 @@ def zoneview(request, zone_id):
'logged_in': request.session.get('_auth_user_id', False)
})
-def zoneedit(request, zone_id):
- if request.GET.has_key('sub'):
- zone = Zone.objects.get(id=zone_id)
- for attrib in zoneattribs:
- if request.POST.has_key(attrib):
- zone.__dict__[attrib] = request.POST[attrib]
- count = 0
- for nameserver in zone.nameservers.all():
- ns, created = Nameserver.objects.get_or_create(name=request.POST['nameserver%i' % count])
- if created or not (nameserver == ns):
- ns.save()
- zone.nameservers.add(ns)
- zone.nameservers.remove(nameserver)
- count += 1
- count = 0
- for mx in zone.mxs.all():
- mrecord, created = MX.objects.get_or_create(priority=request.POST['priority%i' % count],
- mx=request.POST['mx%i' % count])
- if created or not (mx == mrecord):
- mrecord.save()
- zone.mxs.add(mrecord)
- zone.mxs.remove(mx)
- count += 1
- count = 0
- for address in zone.addresses.all():
- arecord, created = ZoneAddress.objects.get_or_create(ip_addr=request.POST['address%i' % count])
- if created or not (arecord == address):
- arecord.save()
- zone.addresses.add(arecord)
- zone.addresses.remove(address)
- count += 1
- zone.save()
- if request.POST['new_nameserver']:
- nameserver, created = Nameserver.objects.get_or_create(name=request.POST['new_nameserver'])
- if created:
- nameserver.save()
- zone.nameservers.add(nameserver)
- if request.POST['new_mx'] and request.POST['new_priority']:
- mx, created = MX.objects.get_or_create(priority=request.POST['new_priority'],
- mx=request.POST['new_mx'])
- if created:
- mx.save()
- zone.mxs.add(mx)
- if request.POST['new_address'] and not request.POST['new_address'] == 'none':
- address, created = ZoneAddress.objects.get_or_create(ip_addr=request.POST['new_address'])
- if created:
- address.save()
- zone.addresses.add(address)
- return HttpResponseRedirect('/hostbase/zones/%s/edit' % zone.id)
- else:
- zone = Zone.objects.get(id=zone_id)
- return render_to_response('zoneedit.html',
- {'zone': zone,
- 'nameservers': zone.nameservers.all(),
- 'mxs': zone.mxs.all(),
- 'addresses': zone.addresses.all(),
- 'logged_in': request.session.get('_auth_user_id', False)
- })
-
def zonenew(request):
- if request.GET.has_key('sub'):
- try:
- Zone.objects.get(zone=request.POST['zone'])
- return render_to_response('errors.html',
- {'failures': ['%s already exists in database' % request.POST['zone']],
- 'logged_in': request.session.get('_auth_user_id', False)})
+ manipulator = Zone.AddManipulator()
+ nsmanipulator = Nameserver.AddManipulator()
+ mxmanipulator = MX.AddManipulator()
+ addressmanipulator = ZoneAddress.AddManipulator()
+
+ if request.method == 'POST':
+ new_data = request.POST.copy()
+ new_data['serial'] = '1'
+ errors = manipulator.get_validation_errors(new_data)
+ errors.update(check_zone_errors(request.POST.copy()))
+ if errors:
+ return render_to_response('errors.html', {'failures': errors})
+ else:
+ do_zone_add(manipulator, new_data)
+ return HttpResponseRedirect('/hostbase/zones/%s' % new_zone.id)
+ else:
+ errors = new_data = {}
+
+ form = forms.FormWrapper(manipulator, {}, {})
+ nsform = forms.FormWrapper(nsmanipulator, {}, {})
+ mxform = forms.FormWrapper(mxmanipulator, {}, {})
+ aform = forms.FormWrapper(addressmanipulator, {}, {})
+ return render_to_response('zonenew.html', {'form': form,
+ 'nsform': nsform,
+ 'mxform': mxform,
+ 'aform': aform,
+ })
- except:
- zone = Zone(zone=request.POST['zone'])
- for attrib in zoneattribs:
- if request.POST.has_key(attrib):
- zone.__dict__[attrib] = request.POST[attrib]
- zone.serial = 1
- zone.save()
- for num in range(0,4):
- if request.POST['nameserver%i' % num]:
- ns, created = Nameserver.objects.get_or_create(name=request.POST['nameserver%i' % num])
- if created:
- ns.save()
- zone.nameservers.add(ns)
- for num in range(0,2):
- if request.POST['priority%i' % num] and request.POST['mx%i' % num]:
- mrecord, created = MX.objects.get_or_create(priority=request.POST['priority%i' % num],
- mx=request.POST['mx%i' % num])
- if created:
- mrecord.save()
- zone.mxs.add(mrecord)
- for num in range(0,2):
- if request.POST['address%i' % num]:
- arecord, created = ZoneAddress.objects.get_or_create(ip_addr=request.POST['address%i' % num])
- if created:
- arecord.save()
- zone.addresses.add(arecord)
- return HttpResponseRedirect('/hostbase/zones/%s/' % zone.id)
+def zoneedit(request, zone_id):
+ manipulator = Zone.ChangeManipulator(zone_id)
+ nsaddmanipulator = Nameserver.AddManipulator()
+ mxaddmanipulator = MX.AddManipulator()
+ addressaddmanipulator = ZoneAddress.AddManipulator()
+ zone = manipulator.original_object
+ nsmanipulators = [Nameserver.ChangeManipulator(ns.id) for ns in zone.nameservers.all()]
+ mxmanipulators = [MX.ChangeManipulator(mx.id) for mx in zone.mxs.all()]
+ addressmanipulators = [ZoneAddress.ChangeManipulator(address.id) for address in zone.addresses.all()]
+ if request.method == 'POST':
+ new_data = request.POST.copy()
+ new_data['serial'] = str(zone.serial)
+ errors = manipulator.get_validation_errors(new_data)
+ errors.update(check_zone_errors(request.POST.copy()))
+ if not errors:
+ do_zone_add(manipulator, new_data)
+ return HttpResponseRedirect('/hostbase/zones/%s' % zone.id)
+ else:
+ return render_to_response('errors.html', {'failures': errors})
else:
- return render_to_response('zonenew.html',
- {'nameservers': range(0,4),
- 'mxs': range(0,2),
- 'addresses': range(0,2),
- 'logged_in': request.session.get('_auth_user_id', False)
- })
+ errors = {}
+ new_data = manipulator.flatten_data()
+
+ form = forms.FormWrapper(manipulator, new_data, errors)
+ nsforms = [forms.FormWrapper(nsm, nsm.flatten_data(), {}) for nsm in nsmanipulators]
+ mxforms = [forms.FormWrapper(mxm, mxm.flatten_data(), {}) for mxm in mxmanipulators]
+ aforms = [forms.FormWrapper(am, am.flatten_data(), {}) for am in addressmanipulators]
+ return render_to_response('zoneedit.html', {'form': form,
+ 'nsforms': nsforms,
+ 'mxforms': mxforms,
+ 'aforms': aforms,
+ 'nsadd': forms.FormWrapper(nsaddmanipulator, {}, {}),
+ 'mxadd': forms.FormWrapper(mxaddmanipulator, {}, {}),
+ 'addadd': forms.FormWrapper(addressaddmanipulator, {}, {}),
+ 'zone_id': zone_id,
+ 'zone': zone.zone
+ })
+
+def do_zone_add(manipulator, new_data):
+ manipulator.do_html2python(new_data)
+ zone = manipulator.save(new_data)
+ for name in new_data.getlist('name'):
+ if name:
+ ns, created = Nameserver.objects.get_or_create(name=name)
+ zone.nameservers.add(ns)
+ priorities = new_data.getlist('priority')
+ for mx in new_data.getlist('mx'):
+ if priorities[0] and mx:
+ mxrecord, created = MX.objects.get_or_create(priority=priorities.pop(0), mx=mx)
+ zone.mxs.add(mxrecord)
+ for address in new_data.getlist('ip_addr'):
+ if address:
+ arecord, created = ZoneAddress.objects.get_or_create(ip_addr=address)
+ zone.addresses.add(arecord)
+
+def check_zone_errors(new_data):
+ errors = {}
+ for ns in new_data.getlist('name'):
+ errors.update(Nameserver.AddManipulator().get_validation_errors({'name':ns}))
+ for addr in new_data.getlist('ip_addr'):
+ errors.update(ZoneAddress.AddManipulator().get_validation_errors({'ip_addr':addr}))
+ priorities = new_data.getlist('priority')
+ count = 0
+ for mx in new_data.getlist('mx'):
+ errors.update(MX.AddManipulator().get_validation_errors({'mx':mx, 'priority':priorities[0]}))
+ count += 1
+ return errors
## login required stuff
## uncomment the views below that you would like to restrict access to
diff --git a/src/lib/Server/Hostbase/hostbase/webtemplates/navbar b/src/lib/Server/Hostbase/hostbase/webtemplates/navbar
index dd565a93e..877d427d0 100644
--- a/src/lib/Server/Hostbase/hostbase/webtemplates/navbar
+++ b/src/lib/Server/Hostbase/hostbase/webtemplates/navbar
@@ -1,5 +1,5 @@
-<a href="/hostbase/" class="sidebar">new search</a><br>
+<a href="/hostbase/" class="sidebar">host search</a><br>
<a href="/hostbase/new" class="sidebar">add a new host</a><br>
<a href="/hostbase/zones" class="sidebar">zone file information</a><br>
-<a href="/hostbase/zones/new" class="sidebar">add a new zone</a><br>
+<a href="/hostbase/zones/new" class="sidebar">add a zone</a><br>
diff --git a/src/lib/Server/Hostbase/hostbase/webtemplates/zoneedit.html b/src/lib/Server/Hostbase/hostbase/webtemplates/zoneedit.html
index 7eb7f2184..06abebed1 100644
--- a/src/lib/Server/Hostbase/hostbase/webtemplates/zoneedit.html
+++ b/src/lib/Server/Hostbase/hostbase/webtemplates/zoneedit.html
@@ -3,7 +3,7 @@
{% block pagebanner %}
<div class="header">
<h2>Zones</h2>
- <p>Edit information for {{ zone.zone }}
+ <p>Edit information for {{ zone }}
</div>
<br/>
{% endblock %}
@@ -11,7 +11,7 @@
{% block sidebar %}
{% include "navbar" %}
<ul>
-<li><a href="/hostbase/zones/{{ zone.id }}/" class="sidebar">view zone</a><br>
+<li><a href="/hostbase/zones/{{ zone_id }}/" class="sidebar">view zone</a><br>
</li>
</ul>
{% include "logout" %}
@@ -40,112 +40,73 @@ div#address{
}
</style>
-<form name="zonedata" action="?sub=true" method="post">
-<input type="hidden" name="zone" value="{{ zone.id }}">
+<form name="zonedata" action="" method="post">
<table border="0" width="100%">
<colgroup>
<col width="200">
<col width="*">
- <tr> <td> <b>zone</b></td>
- <td> <input name="zone" type="text" size="32" value="{{ zone.zone }}"></td></tr>
- <tr> <td> <b>admin</b></td>
- <td> <input name="admin" type="text" size="32" value="{{ zone.admin }}"></td></tr>
- <tr> <td> <b>primary_master</b></td>
- <td> <input name="primary_master" type="text" size="32" value="{{ zone.primary_master }}"></td></tr>
- <tr> <td> <b>expire</b></td>
- <td> <input name="expire" type="text" size="32" value="{{ zone.expire }}"></td></tr>
- <tr> <td> <b>retry</b></td>
- <td> <input name="retry" type="text" size="32" value="{{ zone.retry }}"></td></tr>
- <tr> <td> <b>refresh</b></td>
- <td> <input name="refresh" type="text" size="32" value="{{ zone.refresh }}"></td></tr>
- <tr> <td> <b>ttl</b></td>
- <td> <input name="ttl" type="text" size="32" value="{{ zone.ttl }}"></td></tr>
-
- <tr><td valign="top"> <b>nameservers</b><br>
- <a style="font-size:75%" href=# onclick="toggleField('nameserver')">add a new NS record</a>
- </td>
- <td>
- {% for nameserver in nameservers %}
- <input name="nameserver{{ forloop.counter0 }}" type="text" size="32" value="{{ nameserver.name }}">
- <a style="font-size:75%" href="/hostbase/zones/{{ zone.id }}/nameserver/{{ nameserver.id }}/confirm">remove<br>
- {% endfor %}
- </td></tr>
- </table>
- <div id=nameserver>
- <table border="0" width="100%">
- <colgroup>
- <col width="200">
- <col width="*">
- <tr> <td></td>
- <td> <input name="new_nameserver" size="32" type="text"></td></tr>
- </table>
- </div>
- <table border="0" width="100%">
- <colgroup>
- <col width="200">
- <col width="*">
- <tr><td valign="top"> <b>mxs</b><br>
- <a style="font-size:75%" href=# onclick="toggleField('mx')">add a new MX record</a>
- </td>
- <td>
- {% for mx in mxs %}
- <input name="priority{{ forloop.counter0 }}" type="text" size="6" value="{{ mx.priority }}">
- <input name="mx{{ forloop.counter0 }}" type="text" size="32" value="{{ mx.mx }}">
- <a style="font-size:75%" href="/hostbase/zones/{{ zone.id }}/zonemx/{{ mx.id }}/confirm">remove<br>
- {% endfor %}
- </td></tr>
- </table>
- <div id=mx>
- <table border="0" width="100%">
- <colgroup>
- <col width="200">
- <col width="*">
- <tr> <td></td>
- <td> <input name="new_priority" type="text" size="6" >
- <input name="new_mx" type="text" size="32" >
- </td></tr>
- </table>
- </div>
- <table border="0" width="100%">
- <colgroup>
- <col width="200">
- <col width="*">
- <tr><td valign="top"> <b>A records</b>
- {% if addresses %}
- <br><a style="font-size:75%" href=# onclick="toggleField('address')">add a new A record</a>
- {% endif %}
- </td>
- <td>
- {% if addresses %}
- {% for address in addresses %}
- <input name="address{{ forloop.counter0 }}" type="text" value="{{ address.ip_addr }}">
- <a style="font-size:75%" href="/hostbase/zones/{{ zone.id }}/address/{{ address.id }}/confirm">remove<br>
- {% endfor %}
- </td></tr>
- </table>
- <div id=address>
- <table border="0" width="100%">
- <colgroup>
- <col width="200">
- <col width="*">
- <tr> <td></td>
- <td> <input name="new_address" type="text" >
- </td></tr>
- </table>
- </div>
- <table border="0" width="100%">
- <colgroup>
- <col width="200">
- <col width="*">
- {% else %}
- <input name="new_address" type="text" value="none" >
- {% endif %}
- </td></tr>
-
- <tr> <td valign="top"> <b>aux</b> (for information not generated by the database)</td>
- <td> <textarea rows="20" cols="80" name="aux">{{ zone.aux }}</textarea></td></tr>
- </td></tr>
-
+<tr><td><label for="id_zone">zone:</label></td> <td>{{ form.zone }}</td></tr>
+<tr><td><label for="id_admin">admin:</label></td> <td>{{ form.admin }}</td></tr>
+<tr><td><label for="id_primary_master">primary_master:</label></td> <td>{{ form.primary_master }}</td></tr>
+<tr><td><label for="id_expire">expire:</label></td> <td>{{ form.expire }}</td></tr>
+<tr><td><label for="id_retry">retry:</label></td> <td>{{ form.retry }}</td></tr>
+<tr><td><label for="id_refresh">refresh:</label></td> <td>{{ form.refresh }}</td></tr>
+<tr><td><label for="id_ttl">ttl:</label></td> <td>{{ form.ttl }}</td></tr>
+{% for ns in nsforms %}
+<tr><td><label for="id_name">nameserver:</label></td> <td>{{ ns.name }}</td></tr>
+{% endfor %}
+</table>
+<div id=nameserver>
+<table border="0" width="100%">
+ <colgroup>
+ <col width="200">
+ <col width="*">
+ <tr><td><label for="id_name">nameserver:</label></td> <td>{{ nsadd.name }}</td></tr>
+ <tr><td><label for="id_name">nameserver:</label></td> <td>{{ nsadd.name }}</td></tr>
+</table>
+</div>
+<a style="font-size:75%" href=# onclick="toggleField('nameserver')">Add NS records</a>
+<table border="0" width="100%">
+ <colgroup>
+ <col width="200">
+ <col width="*">
+{% for mx in mxforms %}
+<tr><td><label for="id_mx">mx:</label></td> <td>{{ mx.priority }} {{ mx.mx }}</td></tr>
+{% endfor %}
+</table>
+<div id=mx>
+<table border="0" width="100%">
+ <colgroup>
+ <col width="200">
+ <col width="*">
+ <tr><td><label for="id_mx">mx:</label></td> <td>{{ mxadd.priority }} {{ mxadd.mx }}</td></tr>
+ <tr><td><label for="id_mx">mx:</label></td> <td>{{ mxadd.priority }} {{ mxadd.mx }}</td></tr>
+</table>
+</div>
+<a style="font-size:75%" href=# onclick="toggleField('mx')">Add MX records</a>
+<table border="0" width="100%">
+ <colgroup>
+ <col width="200">
+ <col width="*">
+{% for a in aforms %}
+<tr><td><label for="id_address">ip address:</label></td> <td>{{ a.ip_addr }}</td></tr>
+{% endfor %}
+</table>
+<div id=address>
+<table border="0" width="100%">
+ <colgroup>
+ <col width="200">
+ <col width="*">
+ <tr><td><label for="id_address">ip address:</label></td> <td>{{ addadd.ip_addr }}</td></tr>
+ <tr><td><label for="id_address">ip address:</label></td> <td>{{ addadd.ip_addr }}</td></tr>
+</table>
+</div>
+<a style="font-size:75%" href=# onclick="toggleField('address')">Add A records</a>
+<table border="0" width="100%">
+ <colgroup>
+ <col width="200">
+ <col width="*">
+<tr><td><label for="id_aux">aux:</label></td> <td>{{ form.aux }}</td></tr>
</table>
<p><input type="submit" value="Submit">
</form>
diff --git a/src/lib/Server/Hostbase/hostbase/webtemplates/zonenew.html b/src/lib/Server/Hostbase/hostbase/webtemplates/zonenew.html
index b3d12f9f8..a085b1433 100644
--- a/src/lib/Server/Hostbase/hostbase/webtemplates/zonenew.html
+++ b/src/lib/Server/Hostbase/hostbase/webtemplates/zonenew.html
@@ -14,54 +14,30 @@
{% endblock %}
{% block content %}
-
-<form name="zonedata" action="?sub=true" method="post">
+<form name="zonedata" action="" method="post">
<table border="0" width="100%">
<colgroup>
<col width="200">
<col width="*">
- <tr> <td> <b>zone</b></td>
- <td> <input name="zone" type="text" size="32"></td></tr>
- <tr> <td> <b>admin</b></td>
- <td> <input name="admin" type="text" size="32"></td></tr>
- <tr> <td> <b>primary_master</b></td>
- <td> <input name="primary_master" type="text" size="32"></td></tr>
- <tr> <td> <b>expire</b></td>
- <td> <input name="expire" type="text" size="32"></td></tr>
- <tr> <td> <b>retry</b></td>
- <td> <input name="retry" type="text" size="32" ></td></tr>
- <tr> <td> <b>refresh</b></td>
- <td> <input name="refresh" type="text" size="32"></td></tr>
- <tr> <td> <b>ttl</b></td>
- <td> <input name="ttl" type="text" size="32"></td></tr>
-
- <tr><td valign="top"> <b>nameservers</b></td>
- <td>
- {% for nameserver in nameservers %}
- <input name="nameserver{{ forloop.counter0 }}" type="text" size="32"><br>
- {% endfor %}
- </td></tr>
- <tr><td valign="top"> <b>mxs</b></td>
- <td>
- {% for mx in mxs %}
- <input name="priority{{ forloop.counter0 }}" type="text" size="6" >
- <input name="mx{{ forloop.counter0 }}" type="text" size="32" ><br>
- {% endfor %}
- </td></tr>
- <tr><td valign="top"> <b>A records</b></td>
- <td>
- {% for address in addresses %}
- <input name="address{{ forloop.counter0 }}" type="text" ><br>
- {% endfor %}
- </td></tr>
-
- <tr> <td valign="top"> <b>aux</b> (for information not generated by the database)</td>
- <td> <textarea rows="20" cols="80" name="aux">{{ zone.aux }}</textarea></td></tr>
- </td></tr>
-
+<tr><td><label for="id_zone">zone:</label></td> <td>{{ form.zone }}</td></tr>
+<tr><td><label for="id_admin">admin:</label></td> <td>{{ form.admin }}</td></tr>
+<tr><td><label for="id_primary_master">primary_master:</label></td> <td>{{ form.primary_master }}</td></tr>
+<tr><td><label for="id_expire">expire:</label></td> <td>{{ form.expire }}</td></tr>
+<tr><td><label for="id_retry">retry:</label></td> <td>{{ form.retry }}</td></tr>
+<tr><td><label for="id_refresh">refresh:</label></td> <td>{{ form.refresh }}</td></tr>
+<tr><td><label for="id_ttl">ttl:</label></td> <td>{{ form.ttl }}</td></tr>
+<tr><td><label for="id_name">nameserver:</label> <td>{{ nsform.name }}</td></tr>
+<tr><td><label for="id_name">nameserver:</label> <td>{{ nsform.name }}</td></tr>
+<tr><td><label for="id_name">nameserver:</label> <td>{{ nsform.name }}</td></tr>
+<tr><td><label for="id_name">nameserver:</label> <td>{{ nsform.name }}</td></tr>
+<tr><td><label for="id_mx">mx:</label> <td>{{ mxform.priority }} {{ mxform.mx }}</td></tr>
+<tr><td><label for="id_mx">mx:</label> <td>{{ mxform.priority }} {{ mxform.mx }}</td></tr>
+<tr><td><label for="id_mx">ip address:</label> <td>{{ aform.ip_addr }}</td></tr>
+<tr><td><label for="id_mx">ip address:</label> <td>{{ aform.ip_addr }}</td></tr>
+<tr><td><label for="id_aux">aux:<br>
+(information not generated from Hostbase)</label></td> <td>{{ form.aux }}</td></tr>
</table>
<p><input type="submit" value="Submit">
</form>
-
{% endblock %}