Mercurial > libervia-pubsub
diff idavoll/data_form.py @ 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 | 0228725b705b |
children | ea8b4189ae3b |
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))