annotate idavoll/data_form.py @ 166:5abb017bd687

Oh, and fix README. Really release 0.6.0.
author Ralph Meijer <ralphm@ik.nu>
date Thu, 18 Jan 2007 14:08:32 +0000
parents 5191ba7c4df8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
155
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 152
diff changeset
1 # Copyright (c) 2003-2006 Ralph Meijer
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 152
diff changeset
2 # See LICENSE for details.
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 152
diff changeset
3
152
ea8b4189ae3b Update to Twisted Words 0.3.0.
Ralph Meijer <ralphm@ik.nu>
parents: 147
diff changeset
4 from twisted.words.xish import domish
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 NS_X_DATA = 'jabber:x:data'
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8 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
9 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
10 value=None, values=[], options={}):
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 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
12 self['type'] = type
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13 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
14 self['var'] = var
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15 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
16 self['label'] = label
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
17 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
18 self.set_value(value)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
19 else:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
20 self.set_values(values)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
21 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
22 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
23 self.addChild(Option(value, label))
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 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
26 if self['type'] == 'boolean':
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27 value = str(int(bool(value)))
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 else:
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 value = str(value)
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
31 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
32 value_element.children = []
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
33 value_element.addContent(value)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
34
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
35 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
36 for value in values:
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
37 value = str(value)
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
38 self.addElement('value', content=value)
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
40 class Option(domish.Element):
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
41 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
42 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
43 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
44 self['label'] = label
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
45 self.addElement('value', content=value)
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 class Form(domish.Element):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 def __init__(self, type, form_type):
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 domish.Element.__init__(self, (NS_X_DATA, 'x'),
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 attribs={'type': type})
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
51 self.add_field(type='hidden', var='FORM_TYPE', values=[form_type])
100
0228725b705b Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52
147
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
53 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
54 value=None, values=[], options={}):
fee92e499d6d Changed Data Forms implementation to support all field types and
Ralph Meijer <ralphm@ik.nu>
parents: 100
diff changeset
55 self.addChild(Field(type, var, label, value, values, options))