comparison 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
comparison
equal deleted inserted replaced
146:b4490bdc77e5 147:fee92e499d6d
1 from twisted.xish import domish 1 from twisted.xish import domish
2 2
3 NS_X_DATA = 'jabber:x:data' 3 NS_X_DATA = 'jabber:x:data'
4 4
5 class Field(domish.Element): 5 class Field(domish.Element):
6 def __init__(self, type="text-single", var=None, label=None): 6 def __init__(self, type='text-single', var=None, label=None,
7 value=None, values=[], options={}):
7 domish.Element.__init__(self, (NS_X_DATA, 'field')) 8 domish.Element.__init__(self, (NS_X_DATA, 'field'))
8 self["type"] = type 9 self['type'] = type
9 if var is not None: 10 if var is not None:
10 self["var"] = var 11 self['var'] = var
11 if label is not None: 12 if label is not None:
12 self["label"] = label 13 self['label'] = label
14 if value is not None:
15 self.set_value(value)
16 else:
17 self.set_values(values)
18 if type in ['list-single', 'list-multi']:
19 for value, label in options.iteritems():
20 self.addChild(Option(value, label))
13 21
14 def set_value(self, value): 22 def set_value(self, value):
15 # TODO: handle *-multi types 23 if self['type'] == 'boolean':
16
17 if self["type"] == 'boolean':
18 value = str(int(bool(value))) 24 value = str(int(bool(value)))
19 else: 25 else:
20 value = str(value) 26 value = str(value)
21 27
22 try: 28 value_element = self.value or self.addElement('value')
23 value_element = self.value 29 value_element.children = []
24 value_element.children = [] 30 value_element.addContent(value)
25 except: 31
26 value_element = self.addElement("value") 32 def set_values(self, values):
33 for value in values:
34 value = str(value)
35 self.addElement('value', content=value)
27 36
28 value_element.addContent(value) 37 class Option(domish.Element):
38 def __init__(self, value, label=None):
39 domish.Element.__init__(self, (NS_X_DATA, 'option'))
40 if label is not None:
41 self['label'] = label
42 self.addElement('value', content=value)
29 43
30 class Form(domish.Element): 44 class Form(domish.Element):
31 def __init__(self, type, form_type): 45 def __init__(self, type, form_type):
32 domish.Element.__init__(self, (NS_X_DATA, 'x'), 46 domish.Element.__init__(self, (NS_X_DATA, 'x'),
33 attribs={'type': type}) 47 attribs={'type': type})
34 self.add_field_single(type="hidden", var="FORM_TYPE", value=form_type) 48 self.add_field(type='hidden', var='FORM_TYPE', values=[form_type])
35 49
36 def add_field_single(self, type="text-single", var=None, label=None, 50 def add_field(self, type='text-single', var=None, label=None,
37 value=None): 51 value=None, values=[], options={}):
38 field = Field(type, var, label) 52 self.addChild(Field(type, var, label, value, values, options))
39 if value is not None:
40 field.set_value(value)
41 self.addChild(field)