2018-07-07 19:36:24 +02:00
|
|
|
# Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org>
|
|
|
|
# Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# Copyright (C) 2007 Stephan Erb <steve-e AT h3c.de>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-15 05:20:23 +02:00
|
|
|
|
2018-07-21 14:35:28 +02:00
|
|
|
# XEP-0004: Data Forms
|
2006-07-05 18:35:22 +02:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import helpers
|
2006-07-05 18:35:22 +02:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
# exceptions used in this module
|
2018-07-07 19:36:24 +02:00
|
|
|
class Error(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
# when we get nbxmpp.Node which we do not understand
|
2018-07-07 19:36:24 +02:00
|
|
|
class UnknownDataForm(Error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
# when we get nbxmpp.Node which contains bad fields
|
2018-07-07 19:36:24 +02:00
|
|
|
class WrongFieldValue(Error):
|
|
|
|
pass
|
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
|
|
|
|
# helper class to change class of already existing object
|
2012-12-09 21:37:51 +01:00
|
|
|
class ExtendedNode(nbxmpp.Node, object):
|
2010-02-08 15:08:40 +01:00
|
|
|
@classmethod
|
2018-07-07 19:36:24 +02:00
|
|
|
def __new__(cls, *a, **b):
|
2010-02-08 15:08:40 +01:00
|
|
|
if 'extend' not in b.keys() or not b['extend']:
|
|
|
|
return object.__new__(cls)
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
extend = b['extend']
|
|
|
|
assert issubclass(cls, extend.__class__)
|
|
|
|
extend.__class__ = cls
|
|
|
|
return extend
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
# helper to create fields from scratch
|
|
|
|
def Field(typ, **attrs):
|
2010-02-08 15:08:40 +01:00
|
|
|
''' Helper function to create a field of given type. '''
|
|
|
|
f = {
|
2018-07-07 19:36:24 +02:00
|
|
|
'boolean': BooleanField,
|
|
|
|
'fixed': StringField,
|
|
|
|
'hidden': StringField,
|
|
|
|
'text-private': StringField,
|
|
|
|
'text-single': StringField,
|
|
|
|
'jid-multi': JidMultiField,
|
|
|
|
'jid-single': JidSingleField,
|
|
|
|
'list-multi': ListMultiField,
|
|
|
|
'list-single': ListSingleField,
|
|
|
|
'text-multi': TextMultiField,
|
2010-02-08 15:08:40 +01:00
|
|
|
}[typ](typ=typ, **attrs)
|
|
|
|
return f
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
def ExtendField(node):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Helper function to extend a node to field of appropriate type
|
|
|
|
"""
|
|
|
|
# when validation (XEP-122) will go in, we could have another classes
|
|
|
|
# like DateTimeField - so that dicts in Field() and ExtendField() will
|
|
|
|
# be different...
|
2018-07-07 19:36:24 +02:00
|
|
|
typ = node.getAttr('type')
|
2010-02-08 15:08:40 +01:00
|
|
|
f = {
|
2018-07-07 19:36:24 +02:00
|
|
|
'boolean': BooleanField,
|
|
|
|
'fixed': StringField,
|
|
|
|
'hidden': StringField,
|
|
|
|
'text-private': StringField,
|
|
|
|
'text-single': StringField,
|
|
|
|
'jid-multi': JidMultiField,
|
|
|
|
'jid-single': JidSingleField,
|
|
|
|
'list-multi': ListMultiField,
|
|
|
|
'list-single': ListSingleField,
|
|
|
|
'text-multi': TextMultiField,
|
2010-02-08 15:08:40 +01:00
|
|
|
}
|
|
|
|
if typ not in f:
|
|
|
|
typ = 'text-single'
|
|
|
|
return f[typ](extend=node)
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 21:10:37 +01:00
|
|
|
def ExtendForm(node):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Helper function to extend a node to form of appropriate type
|
|
|
|
"""
|
|
|
|
if node.getTag('reported') is not None:
|
|
|
|
return MultipleDataForm(extend=node)
|
|
|
|
else:
|
|
|
|
return SimpleDataForm(extend=node)
|
2006-11-18 21:10:37 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class DataField(ExtendedNode):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Keeps data about one field - var, field type, labels, instructions... Base
|
|
|
|
class for different kinds of fields. Use Field() function to construct one
|
|
|
|
of these
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, typ=None, var=None, value=None, label=None, desc=None,
|
2018-07-07 19:36:24 +02:00
|
|
|
required=False, options=None, extend=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if extend is None:
|
|
|
|
ExtendedNode.__init__(self, 'field')
|
|
|
|
|
2012-08-22 12:55:57 +02:00
|
|
|
self.type_ = typ
|
2010-02-08 15:08:40 +01:00
|
|
|
self.var = var
|
|
|
|
if value is not None:
|
|
|
|
self.value = value
|
|
|
|
if label is not None:
|
|
|
|
self.label = label
|
2018-07-07 19:36:24 +02:00
|
|
|
if desc is not None:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.desc = desc
|
|
|
|
self.required = required
|
|
|
|
self.options = options
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def type_(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Type of field. Recognized values are: 'boolean', 'fixed', 'hidden',
|
|
|
|
'jid-multi', 'jid-single', 'list-multi', 'list-single', 'text-multi',
|
|
|
|
'text-private', 'text-single'. If you set this to something different,
|
|
|
|
DataField will store given name, but treat all data as text-single
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
t = self.getAttr('type')
|
|
|
|
if t is None:
|
|
|
|
return 'text-single'
|
|
|
|
return t
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@type_.setter
|
|
|
|
def type_(self, value):
|
|
|
|
assert isinstance(value, str)
|
|
|
|
self.setAttr('type', value)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def var(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Field identifier
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getAttr('var')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@var.setter
|
|
|
|
def var(self, value):
|
|
|
|
assert isinstance(value, str)
|
|
|
|
self.setAttr('var', value)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@var.deleter
|
|
|
|
def var(self):
|
|
|
|
self.delAttr('var')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def label(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Human-readable field name
|
|
|
|
"""
|
2018-07-07 19:36:24 +02:00
|
|
|
label_ = self.getAttr('label')
|
|
|
|
if not label_:
|
|
|
|
label_ = self.var
|
|
|
|
return label_
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@label.setter
|
|
|
|
def label(self, value):
|
|
|
|
assert isinstance(value, str)
|
|
|
|
self.setAttr('label', value)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@label.deleter
|
|
|
|
def label(self):
|
|
|
|
if self.getAttr('label'):
|
|
|
|
self.delAttr('label')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def description(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Human-readable description of field meaning
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getTagData('desc') or ''
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@description.setter
|
|
|
|
def description(self, value):
|
|
|
|
assert isinstance(value, str)
|
|
|
|
if value == '':
|
|
|
|
del self.description
|
|
|
|
else:
|
|
|
|
self.setTagData('desc', value)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@description.deleter
|
|
|
|
def description(self):
|
|
|
|
t = self.getTag('desc')
|
|
|
|
if t is not None:
|
|
|
|
self.delChild(t)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def required(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Controls whether this field required to fill. Boolean
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return bool(self.getTag('required'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@required.setter
|
|
|
|
def required(self, value):
|
|
|
|
t = self.getTag('required')
|
|
|
|
if t and not value:
|
|
|
|
self.delChild(t)
|
|
|
|
elif not t and value:
|
|
|
|
self.addChild('required')
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def media(self):
|
2010-06-27 23:09:07 +02:00
|
|
|
"""
|
|
|
|
Media data
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
media = self.getTag('media', namespace=nbxmpp.NS_DATA_MEDIA)
|
|
|
|
if media:
|
|
|
|
return Media(media)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@media.setter
|
|
|
|
def media(self, value):
|
|
|
|
del self.media
|
|
|
|
self.addChild(node=value)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@media.deleter
|
|
|
|
def media(self):
|
|
|
|
t = self.getTag('media')
|
|
|
|
if t is not None:
|
|
|
|
self.delChild(t)
|
2012-08-22 12:55:57 +02:00
|
|
|
|
2010-08-10 16:08:06 +02:00
|
|
|
def is_valid(self):
|
|
|
|
return True
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
class Uri(nbxmpp.Node):
|
2010-06-27 23:09:07 +02:00
|
|
|
def __init__(self, uri_tag):
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.Node.__init__(self, node=uri_tag)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def type_(self):
|
2010-06-27 23:09:07 +02:00
|
|
|
"""
|
|
|
|
uri type
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getAttr('type')
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@type_.setter
|
|
|
|
def type_(self, value):
|
|
|
|
self.setAttr('type', value)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@type_.deleter
|
|
|
|
def type_(self):
|
|
|
|
self.delAttr('type')
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def uri_data(self):
|
2010-06-27 23:09:07 +02:00
|
|
|
"""
|
|
|
|
uri data
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getData()
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@uri_data.setter
|
|
|
|
def uri_data(self, value):
|
|
|
|
self.setData(value)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@uri_data.deleter
|
|
|
|
def uri_data(self):
|
|
|
|
self.setData(None)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
class Media(nbxmpp.Node):
|
2010-06-27 23:09:07 +02:00
|
|
|
def __init__(self, media_tag):
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.Node.__init__(self, node=media_tag)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def uris(self):
|
2010-06-27 23:09:07 +02:00
|
|
|
"""
|
|
|
|
URIs of the media element.
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return map(Uri, self.getTags('uri'))
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@uris.setter
|
|
|
|
def uris(self, value):
|
|
|
|
del self.uris
|
|
|
|
for uri in value:
|
|
|
|
self.addChild(node=uri)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@uris.deleter
|
|
|
|
def uris(self):
|
|
|
|
for element in self.getTags('uri'):
|
|
|
|
self.delChild(element)
|
2010-06-27 23:09:07 +02:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class BooleanField(DataField):
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def value(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Value of field. May contain True, False or None
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
v = self.getTagData('value')
|
|
|
|
if v in ('0', 'false'):
|
|
|
|
return False
|
|
|
|
if v in ('1', 'true'):
|
|
|
|
return True
|
|
|
|
if v is None:
|
2018-07-07 19:36:24 +02:00
|
|
|
return False # default value is False
|
2017-08-27 21:41:27 +02:00
|
|
|
raise WrongFieldValue
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@value.setter
|
|
|
|
def value(self, value):
|
|
|
|
self.setTagData('value', value and '1' or '0')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@value.deleter
|
|
|
|
def value(self):
|
|
|
|
t = self.getTag('value')
|
|
|
|
if t is not None:
|
|
|
|
self.delChild(t)
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class StringField(DataField):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Covers fields of types: fixed, hidden, text-private, text-single
|
|
|
|
"""
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def value(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2013-01-01 21:06:16 +01:00
|
|
|
Value of field. May be any string
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getTagData('value') or ''
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@value.setter
|
|
|
|
def value(self, value):
|
|
|
|
assert isinstance(value, str)
|
|
|
|
if value == '' and not self.required:
|
|
|
|
del self.value
|
|
|
|
return
|
|
|
|
self.setTagData('value', value)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@value.deleter
|
|
|
|
def value(self):
|
|
|
|
try:
|
|
|
|
self.delChild(self.getTag('value'))
|
2018-07-07 19:36:24 +02:00
|
|
|
except ValueError: # if there already were no value tag
|
2017-08-27 21:41:27 +02:00
|
|
|
pass
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class ListField(DataField):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Covers fields of types: jid-multi, jid-single, list-multi, list-single
|
|
|
|
"""
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def options(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Options
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
options = []
|
|
|
|
for element in self.getTags('option'):
|
|
|
|
v = element.getTagData('value')
|
|
|
|
if v is None:
|
|
|
|
raise WrongFieldValue
|
2018-07-07 19:36:24 +02:00
|
|
|
label = element.getAttr('label')
|
|
|
|
if not label:
|
|
|
|
label = v
|
|
|
|
options.append((label, v))
|
2017-08-27 21:41:27 +02:00
|
|
|
return options
|
|
|
|
|
|
|
|
@options.setter
|
|
|
|
def options(self, values):
|
|
|
|
del self.options
|
|
|
|
for value, label in values:
|
2018-07-07 19:36:24 +02:00
|
|
|
self.addChild('option',
|
|
|
|
{'label': label}).setTagData('value', value)
|
2017-08-27 21:41:27 +02:00
|
|
|
|
|
|
|
@options.deleter
|
|
|
|
def options(self):
|
|
|
|
for element in self.getTags('option'):
|
|
|
|
self.delChild(element)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def iter_options(self):
|
|
|
|
for element in self.iterTags('option'):
|
|
|
|
v = element.getTagData('value')
|
|
|
|
if v is None:
|
|
|
|
raise WrongFieldValue
|
2018-07-07 19:36:24 +02:00
|
|
|
label = element.getAttr('label')
|
|
|
|
if not label:
|
|
|
|
label = v
|
|
|
|
yield (v, label)
|
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
|
|
|
|
class ListSingleField(ListField, StringField):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-08-10 16:08:06 +02:00
|
|
|
Covers list-single field
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-08-11 09:31:41 +02:00
|
|
|
def is_valid(self):
|
|
|
|
if not self.required:
|
|
|
|
return True
|
|
|
|
if not self.value:
|
|
|
|
return False
|
2010-08-12 21:37:26 +02:00
|
|
|
return True
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2010-08-10 16:08:06 +02:00
|
|
|
class JidSingleField(ListSingleField):
|
|
|
|
"""
|
|
|
|
Covers jid-single fields
|
|
|
|
"""
|
|
|
|
def is_valid(self):
|
|
|
|
if self.value:
|
|
|
|
try:
|
|
|
|
helpers.parse_jid(self.value)
|
|
|
|
return True
|
2018-07-07 19:36:24 +02:00
|
|
|
except Exception:
|
2010-08-10 16:08:06 +02:00
|
|
|
return False
|
|
|
|
if self.required:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class ListMultiField(ListField):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-08-10 16:08:06 +02:00
|
|
|
Covers list-multi fields
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def values(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Values held in field
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
values = []
|
|
|
|
for element in self.getTags('value'):
|
|
|
|
values.append(element.getData())
|
|
|
|
return values
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@values.setter
|
|
|
|
def values(self, values):
|
|
|
|
del self.values
|
|
|
|
for value in values:
|
|
|
|
self.addChild('value').setData(value)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@values.deleter
|
|
|
|
def values(self):
|
|
|
|
for element in self.getTags('value'):
|
|
|
|
self.delChild(element)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def iter_values(self):
|
|
|
|
for element in self.getTags('value'):
|
|
|
|
yield element.getData()
|
2012-08-22 12:55:57 +02:00
|
|
|
|
2010-08-11 09:31:41 +02:00
|
|
|
def is_valid(self):
|
|
|
|
if not self.required:
|
|
|
|
return True
|
|
|
|
if not self.values:
|
|
|
|
return False
|
2010-08-12 21:42:15 +02:00
|
|
|
return True
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2010-08-10 16:08:06 +02:00
|
|
|
class JidMultiField(ListMultiField):
|
|
|
|
"""
|
|
|
|
Covers jid-multi fields
|
|
|
|
"""
|
|
|
|
def is_valid(self):
|
|
|
|
if len(self.values):
|
|
|
|
for value in self.values:
|
|
|
|
try:
|
2010-08-16 16:13:55 +02:00
|
|
|
helpers.parse_jid(value)
|
2018-07-07 19:36:24 +02:00
|
|
|
except Exception:
|
2010-08-10 16:08:06 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
if self.required:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class TextMultiField(DataField):
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def value(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Value held in field
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
value = ''
|
|
|
|
for element in self.iterTags('value'):
|
|
|
|
value += '\n' + element.getData()
|
|
|
|
return value[1:]
|
|
|
|
|
|
|
|
@value.setter
|
|
|
|
def value(self, value):
|
|
|
|
del self.value
|
|
|
|
if value == '':
|
|
|
|
return
|
|
|
|
for line in value.split('\n'):
|
|
|
|
self.addChild('value').setData(line)
|
|
|
|
|
|
|
|
@value.deleter
|
|
|
|
def value(self):
|
|
|
|
for element in self.getTags('value'):
|
|
|
|
self.delChild(element)
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class DataRecord(ExtendedNode):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
The container for data fields - an xml element which has DataField elements
|
|
|
|
as children
|
|
|
|
"""
|
|
|
|
def __init__(self, fields=None, associated=None, extend=None):
|
|
|
|
self.associated = associated
|
|
|
|
self.vars = {}
|
|
|
|
if extend is None:
|
|
|
|
# we have to build this object from scratch
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.Node.__init__(self)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if fields is not None:
|
|
|
|
self.fields = fields
|
|
|
|
else:
|
2012-12-09 21:37:51 +01:00
|
|
|
# we already have nbxmpp.Node inside - try to convert all
|
2010-02-08 15:08:40 +01:00
|
|
|
# fields into DataField objects
|
|
|
|
if fields is None:
|
|
|
|
for field in self.iterTags('field'):
|
|
|
|
if not isinstance(field, DataField):
|
|
|
|
ExtendField(field)
|
|
|
|
self.vars[field.var] = field
|
|
|
|
else:
|
|
|
|
for field in self.getTags('field'):
|
|
|
|
self.delChild(field)
|
|
|
|
self.fields = fields
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def fields(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
List of fields in this record
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getTags('field')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@fields.setter
|
|
|
|
def fields(self, fields):
|
|
|
|
del self.fields
|
|
|
|
for field in fields:
|
|
|
|
if not isinstance(field, DataField):
|
|
|
|
ExtendField(field)
|
|
|
|
self.addChild(node=field)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@fields.deleter
|
|
|
|
def fields(self):
|
|
|
|
for element in self.getTags('field'):
|
|
|
|
self.delChild(element)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def iter_fields(self):
|
|
|
|
"""
|
|
|
|
Iterate over fields in this record. Do not take associated into account
|
|
|
|
"""
|
|
|
|
for field in self.iterTags('field'):
|
|
|
|
yield field
|
|
|
|
|
|
|
|
def iter_with_associated(self):
|
|
|
|
"""
|
|
|
|
Iterate over associated, yielding both our field and associated one
|
|
|
|
together
|
|
|
|
"""
|
|
|
|
for field in self.associated.iter_fields():
|
|
|
|
yield self[field.var], field
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
return self.vars[item]
|
2012-08-22 12:55:57 +02:00
|
|
|
|
2010-08-10 16:08:06 +02:00
|
|
|
def is_valid(self):
|
|
|
|
for f in self.iter_fields():
|
|
|
|
if not f.is_valid():
|
|
|
|
return False
|
|
|
|
return True
|
2006-07-05 18:35:22 +02:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class DataForm(ExtendedNode):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, type_=None, title=None, instructions=None, extend=None):
|
|
|
|
if extend is None:
|
|
|
|
# we have to build form from scratch
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.Node.__init__(self, 'x', attrs={'xmlns': nbxmpp.NS_DATA})
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if type_ is not None:
|
2018-07-07 19:36:24 +02:00
|
|
|
self.type_ = type_
|
2010-02-08 15:08:40 +01:00
|
|
|
if title is not None:
|
2018-07-07 19:36:24 +02:00
|
|
|
self.title = title
|
2010-02-08 15:08:40 +01:00
|
|
|
if instructions is not None:
|
2018-07-07 19:36:24 +02:00
|
|
|
self.instructions = instructions
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def type_(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Type of the form. Must be one of: 'form', 'submit', 'cancel', 'result'.
|
|
|
|
'form' - this form is to be filled in; you will be able soon to do:
|
|
|
|
filledform = DataForm(replyto=thisform)
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getAttr('type')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@type_.setter
|
|
|
|
def type_(self, type_):
|
|
|
|
assert type_ in ('form', 'submit', 'cancel', 'result')
|
|
|
|
self.setAttr('type', type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def title(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Title of the form
|
|
|
|
|
|
|
|
Human-readable, should not contain any \\r\\n.
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return self.getTagData('title')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@title.setter
|
|
|
|
def title(self, title):
|
|
|
|
self.setTagData('title', title)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@title.deleter
|
|
|
|
def title(self):
|
|
|
|
try:
|
|
|
|
self.delChild('title')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def instructions(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Instructions for this form
|
|
|
|
|
|
|
|
Human-readable, may contain \\r\\n.
|
|
|
|
"""
|
|
|
|
# TODO: the same code is in TextMultiField. join them
|
2017-08-27 21:41:27 +02:00
|
|
|
value = ''
|
|
|
|
for valuenode in self.getTags('instructions'):
|
|
|
|
value += '\n' + valuenode.getData()
|
|
|
|
return value[1:]
|
|
|
|
|
|
|
|
@instructions.setter
|
|
|
|
def instructions(self, value):
|
|
|
|
del self.instructions
|
2018-07-07 19:36:24 +02:00
|
|
|
if value == '':
|
|
|
|
return
|
2017-08-27 21:41:27 +02:00
|
|
|
for line in value.split('\n'):
|
|
|
|
self.addChild('instructions').setData(line)
|
|
|
|
|
|
|
|
@instructions.deleter
|
|
|
|
def instructions(self):
|
|
|
|
for value in self.getTags('instructions'):
|
|
|
|
self.delChild(value)
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class SimpleDataForm(DataForm, DataRecord):
|
2018-07-07 19:36:24 +02:00
|
|
|
def __init__(self, type_=None, title=None, instructions=None, fields=None,
|
|
|
|
extend=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
DataForm.__init__(self, type_=type_, title=title,
|
2018-07-07 19:36:24 +02:00
|
|
|
instructions=instructions, extend=extend)
|
2010-02-08 15:08:40 +01:00
|
|
|
DataRecord.__init__(self, fields=fields, extend=self, associated=self)
|
|
|
|
|
|
|
|
def get_purged(self):
|
|
|
|
c = SimpleDataForm(extend=self)
|
|
|
|
del c.title
|
|
|
|
c.instructions = ''
|
|
|
|
to_be_removed = []
|
|
|
|
for f in c.iter_fields():
|
|
|
|
if f.required:
|
2010-08-10 18:25:45 +02:00
|
|
|
# add <value> if there is not
|
2018-07-07 19:36:24 +02:00
|
|
|
if hasattr(f, 'value') and not f.value:
|
2010-08-10 18:25:45 +02:00
|
|
|
f.value = ''
|
2010-02-08 15:08:40 +01:00
|
|
|
# Keep all required fields
|
|
|
|
continue
|
2018-07-07 19:36:24 +02:00
|
|
|
if ((hasattr(f, 'value') and not f.value and f.value != 0) or
|
|
|
|
(hasattr(f, 'values') and len(f.values) == 0)):
|
2010-02-08 15:08:40 +01:00
|
|
|
to_be_removed.append(f)
|
|
|
|
else:
|
|
|
|
del f.label
|
|
|
|
del f.description
|
2010-06-27 23:09:07 +02:00
|
|
|
del f.media
|
2010-02-08 15:08:40 +01:00
|
|
|
for f in to_be_removed:
|
|
|
|
c.delChild(f)
|
|
|
|
return c
|
2006-11-18 19:39:02 +01:00
|
|
|
|
2018-07-07 19:36:24 +02:00
|
|
|
|
2006-11-18 19:39:02 +01:00
|
|
|
class MultipleDataForm(DataForm):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, type_=None, title=None, instructions=None, items=None,
|
2018-07-07 19:36:24 +02:00
|
|
|
extend=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
DataForm.__init__(self, type_=type_, title=title,
|
2018-07-07 19:36:24 +02:00
|
|
|
instructions=instructions, extend=extend)
|
2010-02-08 15:08:40 +01:00
|
|
|
# all records, recorded into DataRecords
|
|
|
|
if extend is None:
|
|
|
|
if items is not None:
|
|
|
|
self.items = items
|
|
|
|
else:
|
2012-12-09 21:37:51 +01:00
|
|
|
# we already have nbxmpp.Node inside - try to convert all
|
2010-02-08 15:08:40 +01:00
|
|
|
# fields into DataField objects
|
|
|
|
if items is None:
|
|
|
|
self.items = list(self.iterTags('item'))
|
|
|
|
else:
|
|
|
|
for item in self.getTags('item'):
|
|
|
|
self.delChild(item)
|
|
|
|
self.items = items
|
|
|
|
reported_tag = self.getTag('reported')
|
|
|
|
self.reported = DataRecord(extend=reported_tag)
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@property
|
|
|
|
def items(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
A list of all records
|
|
|
|
"""
|
2017-08-27 21:41:27 +02:00
|
|
|
return list(self.iter_records())
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@items.setter
|
|
|
|
def items(self, records):
|
|
|
|
del self.items
|
|
|
|
for record in records:
|
|
|
|
if not isinstance(record, DataRecord):
|
|
|
|
DataRecord(extend=record)
|
|
|
|
self.addChild(node=record)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
@items.deleter
|
|
|
|
def items(self):
|
|
|
|
for record in self.getTags('item'):
|
|
|
|
self.delChild(record)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def iter_records(self):
|
|
|
|
for record in self.getTags('item'):
|
|
|
|
yield record
|
|
|
|
|
2017-08-27 21:41:27 +02:00
|
|
|
# @property
|
|
|
|
# def reported(self):
|
|
|
|
# """
|
|
|
|
# DataRecord that contains descriptions of fields in records
|
|
|
|
# """
|
|
|
|
# return self.getTag('reported')
|
|
|
|
#
|
|
|
|
# @reported.setter
|
|
|
|
# def reported(self, record):
|
|
|
|
# try:
|
|
|
|
# self.delChild('reported')
|
|
|
|
# except:
|
|
|
|
# pass
|
2007-04-19 15:27:14 +02:00
|
|
|
#
|
2017-08-27 21:41:27 +02:00
|
|
|
# record.setName('reported')
|
|
|
|
# self.addChild(node=record)
|