Author: justi9
Date: 2008-02-29 10:25:05 -0500 (Fri, 29 Feb 2008)
New Revision: 1740
Modified:
mgmt/cumin/python/cumin/broker.py
mgmt/cumin/python/cumin/broker.strings
Log:
Add broker add form validation. Check for empty fields and that the
broker name is unique.
Modified: mgmt/cumin/python/cumin/broker.py
===================================================================
--- mgmt/cumin/python/cumin/broker.py 2008-02-28 18:42:43 UTC (rev 1739)
+++ mgmt/cumin/python/cumin/broker.py 2008-02-29 15:25:05 UTC (rev 1740)
@@ -532,6 +532,9 @@
self.add_parameter(self.names)
self.add_form_parameter(self.names)
+ self.name_errors = self.Errors(self, "name_errors")
+ self.add_attribute(self.name_errors)
+
self.addr_param = Parameter(app, "addr_param")
self.add_parameter(self.addr_param)
self.add_form_parameter(self.addr_param)
@@ -540,6 +543,9 @@
self.add_parameter(self.addrs)
self.add_form_parameter(self.addrs)
+ self.addr_errors = self.Errors(self, "addr_errors")
+ self.add_attribute(self.addr_errors)
+
self.group_param = BrokerGroupParameter(app, "group_param")
self.group_param.set_default(None)
self.add_parameter(self.group_param)
@@ -559,6 +565,10 @@
self.more = self.MoreEntries(app, "more", self)
self.add_child(self.more)
+ class Errors(Attribute):
+ def get_default(self, session):
+ return dict()
+
def process_display(self, session, object):
if self.more.get(session):
self.fields.set(session, self.fields.get(session) + 3)
@@ -579,17 +589,29 @@
if len(names) > index:
return names[index]
+ def render_field_name_errors(self, session, index):
+ errors = self.name_errors.get(session)
+ if index in errors:
+ return "<ul
class=\"errors\"><li>%s</li></ul>" % \
+ "</li><li>".join(errors[index])
+
def render_field_address_name(self, session, object):
return self.addrs.path()
- def render_field_group_name(self, session, object):
- return self.groups.path()
-
def render_field_address_value(self, session, index):
addrs = self.addrs.get(session)
if len(addrs) > index:
return addrs[index]
+ def render_field_address_errors(self, session, index):
+ errors = self.addr_errors.get(session)
+ if index in errors:
+ return "<ul
class=\"errors\"><li>%s</li></ul>" % \
+ "</li><li>".join(errors[index])
+
+ def render_field_group_name(self, session, object):
+ return self.groups.path()
+
def render_groups(self, session, index):
writer = Writer()
@@ -632,35 +654,59 @@
names = self.names.get(session)
groups = self.groups.get(session)
- for i in range(0, len(addrs)):
- addr = addrs[i]
+ if self.validate(session, addrs, names, groups):
+ for i in range(0, len(addrs)):
+ addr = addrs[i]
- if addr:
- name = names[i]
+ if addr:
+ name = names[i]
- elems = addr.split(":")
+ elems = addr.split(":")
- if len(elems) > 1:
- host, port = elems[0], int(elems[1])
- else:
- host, port = elems[0], 5672
+ if len(elems) > 1:
+ host, port = elems[0], int(elems[1])
+ else:
+ host, port = elems[0], 5672
- args = {
- "name": name,
- "host": host,
- "port": port
- }
+ args = {
+ "name": name,
+ "host": host,
+ "port": port
+ }
- reg = action.invoke(None, args);
+ reg = action.invoke(None, args);
- if len(groups) > i:
- group = groups[i]
+ if len(groups) > i:
+ group = groups[i]
- if group:
- reg.addBrokerGroup(group)
+ if group:
+ reg.addBrokerGroup(group)
- self.process_cancel(session, model)
+ self.process_cancel(session, model)
+ def validate(self, session, addrs, names, groups):
+ nerrs = self.name_errors.get(session)
+ aerrs = self.addr_errors.get(session)
+
+ for i in range(0, len(addrs)):
+ addr, name = addrs[i], names[i]
+
+ if not addr and not name:
+ pass # It's just an empty row
+ else:
+ if not name:
+ errs = nerrs.setdefault(i, list())
+ errs.append("The name field is empty; it is required")
+ elif BrokerRegistration.selectBy(name=name).count():
+ errs = nerrs.setdefault(i, list())
+ errs.append("A broker called '%s' already exists" %
name)
+
+ if not addr:
+ errs = aerrs.setdefault(i, list())
+ errs.append("The address field is empty; it is required")
+
+ return not len(nerrs) and not len(aerrs)
+
# XXX I would like to move this to brokergroup.py, once I fix the
# python import issues
class BrokerGroupInputSet(CheckboxInputSet):
Modified: mgmt/cumin/python/cumin/broker.strings
===================================================================
--- mgmt/cumin/python/cumin/broker.strings 2008-02-28 18:42:43 UTC (rev 1739)
+++ mgmt/cumin/python/cumin/broker.strings 2008-02-29 15:25:05 UTC (rev 1740)
@@ -220,11 +220,13 @@
</div>
<div class="body">
<fieldset>
+ {errors}
+
<table class="BrokerSetForm">
<tr>
<th>Name</th>
<th>
- Domain Name or IP Address
+ Address
<br/>
<span class="example">Examples:
example.net,
example.net:5762, 172.16.82.10</span>
</th>
@@ -255,9 +257,15 @@
[BrokerSetForm.field_html]
<tr>
- <td><input type="text" name="{field_name_name}"
value="{field_name_value}" size="15"
tabindex="100"/></td>
- <td><input type="text" name="{field_address_name}"
value="{field_address_value}" size="35"
tabindex="100"/></td>
<td>
+ <input type="text" name="{field_name_name}"
value="{field_name_value}" size="15" tabindex="100"/>
+ {field_name_errors}
+ </td>
+ <td>
+ <input type="text" name="{field_address_name}"
value="{field_address_value}" size="35" tabindex="100"/>
+ {field_address_errors}
+ </td>
+ <td>
<select name="{field_group_name}" tabindex="100">
<option value="__none__">None</option>
{groups}