annotate idavoll/data_form.py @ 113:dfef919aaf1b

Fix interpretations of JIDs. Catch more specific exceptions only.
author Ralph Meijer <ralphm@ik.nu>
date Fri, 08 Apr 2005 16:33:20 +0000
parents 0228725b705b
children fee92e499d6d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.xish import domish
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 NS_X_DATA = 'jabber:x:data'
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 class Field(domish.Element):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 def __init__(self, type="text-single", var=None, label=None):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 domish.Element.__init__(self, (NS_X_DATA, 'field'))
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8 self["type"] = type
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
9 if var is not None:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10 self["var"] = var
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 if label is not None:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 self["label"] = label
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14 def set_value(self, value):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 # TODO: handle *-multi types
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17 if self["type"] == 'boolean':
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18 value = str(int(bool(value)))
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19 else:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20 value = str(value)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 try:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 value_element = self.value
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24 value_element.children = []
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 except:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26 value_element = self.addElement("value")
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 value_element.addContent(value)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 class Form(domish.Element):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31 def __init__(self, type, form_type):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 domish.Element.__init__(self, (NS_X_DATA, 'x'),
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33 attribs={'type': type})
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34 self.add_field_single(type="hidden", var="FORM_TYPE", value=form_type)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36 def add_field_single(self, type="text-single", var=None, label=None,
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 value=None):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 field = Field(type, var, label)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39 if value is not None:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40 field.set_value(value)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41 self.addChild(field)