changeset 147:fee92e499d6d

Changed Data Forms implementation to support all field types and options in case of list-single and list-multi. Strip the options before returning meta data in disco#info request to a node. Fix error handling in disco#info handling to not mask unexpected exceptions.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 29 Jul 2005 15:38:29 +0000
parents b4490bdc77e5
children b03e5ad81173
files idavoll/data_form.py idavoll/pubsub.py
diffstat 2 files changed, 51 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/idavoll/data_form.py	Fri Jul 29 15:33:43 2005 +0000
+++ b/idavoll/data_form.py	Fri Jul 29 15:38:29 2005 +0000
@@ -3,39 +3,50 @@
 NS_X_DATA = 'jabber:x:data'
 
 class Field(domish.Element):
-    def __init__(self, type="text-single", var=None, label=None):
+    def __init__(self, type='text-single', var=None, label=None,
+                       value=None, values=[], options={}):
         domish.Element.__init__(self, (NS_X_DATA, 'field'))
-        self["type"] = type
+        self['type'] = type
         if var is not None:
-            self["var"] = var
+            self['var'] = var
         if label is not None:
-            self["label"] = label
+            self['label'] = label
+        if value is not None:
+            self.set_value(value)
+        else:
+            self.set_values(values)
+        if type in ['list-single', 'list-multi']:
+            for value, label in options.iteritems():
+                self.addChild(Option(value, label))
 
     def set_value(self, value):
-        # TODO: handle *-multi types
-
-        if self["type"] == 'boolean':
+        if self['type'] == 'boolean':
             value = str(int(bool(value)))
         else:
             value = str(value)
 
-        try:
-            value_element = self.value
-            value_element.children = []
-        except:
-            value_element = self.addElement("value")
+        value_element = self.value or self.addElement('value')
+        value_element.children = []
+        value_element.addContent(value)
+            
+    def set_values(self, values):
+        for value in values:
+            value = str(value)
+            self.addElement('value', content=value)
 
-        value_element.addContent(value)
+class Option(domish.Element):
+    def __init__(self, value, label=None):
+        domish.Element.__init__(self, (NS_X_DATA, 'option'))
+        if label is not None:
+            self['label'] = label
+        self.addElement('value', content=value)
 
 class Form(domish.Element):
     def __init__(self, type, form_type):
         domish.Element.__init__(self, (NS_X_DATA, 'x'),
                                 attribs={'type': type})
-        self.add_field_single(type="hidden", var="FORM_TYPE", value=form_type)
+        self.add_field(type='hidden', var='FORM_TYPE', values=[form_type])
 
-    def add_field_single(self, type="text-single", var=None, label=None,
-                         value=None):
-        field = Field(type, var, label)
-        if value is not None:
-            field.set_value(value)
-        self.addChild(field)
+    def add_field(self, type='text-single', var=None, label=None,
+                        value=None, values=[], options={}):
+        self.addChild(Field(type, var, label, value, values, options))
--- a/idavoll/pubsub.py	Fri Jul 29 15:33:43 2005 +0000
+++ b/idavoll/pubsub.py	Fri Jul 29 15:38:29 2005 +0000
@@ -153,12 +153,13 @@
 
             return defer.succeed(info)
         else:
-            try:
-                d = self.backend.get_node_type(node)
-                d.addCallback(self._add_identity, [], node)
-                d.addErrback(lambda _: [])
-            except storage.NodeNotFound:
-                return defer.succeed([])
+            def trap_not_found(result):
+                result.trap(storage.NodeNotFound)
+                return []
+
+            d = self.backend.get_node_type(node)
+            d.addCallback(self._add_identity, [], node)
+            d.addErrback(trap_not_found)
             return d
 
     def _add_identity(self, node_type, result_list, node):
@@ -169,12 +170,19 @@
 
     def _add_meta_data(self, meta_data, node_type, result_list):
         form = data_form.Form(type="result", form_type=NS_PUBSUB + "#meta-data")
+
         for meta_datum in meta_data:
-            form.add_field_single(**meta_datum)
-        form.add_field_single("text-single",
-                              "pubsub#node_type",
-                              "The type of node (collection or leaf)",
-                              node_type)
+            try:
+                del meta_datum['options']
+            except KeyError:
+                pass
+            
+            form.add_field(**meta_datum)
+
+        form.add_field("text-single",
+                       "pubsub#node_type",
+                       "The type of node (collection or leaf)",
+                       node_type)
         result_list.append(form)
         return result_list
 
@@ -376,7 +384,7 @@
                               form_type=NS_PUBSUB + "#node_config")
 
         for option in options:
-            form.add_field_single(**option)
+            form.add_field(**option)
 
         form.parent = configure
         configure.addChild(form)