view idavoll/data_form.py @ 154:bd8e58c73370

Update README and fix typo. - Adds informations about how to use Idavoll with jabberd 2.x - Updates dependency information - Updates implemented specification version
author Ralph Meijer <ralphm@ik.nu>
date Thu, 29 Jun 2006 08:38:24 +0000
parents ea8b4189ae3b
children 5191ba7c4df8
line wrap: on
line source

from twisted.words.xish import domish

NS_X_DATA = 'jabber:x:data'

class Field(domish.Element):
    def __init__(self, type='text-single', var=None, label=None,
                       value=None, values=[], options={}):
        domish.Element.__init__(self, (NS_X_DATA, 'field'))
        self['type'] = type
        if var is not None:
            self['var'] = var
        if label is not None:
            self['label'] = label
        if value is not None:
            self.set_value(value)
        else:
            self.set_values(values)
        if type in ['list-single', 'list-multi']:
            for value, label in options.iteritems():
                self.addChild(Option(value, label))

    def set_value(self, value):
        if self['type'] == 'boolean':
            value = str(int(bool(value)))
        else:
            value = str(value)

        value_element = self.value or self.addElement('value')
        value_element.children = []
        value_element.addContent(value)
            
    def set_values(self, values):
        for value in values:
            value = str(value)
            self.addElement('value', content=value)

class Option(domish.Element):
    def __init__(self, value, label=None):
        domish.Element.__init__(self, (NS_X_DATA, 'option'))
        if label is not None:
            self['label'] = label
        self.addElement('value', content=value)

class Form(domish.Element):
    def __init__(self, type, form_type):
        domish.Element.__init__(self, (NS_X_DATA, 'x'),
                                attribs={'type': type})
        self.add_field(type='hidden', var='FORM_TYPE', values=[form_type])

    def add_field(self, type='text-single', var=None, label=None,
                        value=None, values=[], options={}):
        self.addChild(Field(type, var, label, value, values, options))