Author: justi9
Date: 2007-11-07 11:11:13 -0500 (Wed, 07 Nov 2007)
New Revision: 1260
Modified:
mgmt/cumin/python/cumin/exchange.py
mgmt/cumin/python/cumin/queue.py
mgmt/cumin/python/wooly/__init__.py
mgmt/cumin/python/wooly/forms.py
mgmt/cumin/python/wooly/forms.strings
mgmt/notes/justin-todo.txt
Log:
Overhauls error handling. Puts an errors attribute on each widget.
Introduces an Error class for subclassing to create reusable error
types.
Also renames render_tabindex methods to tab_index.
Modified: mgmt/cumin/python/cumin/exchange.py
===================================================================
--- mgmt/cumin/python/cumin/exchange.py 2007-11-07 15:33:34 UTC (rev 1259)
+++ mgmt/cumin/python/cumin/exchange.py 2007-11-07 16:11:13 UTC (rev 1260)
@@ -216,23 +216,21 @@
self.add_child(self.fanout)
def validate(self, session):
- valid = True
+ error = None
name = self.exchange_name.get(session)
if name == "":
- valid = False
- self.exchange_name.add_error(session, """
- The exchange name is empty; it is required
- """)
+ error = EmptyInputError(exchange_name)
+ self.exchange_name.add_error(session, error)
elif " " in name:
- valid = False
- self.exchange_name.add_error(session, """
+ error = Error("""
The exchange name is invalid; allowed characters are
letters, digits, ".", and "_"
""")
+ self.exchange_name.add_error(session, error)
- return valid
+ return error is None
class ExchangeAdd(ExchangeForm):
def process_cancel(self, session, vhost):
Modified: mgmt/cumin/python/cumin/queue.py
===================================================================
--- mgmt/cumin/python/cumin/queue.py 2007-11-07 15:33:34 UTC (rev 1259)
+++ mgmt/cumin/python/cumin/queue.py 2007-11-07 16:11:13 UTC (rev 1260)
@@ -238,23 +238,21 @@
self.add_child(self.throughput)
def validate(self, session):
- valid = True
+ error = None
name = self.queue_name.get(session)
if name == "":
- valid = False
- self.queue_name.add_error(session, """
- The queue name is empty; it is required
- """)
+ error = EmptyInputError(self.queue_name)
+ self.queue_name.add_error(session, error)
elif " " in name:
- valid = False
- self.queue_name.add_error(session, """
+ error = Error("""
The queue name is invalid; allowed characters are
letters, digits, ".", and "_"
""")
+ self.queue_name.add_error(session, error)
- return valid
+ return error is None
class QueueAdd(QueueForm):
def process_cancel(self, session, vhost):
Modified: mgmt/cumin/python/wooly/__init__.py
===================================================================
--- mgmt/cumin/python/wooly/__init__.py 2007-11-07 15:33:34 UTC (rev 1259)
+++ mgmt/cumin/python/wooly/__init__.py 2007-11-07 16:11:13 UTC (rev 1260)
@@ -17,8 +17,13 @@
self.children = list()
self.attributes = list()
self.parameters = list()
+
self.template = Template(self, "html")
+ self.errors = Attribute(app, "errors")
+ self.errors.set_default(list())
+ self.add_attribute(self.errors)
+
self.cached_ancestors = None
self.cached_path = None
self.cached_page = None
@@ -97,6 +102,12 @@
if str:
return str
+ def get_errors(self, session):
+ return self.errors.get(session)
+
+ def add_error(self, session, error):
+ self.errors.get(session).append(error)
+
def get_saved_parameters(self, session):
params = list()
params.extend(self.parameters)
@@ -491,17 +502,6 @@
if key in self.values:
del self.values[key]
- # XXX this is a little out of line with other session methods in
- # that it uses widget as a key rather than having a method through
- # widget to do that
- def get_errors(self, widget):
- return self.errors.get(widget)
-
- def add_error(self, widget, error):
- errors = self.errors.setdefault(widget, list())
-
- errors.append(error)
-
def marshal(self):
page = self.marshal_page()
vars = self.marshal_url_vars()
@@ -622,6 +622,10 @@
class Writer(StringIOWriter):
pass
+class Error(object):
+ def __init__(self, message):
+ self.message = message
+
class Template(object):
def __init__(self, widget, key):
self.widget = widget
Modified: mgmt/cumin/python/wooly/forms.py
===================================================================
--- mgmt/cumin/python/wooly/forms.py 2007-11-07 15:33:34 UTC (rev 1259)
+++ mgmt/cumin/python/wooly/forms.py 2007-11-07 16:11:13 UTC (rev 1260)
@@ -44,7 +44,6 @@
writer.write("<input type='hidden' name='%s'
value='%s'/>" \
% (name, value))
-
class FormInput(Widget):
def __init__(self, app, name, form):
super(FormInput, self).__init__(app, name)
@@ -77,12 +76,6 @@
def set_default(self, default):
self.param.set_default(default)
- def add_error(self, session, error):
- session.add_error(self, error)
-
- def get_errors(self, session):
- return session.get_errors(self)
-
def set_tab_index(self, tab_index):
self.tab_index = tab_index
@@ -95,21 +88,37 @@
def render_value(self, session, object):
return self.param.marshal(self.param.get(session))
- # XXX do this proper
def render_errors(self, session, object):
- errors = self.get_errors(session)
+ writer = Writer()
+
+ if self.get_errors(session):
+ self.errors_tmp.render(session, object, writer)
- if errors:
- return "<ul class=\"errors\"><li>" + \
- "</li><li>".join(errors) + \
- "</li></ul>"
+ return writer.to_string()
- def render_tabindex(self, session, object):
+ def render_error_messages(self, session, object):
+ writer = Writer()
+
+ for error in self.get_errors(session):
+ self.errors_tmpl.render(session, error, writer)
+
+ return writer.to_string()
+
+ def render_error_message(self, session, error):
+ return error.message
+
+ def render_tab_index(self, session, object):
return self.tab_index
def render_disabled_attr(self, session, object):
return self.disabled and "disabled=\"disabled\"" or None
+class EmptyInputError(Error):
+ def __init__(self, input):
+ message = "Input '%s' is empty; it is required" % input.name
+
+ super(EmptyInputError, self).__init__(message)
+
class TextInput(FormInput):
def __init__(self, app, name, form):
super(TextInput, self).__init__(app, name, form)
Modified: mgmt/cumin/python/wooly/forms.strings
===================================================================
--- mgmt/cumin/python/wooly/forms.strings 2007-11-07 15:33:34 UTC (rev 1259)
+++ mgmt/cumin/python/wooly/forms.strings 2007-11-07 16:11:13 UTC (rev 1260)
@@ -1,26 +1,32 @@
[FormButton.html]
<button id="{id}" type="submit" name="{name}"
value="{value}" tabindex="{tabindex}"
{disabled_attr}>{content}</button>
+[FormInput.errors_html]
+<ul class="errors">{error_messages}</ul>
+
+[FormInput.error_message_html]
+<li>{error_message}</li>
+
[TextInput.html]
{errors}
-<input type="text" name="{name}" value="{value}"
tabindex="{tabindex}" {disabled_attr} size="{size}"/>
+<input type="text" name="{name}" value="{value}"
tabindex="{tab_index}" {disabled_attr} size="{size}"/>
[CheckboxInput.html]
-<input type="checkbox" name="{name}" value="{value}"
tabindex="{tabindex}" {checked_attr} {disabled_attr}/>
+<input type="checkbox" name="{name}" value="{value}"
tabindex="{tab_index}" {checked_attr} {disabled_attr}/>
[RadioInput.html]
-<input type="radio" name="{name}" value="{value}"
tabindex="{tabindex}" {checked_attr} {disabled_attr}/>
+<input type="radio" name="{name}" value="{value}"
tabindex="{tab_index}" {checked_attr} {disabled_attr}/>
[CheckboxInputSet.html]
{errors}{items}
[CheckboxInputSet.item_html]
-<input type="checkbox" name="{name}"
value="{item_value}" tabindex="{tabindex}" {item_checked_attr}
{disabled_attr}/>
+<input type="checkbox" name="{name}"
value="{item_value}" tabindex="{tab_index}" {item_checked_attr}
{disabled_attr}/>
{item_content}
[RadioInputSet.html]
{errors}{items}
[RadioInputSet.item_html]
-<input type="radio" name="{name}" value="{item_value}"
tabindex="{tabindex}" {item_checked_attr} {disabled_attr}/>
+<input type="radio" name="{name}" value="{item_value}"
tabindex="{tab_index}" {item_checked_attr} {disabled_attr}/>
{item_content}
Modified: mgmt/notes/justin-todo.txt
===================================================================
--- mgmt/notes/justin-todo.txt 2007-11-07 15:33:34 UTC (rev 1259)
+++ mgmt/notes/justin-todo.txt 2007-11-07 16:11:13 UTC (rev 1260)
@@ -2,8 +2,6 @@
* Add ability to send a test message to a queue
- * Use page attributes for session errors
-
* Queue: Add a msg enq rate msg deq rate chart
* Right now, non cumin pages don't print their stack traces in the
@@ -29,6 +27,8 @@
Deferred
+ * Rename Widget.name to .__name
+
* Make the status lights also be links to an appropriate view
- Defer until we know what we're going to link to