annotate idavoll/data_form.py @ 151:f809b998c6f4

Add comments about uuid.py and the required Twisted modules.
author Ralph Meijer <ralphm@ik.nu>
date Wed, 05 Oct 2005 07:02:40 +0000
parents fee92e499d6d
children ea8b4189ae3b
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):
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
6 def __init__(self, type='text-single', var=None, label=None,
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
7 value=None, values=[], options={}):
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8 domish.Element.__init__(self, (NS_X_DATA, 'field'))
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
9 self['type'] = type
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10 if var is not None:
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
11 self['var'] = var
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 if label is not None:
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
13 self['label'] = label
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
14 if value is not None:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
15 self.set_value(value)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
16 else:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
17 self.set_values(values)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
18 if type in ['list-single', 'list-multi']:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
19 for value, label in options.iteritems():
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
20 self.addChild(Option(value, label))
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 def set_value(self, value):
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
23 if self['type'] == 'boolean':
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24 value = str(int(bool(value)))
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 else:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26 value = str(value)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
28 value_element = self.value or self.addElement('value')
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
29 value_element.children = []
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
30 value_element.addContent(value)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
31
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
32 def set_values(self, values):
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
33 for value in values:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
34 value = str(value)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
35 self.addElement('value', content=value)
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
37 class Option(domish.Element):
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
38 def __init__(self, value, label=None):
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
39 domish.Element.__init__(self, (NS_X_DATA, 'option'))
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
40 if label is not None:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
41 self['label'] = label
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
42 self.addElement('value', content=value)
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 class Form(domish.Element):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 def __init__(self, type, form_type):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46 domish.Element.__init__(self, (NS_X_DATA, 'x'),
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 attribs={'type': type})
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
48 self.add_field(type='hidden', var='FORM_TYPE', values=[form_type])
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
50 def add_field(self, type='text-single', var=None, label=None,
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
51 value=None, values=[], options={}):
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
52 self.addChild(Field(type, var, label, value, values, options))