Mercurial > libervia-pubsub
annotate idavoll/data_form.py @ 163:1701c0e2c707
Add --dbpass option for passing a password to the PostgreSQL backend.
Author: edwinm
Reviewer: ralphm
Fixes #1
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Thu, 07 Sep 2006 11:13:46 +0000 |
parents | 5191ba7c4df8 |
children |
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 | 4 from twisted.words.xish import domish |
100 | 5 |
6 NS_X_DATA = 'jabber:x:data' | |
7 | |
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 | 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 | 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 | 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 | 24 |
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 | 27 value = str(int(bool(value))) |
28 else: | |
29 value = str(value) | |
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 | 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 | 46 |
47 class Form(domish.Element): | |
48 def __init__(self, type, form_type): | |
49 domish.Element.__init__(self, (NS_X_DATA, 'x'), | |
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 | 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)) |