summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0004
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2011-08-13 01:28:18 -0700
committerLance Stout <lancestout@gmail.com>2011-08-13 01:28:18 -0700
commit017d7ec62bf02c72729be668d1f8ef3b144222fb (patch)
tree03454418bbffc30e2a41a1de557155e4b7e3328d /sleekxmpp/plugins/xep_0004
parent76826b5495bc64d92ad4ffae3abca738c7b5e134 (diff)
downloadslixmpp-017d7ec62bf02c72729be668d1f8ef3b144222fb.tar.gz
slixmpp-017d7ec62bf02c72729be668d1f8ef3b144222fb.tar.bz2
slixmpp-017d7ec62bf02c72729be668d1f8ef3b144222fb.tar.xz
slixmpp-017d7ec62bf02c72729be668d1f8ef3b144222fb.zip
Add tests for setting a form's type to 'submit' or 'cancel'.
Form fields now remember their current type if the type is deleted. This allows for fields to properly format their values if set after the form has been changed to the 'submit' type.
Diffstat (limited to 'sleekxmpp/plugins/xep_0004')
-rw-r--r--sleekxmpp/plugins/xep_0004/stanza/field.py33
1 files changed, 21 insertions, 12 deletions
diff --git a/sleekxmpp/plugins/xep_0004/stanza/field.py b/sleekxmpp/plugins/xep_0004/stanza/field.py
index 9bb92311..43293bfd 100644
--- a/sleekxmpp/plugins/xep_0004/stanza/field.py
+++ b/sleekxmpp/plugins/xep_0004/stanza/field.py
@@ -29,8 +29,19 @@ class FormField(ElementBase):
multi_value_types = set(('hidden', 'jid-multi',
'list-multi', 'text-multi'))
+ def setup(self, xml=None):
+ if ElementBase.setup(self, xml):
+ self._type = None
+ else:
+ self._type = self['type']
+
+ def set_type(self, value):
+ self._set_attr('type', value)
+ if value:
+ self._type = value
+
def add_option(self, label='', value=''):
- if self['type'] in self.option_types:
+ if self._type in self.option_types:
opt = FieldOption(parent=self)
opt['label'] = label
opt['value'] = value
@@ -72,15 +83,15 @@ class FormField(ElementBase):
valsXML = self.xml.findall('{%s}value' % self.namespace)
if len(valsXML) == 0:
return None
- elif self['type'] == 'boolean':
+ elif self._type == 'boolean':
return valsXML[0].text in self.true_values
- elif self['type'] in self.multi_value_types:
+ elif self._type in self.multi_value_types:
values = []
for valXML in valsXML:
if valXML.text is None:
valXML.text = ''
values.append(valXML.text)
- if self['type'] == 'text-multi':
+ if self._type == 'text-multi':
values = "\n".join(values)
return values
else:
@@ -113,7 +124,7 @@ class FormField(ElementBase):
del self['value']
valXMLName = '{%s}value' % self.namespace
- if self['type'] == 'boolean':
+ if self._type == 'boolean':
if value in self.true_values:
valXML = ET.Element(valXMLName)
valXML.text = '1'
@@ -122,14 +133,12 @@ class FormField(ElementBase):
valXML = ET.Element(valXMLName)
valXML.text = '0'
self.xml.append(valXML)
- elif self['type'] in self.multi_value_types or not self['type']:
+ elif self._type in self.multi_value_types or self._type in ('', None):
if not isinstance(value, list):
- if self['type'] in self.multi_line_types:
- value = value.split('\n')
- else:
- value = [value]
+ value = value.replace('\r', '')
+ value = value.split('\n')
for val in value:
- if self['type'] in ['', None] and val in self.true_values:
+ if self._type in ('', None) and val in self.true_values:
val = '1'
valXML = ET.Element(valXMLName)
valXML.text = val
@@ -137,7 +146,7 @@ class FormField(ElementBase):
else:
if isinstance(value, list):
raise ValueError("Cannot add multiple values " + \
- "to a %s field." % self['type'])
+ "to a %s field." % self._type)
valXML = ET.Element(valXMLName)
valXML.text = value
self.xml.append(valXML)