comparison sat_tmp/wokkel/data_form.py @ 61:1245269f9aab

wokkel: new data_form module to add support of XML element + new install() function to make patching easier.
author Goffi <goffi@goffi.org>
date Wed, 16 Jan 2019 09:17:57 +0100
parents
children 0721b6254c9e
comparison
equal deleted inserted replaced
60:938c740d31fe 61:1245269f9aab
1 # -*- coding: utf-8 -*-
2 # -*- test-case-name: wokkel.test.test_rsm -*-
3 #
4 # Series of Wokkel patches used by SàT
5 # Copyright (C) 2019 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from wokkel import data_form
21 from twisted.words.xish import domish
22
23 NS_XML_ELEMENT = u"urn:xmpp:xml-element"
24
25
26 class Field(data_form.Field):
27 """Field class adding implementation for XML Element"""
28
29
30 def __init__(self, fieldType='text-single', var=None, value=None,
31 values=None, options=None, label=None, desc=None,
32 required=False, ext_type=None):
33 """
34 @param ext_type(None, unicode): extended type, can be:
35 - xml: field is an XML Element
36 - None: field is a base (XEP-0004) type
37 """
38
39 super(Field, self).__init__(fieldType, var, value, values, options, label, desc,
40 required)
41 self.ext_type = ext_type
42
43 def typeCheck(self):
44 if self.ext_type is not None:
45 # we use an extended type
46 if self.fieldType not in (None, 'hidden'):
47 raise ValueError('Invalid main type for a extended type field')
48 if len(self.values) != 1:
49 raise ValueError('extended type must have one and only one value')
50 if self.options:
51 raise ValueError('options must not be set for extented type')
52 if self.ext_type == u'xml':
53 if not isinstance(self.value, domish.Element):
54 raise ValueError('invalid value, a domish.Element is expected')
55 else:
56 raise ValueError(
57 'Unknown extended type: {ext_type}'.format(ext_type=self.ext_type))
58 return super(Field, self).typeCheck()
59
60 def toElement(self, asForm=False):
61 if self.ext_type is None:
62 return super(Field, self).toElement(asForm)
63 else:
64 self.typeCheck()
65 field_elt = domish.Element((data_form.NS_X_DATA, 'field'))
66 if self.fieldType:
67 field_elt['type'] = self.fieldType
68
69 if self.var is not None:
70 field_elt['var'] = self.var
71
72 wrapper_elt = field_elt.addElement((NS_XML_ELEMENT, u'wrapper'))
73 wrapper_elt.addChild(self.value)
74
75 if asForm:
76 if self.label is not None:
77 field_elt['label'] = self.label
78
79 if self.desc is not None:
80 field_elt.addElement('desc', content=self.desc)
81
82 if self.required:
83 field_elt.addElement('required')
84
85 return field_elt
86
87 @staticmethod
88 def fromElement(element):
89 field = super(Field, Field).fromElement(element)
90 if element.wrapper and element.wrapper.uri == NS_XML_ELEMENT:
91 field.ext_type = u"xml"
92 field.value = element.wrapper.firstChildElement()
93 return field
94
95
96 def install():
97 data_form.Field = Field