summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0004
diff options
context:
space:
mode:
authorMike Taylor <bear42@gmail.com>2015-09-18 15:11:47 -0400
committerMike Taylor <bear42@gmail.com>2015-09-18 15:11:47 -0400
commit5525ef22851d42246782fa0adf1dc2b40e493840 (patch)
tree276bd6aab21a1528b7bac772428182fb4b400ca7 /sleekxmpp/plugins/xep_0004
parentda14ce16ec564a11394dc0ee6ee446a4dfa97a5d (diff)
parent0b14ef82d4058925e7e9db22038777d4654e8199 (diff)
downloadslixmpp-5525ef22851d42246782fa0adf1dc2b40e493840.tar.gz
slixmpp-5525ef22851d42246782fa0adf1dc2b40e493840.tar.bz2
slixmpp-5525ef22851d42246782fa0adf1dc2b40e493840.tar.xz
slixmpp-5525ef22851d42246782fa0adf1dc2b40e493840.zip
Merge pull request #395 from rerobins/refactor_forms
XEP_0004: Data Forms use register_stanza_plugin
Diffstat (limited to 'sleekxmpp/plugins/xep_0004')
-rw-r--r--sleekxmpp/plugins/xep_0004/stanza/field.py4
-rw-r--r--sleekxmpp/plugins/xep_0004/stanza/form.py42
2 files changed, 32 insertions, 14 deletions
diff --git a/sleekxmpp/plugins/xep_0004/stanza/field.py b/sleekxmpp/plugins/xep_0004/stanza/field.py
index 51f85995..73e48758 100644
--- a/sleekxmpp/plugins/xep_0004/stanza/field.py
+++ b/sleekxmpp/plugins/xep_0004/stanza/field.py
@@ -13,8 +13,9 @@ class FormField(ElementBase):
namespace = 'jabber:x:data'
name = 'field'
plugin_attrib = 'field'
+ plugin_multi_attrib = 'fields'
interfaces = set(('answer', 'desc', 'required', 'value',
- 'options', 'label', 'type', 'var'))
+ 'label', 'type', 'var'))
sub_interfaces = set(('desc',))
plugin_tag_map = {}
plugin_attrib_map = {}
@@ -165,6 +166,7 @@ class FieldOption(ElementBase):
plugin_attrib = 'option'
interfaces = set(('label', 'value'))
sub_interfaces = set(('value',))
+ plugin_multi_attrib = 'options'
FormField.addOption = FormField.add_option
diff --git a/sleekxmpp/plugins/xep_0004/stanza/form.py b/sleekxmpp/plugins/xep_0004/stanza/form.py
index 1d733760..3dcc7821 100644
--- a/sleekxmpp/plugins/xep_0004/stanza/form.py
+++ b/sleekxmpp/plugins/xep_0004/stanza/form.py
@@ -9,7 +9,7 @@
import copy
import logging
-from sleekxmpp.thirdparty import OrderedDict
+from sleekxmpp.thirdparty import OrderedDict, OrderedSet
from sleekxmpp.xmlstream import ElementBase, ET
from sleekxmpp.plugins.xep_0004.stanza import FormField
@@ -22,8 +22,7 @@ class Form(ElementBase):
namespace = 'jabber:x:data'
name = 'x'
plugin_attrib = 'form'
- interfaces = set(('fields', 'instructions', 'items',
- 'reported', 'title', 'type', 'values'))
+ interfaces = OrderedSet(('instructions', 'reported', 'title', 'type', 'items', ))
sub_interfaces = set(('title',))
form_types = set(('cancel', 'form', 'result', 'submit'))
@@ -43,12 +42,12 @@ class Form(ElementBase):
@property
def field(self):
- return self['fields']
+ return self.get_fields()
def set_type(self, ftype):
self._set_attr('type', ftype)
if ftype == 'submit':
- fields = self['fields']
+ fields = self.get_fields()
for var in fields:
field = fields[var]
del field['type']
@@ -74,7 +73,8 @@ class Form(ElementBase):
field['desc'] = desc
field['required'] = required
if options is not None:
- field['options'] = options
+ for option in options:
+ field.add_option(**option)
else:
del field['type']
self.append(field)
@@ -169,7 +169,7 @@ class Form(ElementBase):
def get_reported(self):
fields = OrderedDict()
xml = self.xml.findall('{%s}reported/{%s}field' % (self.namespace,
- FormField.namespace))
+ FormField.namespace))
for field in xml:
field = FormField(xml=field)
fields[field['var']] = field
@@ -177,7 +177,7 @@ class Form(ElementBase):
def get_values(self):
values = OrderedDict()
- fields = self['fields']
+ fields = self.get_fields()
for var in fields:
values[var] = fields[var]['value']
return values
@@ -219,17 +219,33 @@ class Form(ElementBase):
self.add_item(item)
def set_reported(self, reported):
+ """
+ This either needs a dictionary or dictionaries or a dictionary of form fields.
+ :param reported:
+ :return:
+ """
for var in reported:
field = reported[var]
- field['var'] = var
- self.add_reported(var, **field)
+
+ if isinstance(field, dict):
+ self.add_reported(**field)
+ else:
+ reported = self.xml.find('{%s}reported' % self.namespace)
+ if reported is None:
+ reported = ET.Element('{%s}reported' % self.namespace)
+ self.xml.append(reported)
+
+ fieldXML = ET.Element('{%s}field' % FormField.namespace)
+ reported.append(fieldXML)
+ new_field = FormField(xml=fieldXML)
+ new_field.values = field.values
def set_values(self, values):
- fields = self['fields']
+ fields = self.get_fields()
for field in values:
- if field not in fields:
+ if field not in self.get_fields():
fields[field] = self.add_field(var=field)
- fields[field]['value'] = values[field]
+ self.get_fields()[field]['value'] = values[field]
def merge(self, other):
new = copy.copy(self)