comparison src/plugins/plugin_exp_pubsub_schema.py @ 2386:2e05921df16a

plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
author Goffi <goffi@goffi.org>
date Fri, 20 Oct 2017 08:39:05 +0200
parents 72c30e73a9a5
children 4c883d1c3e81
comparison
equal deleted inserted replaced
2385:39d30cf722cb 2386:2e05921df16a
29 from wokkel import disco, iwokkel 29 from wokkel import disco, iwokkel
30 from wokkel import data_form 30 from wokkel import data_form
31 from wokkel import generic 31 from wokkel import generic
32 from zope.interface import implements 32 from zope.interface import implements
33 from collections import Iterable 33 from collections import Iterable
34 import copy
34 35
35 NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0' 36 NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0'
36 NS_SCHEMA_FORM = 'https://salut-a-toi/protocol/schema#schema:0' 37 NS_SCHEMA_FORM = 'https://salut-a-toi/protocol/schema#schema:0'
37 38
38 PLUGIN_INFO = { 39 PLUGIN_INFO = {
124 d = iq_elt.send() 125 d = iq_elt.send()
125 d.addCallback(self._getSchemaCb) 126 d.addCallback(self._getSchemaCb)
126 return d 127 return d
127 128
128 @defer.inlineCallbacks 129 @defer.inlineCallbacks
129 def getSchemaForm(self, client, service, nodeIdentifier, schema=None, form_type='form'): 130 def getSchemaForm(self, client, service, nodeIdentifier, schema=None, form_type='form', copy_form=True):
130 """get data form from node's schema 131 """get data form from node's schema
131 132
132 @param service(None, jid.JID): PubSub service 133 @param service(None, jid.JID): PubSub service
133 @param nodeIdentifier(unicode): node 134 @param nodeIdentifier(unicode): node
134 @param schema(domish.Element, data_form.Form, None): node schema 135 @param schema(domish.Element, data_form.Form, None): node schema
135 if domish.Element, will be converted to data form 136 if domish.Element, will be converted to data form
136 if data_form.Form it will be returned without modification 137 if data_form.Form it will be returned without modification
137 if None, it will be retrieved from node (imply one additional XMPP request) 138 if None, it will be retrieved from node (imply one additional XMPP request)
139 @param form_type(unicode): type of the form
140 @param copy_form(bool): if True and if schema is already a data_form.Form, will deep copy it before returning
141 needed when the form is reused and it will be modified (e.g. in sendDataFormItem)
138 @return(data_form.Form): data form 142 @return(data_form.Form): data form
143 the form should not be modified if copy_form is not set
139 """ 144 """
140 if schema is None: 145 if schema is None:
141 log.debug(_(u"unspecified schema, we need to request it")) 146 log.debug(_(u"unspecified schema, we need to request it"))
142 schema = yield self.getSchema(client, service, nodeIdentifier) 147 schema = yield self.getSchema(client, service, nodeIdentifier)
143 if schema is None: 148 if schema is None:
144 raise exceptions.DataError(_(u"no schema specified, and this node has no schema either, we can't construct the data form")) 149 raise exceptions.DataError(_(u"no schema specified, and this node has no schema either, we can't construct the data form"))
145 elif isinstance(schema, data_form.Form): 150 elif isinstance(schema, data_form.Form):
151 if copy_form:
152 schema = copy.deepcopy(schema)
146 defer.returnValue(schema) 153 defer.returnValue(schema)
147 154
148 try: 155 try:
149 form = data_form.Form.fromElement(schema) 156 form = data_form.Form.fromElement(schema)
150 except data_form.Error as e: 157 except data_form.Error as e:
217 @return (list[unicode]): XMLUI of the forms 224 @return (list[unicode]): XMLUI of the forms
218 if an item is invalid (not corresponding to form_ns or not a data_form) 225 if an item is invalid (not corresponding to form_ns or not a data_form)
219 it will be skipped 226 it will be skipped
220 """ 227 """
221 # we need the initial form to get options of fields when suitable 228 # we need the initial form to get options of fields when suitable
222 schema_form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='result') 229 schema_form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='result', copy_form=False)
223 items_data = yield self._p.getItems(client, service, nodeIdentifier, max_items, item_ids, sub_id, rsm_request, extra) 230 items_data = yield self._p.getItems(client, service, nodeIdentifier, max_items, item_ids, sub_id, rsm_request, extra)
224 items, metadata = items_data 231 items, metadata = items_data
225 items_xmlui = [] 232 items_xmlui = []
226 for item_elt in items: 233 for item_elt in items:
227 for x_elt in item_elt.elements((data_form.NS_X_DATA, u'x')): 234 for x_elt in item_elt.elements((data_form.NS_X_DATA, u'x')):