Mercurial > libervia-pubsub
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 |
rev | line source |
---|---|
100 | 1 from twisted.xish import domish |
2 | |
3 NS_X_DATA = 'jabber:x:data' | |
4 | |
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 | 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 | 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 | 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 | 21 |
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 | 24 value = str(int(bool(value))) |
25 else: | |
26 value = str(value) | |
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 | 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 | 43 |
44 class Form(domish.Element): | |
45 def __init__(self, type, form_type): | |
46 domish.Element.__init__(self, (NS_X_DATA, 'x'), | |
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 | 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)) |