annotate sat/plugins/plugin_exp_pubsub_schema.py @ 2580:5e54afd17321

quick frontend (chat): added a property to Message which is True when message comes from history
author Goffi <goffi@goffi.org>
date Thu, 10 May 2018 08:34:44 +0200
parents 26edcf3a30eb
children 5d4ac5415b40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Pubsub Schemas
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2471
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import exceptions
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.constants import Const as C
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
23 from sat.tools import xml_tools
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
24 from sat.tools import utils
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import jid
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
27 from twisted.internet import defer
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.log import getLogger
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 log = getLogger(__name__)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel import disco, iwokkel
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from wokkel import data_form
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from wokkel import generic
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from zope.interface import implements
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
34 from collections import Iterable
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
35 import copy
2421
b7e24ce97a06 plugin schema: separate lines of text-multi in different values as requested by XEP-0004
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
36 import itertools
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0'
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_NAME: "PubSub Schema",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_IMPORT_NAME: "PUBSUB_SCHEMA",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_TYPE: "EXP",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_PROTOCOLS: [],
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
45 C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"],
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_MAIN: "PubsubSchema",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_HANDLER: "yes",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_DESCRIPTION: _("""Handle Pubsub data schemas""")
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 }
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 class PubsubSchema(object):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 log.info(_(u"PubSub Schema initialization"))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.host = host
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
57 self._p = self.host.plugins["XEP-0060"]
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
58 self._i = self.host.plugins["IDENTITY"]
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 host.bridge.addMethod("psSchemaGet", ".plugin",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 in_sign='sss', out_sign='s',
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 method=self._getSchema,
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 async=True
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 )
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 host.bridge.addMethod("psSchemaSet", ".plugin",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 in_sign='ssss', out_sign='',
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 method=self._setSchema,
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 async=True
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
69 host.bridge.addMethod("psSchemaUIGet", ".plugin",
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
70 in_sign='sss', out_sign='s',
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
71 method=utils.partial(self._getUISchema, default_node=None),
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
72 async=True
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
73 )
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
74 host.bridge.addMethod("psItemsFormGet", ".plugin",
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
75 in_sign='ssssiassa{ss}s', out_sign='(asa{ss})',
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
76 method=self._getDataFormItems,
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
77 async=True)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
78 host.bridge.addMethod("psItemFormSend", ".plugin",
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
79 in_sign='ssa{sas}ssa{ss}s', out_sign='s',
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
80 method=self._sendDataFormItem,
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
81 async=True)
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def getHandler(self, client):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return SchemaHandler()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def _getSchemaBridgeCb(self, schema_elt):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if schema_elt is None:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 return u''
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 return schema_elt.toXml()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def _getSchema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 client = self.host.getClient(profile_key)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 service = None if not service else jid.JID(service)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 d = self.getSchema(client, service, nodeIdentifier)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 d.addCallback(self._getSchemaBridgeCb)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return d
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def _getSchemaCb(self, iq_elt):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99 try:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100 schema_elt = next(iq_elt.elements(NS_SCHEMA, 'schema'))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 except StopIteration:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 raise exceptions.DataError('missing <schema> element')
2395
713cedc99752 plugin pubsub schema: allow any data_form schema:
Goffi <goffi@goffi.org>
parents: 2388
diff changeset
103 try:
713cedc99752 plugin pubsub schema: allow any data_form schema:
Goffi <goffi@goffi.org>
parents: 2388
diff changeset
104 x_elt = next(schema_elt.elements((data_form.NS_X_DATA, 'x')))
713cedc99752 plugin pubsub schema: allow any data_form schema:
Goffi <goffi@goffi.org>
parents: 2388
diff changeset
105 except StopIteration:
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 # there is not schema on this node
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return None
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return x_elt
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def getSchema(self, client, service, nodeIdentifier):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 """retrieve PubSub node schema
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 @param service(jid.JID, None): jid of PubSub service
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 None to use our PEP
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 @param nodeIdentifier(unicode): node to get schema from
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 @return (domish.Element, None): schema (<x> element)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 None if not schema has been set on this node
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 """
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 iq_elt = client.IQ(u'get')
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if service is not None:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 iq_elt['to'] = service.full()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub'))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema'))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 schema_elt['node'] = nodeIdentifier
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 d = iq_elt.send()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 d.addCallback(self._getSchemaCb)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 return d
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
129 @defer.inlineCallbacks
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
130 def getSchemaForm(self, client, service, nodeIdentifier, schema=None, form_type='form', copy_form=True):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
131 """get data form from node's schema
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
132
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
133 @param service(None, jid.JID): PubSub service
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
134 @param nodeIdentifier(unicode): node
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
135 @param schema(domish.Element, data_form.Form, None): node schema
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
136 if domish.Element, will be converted to data form
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
137 if data_form.Form it will be returned without modification
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
138 if None, it will be retrieved from node (imply one additional XMPP request)
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
139 @param form_type(unicode): type of the form
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
140 @param copy_form(bool): if True and if schema is already a data_form.Form, will deep copy it before returning
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
141 needed when the form is reused and it will be modified (e.g. in sendDataFormItem)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
142 @return(data_form.Form): data form
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
143 the form should not be modified if copy_form is not set
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
144 """
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
145 if schema is None:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
146 log.debug(_(u"unspecified schema, we need to request it"))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
147 schema = yield self.getSchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
148 if schema is None:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
149 raise exceptions.DataError(_(u"no schema specified, and this node has no schema either, we can't construct the data form"))
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
150 elif isinstance(schema, data_form.Form):
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
151 if copy_form:
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
152 schema = copy.deepcopy(schema)
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
153 defer.returnValue(schema)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
154
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
155 try:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
156 form = data_form.Form.fromElement(schema)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
157 except data_form.Error as e:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
158 raise exceptions.DataError(_(u"Invalid Schema: {msg}").format(
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
159 msg = e))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
160 form.formType = form_type
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
161 defer.returnValue(form)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
162
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
163 def schema2XMLUI(self, schema_elt):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
164 form = data_form.Form.fromElement(schema_elt)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
165 xmlui = xml_tools.dataForm2XMLUI(form, '')
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
166 return xmlui
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
167
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
168 def _getUISchema(self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
169 if not nodeIdentifier:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
170 if not default_node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
171 raise ValueError(_(u"nodeIndentifier needs to be set"))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
172 nodeIdentifier = default_node
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
173 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
174 service = None if not service else jid.JID(service)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
175 d = self.getUISchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
176 d.addCallback(lambda xmlui: xmlui.toXml())
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
177 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
178
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
179 def getUISchema(self, client, service, nodeIdentifier):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
180 d = self.getSchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
181 d.addCallback(self.schema2XMLUI)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
182 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
183
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184 def _setSchema(self, service, nodeIdentifier, schema, profile_key=C.PROF_KEY_NONE):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 client = self.host.getClient(profile_key)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 service = None if not service else jid.JID(service)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187 schema = generic.parseXml(schema.encode('utf-8'))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
188 return self.setSchema(client, service, nodeIdentifier, schema)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
189
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190 def setSchema(self, client, service, nodeIdentifier, schema):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
191 """set or replace PubSub node schema
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
192
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
193 @param schema(domish.Element, None): schema to set
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
194 None if schema need to be removed
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
195 """
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
196 iq_elt = client.IQ()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
197 if service is not None:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
198 iq_elt['to'] = service.full()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199 pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub'))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
200 schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema'))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
201 schema_elt['node'] = nodeIdentifier
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 if schema is not None:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203 schema_elt.addChild(schema)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 return iq_elt.send()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
206 def _getDataFormItems(self, form_ns='', service='', node='', schema='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, profile_key=C.PROF_KEY_NONE):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
207 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
208 service = jid.JID(service) if service else None
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2395
diff changeset
209 if not node:
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2395
diff changeset
210 raise exceptions.DataError(_(u'empty node is not allowed'))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
211 if schema:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
212 schema = generic.parseXml(schema.encode('utf-8'))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
213 else:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
214 schema = None
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
215 max_items = None if max_items == C.NO_LIMIT else max_items
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
216 extra = self._p.parseExtra(extra_dict)
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
217 d = self.getDataFormItems(client, service, node, schema, max_items or None, item_ids, sub_id or None, extra.rsm_request, extra.extra, form_ns=form_ns or None)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
218 d.addCallback(self._p.serItemsData)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
219 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
220
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
221 @defer.inlineCallbacks
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
222 def getDataFormItems(self, client, service, nodeIdentifier, schema=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None, default_node=None, form_ns=None, filters=None):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
223 """Get items known as being data forms, and convert them to XMLUI
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
224
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
225 @param schema(domish.Element, data_form.Form, None): schema of the node if known
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
226 if None, it will be retrieved from node
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
227 @param default_node(unicode): node to use if nodeIdentifier is None or empty
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
228 @param form_ns (unicode, None): namespace of the form
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
229 None to accept everything, even if form has no namespace
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2395
diff changeset
230 @param filters(dict, None): same as for xml_tools.dataFormResult2XMLUI
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
231 other parameters as the same as for [getItems]
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
232 @return (list[unicode]): XMLUI of the forms
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
233 if an item is invalid (not corresponding to form_ns or not a data_form)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
234 it will be skipped
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
235 @raise ValueError: one argument is invalid
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
236 """
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
237 if not nodeIdentifier:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
238 if not default_node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
239 raise ValueError(_(u"default_node must be set if nodeIdentifier is not set"))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
240 nodeIdentifier = default_node
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
241 # we need the initial form to get options of fields when suitable
2386
2e05921df16a plugin schema, core(xmlui): deep copy base form when reused to avoid accidental modification
Goffi <goffi@goffi.org>
parents: 2381
diff changeset
242 schema_form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='result', copy_form=False)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
243 items_data = yield self._p.getItems(client, service, nodeIdentifier, max_items, item_ids, sub_id, rsm_request, extra)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
244 items, metadata = items_data
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
245 items_xmlui = []
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
246 for item_elt in items:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
247 for x_elt in item_elt.elements((data_form.NS_X_DATA, u'x')):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
248 form = data_form.Form.fromElement(x_elt)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
249 if form_ns and form.formNamespace != form_ns:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
250 continue
2381
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
251 xmlui = xml_tools.dataFormResult2XMLUI(
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
252 form,
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
253 schema_form,
2426
6c39f30444a0 plugin schema: prepend a "publisher" field after "id" in getDataFormItems with published given by PubSub service
Goffi <goffi@goffi.org>
parents: 2421
diff changeset
254 # FIXME: conflicts with schema (i.e. if "id" or "publisher" already exists)
6c39f30444a0 plugin schema: prepend a "publisher" field after "id" in getDataFormItems with published given by PubSub service
Goffi <goffi@goffi.org>
parents: 2421
diff changeset
255 # are not checked
6c39f30444a0 plugin schema: prepend a "publisher" field after "id" in getDataFormItems with published given by PubSub service
Goffi <goffi@goffi.org>
parents: 2421
diff changeset
256 prepend = (('label', 'id'),('text', item_elt['id'], u'id'),
6c39f30444a0 plugin schema: prepend a "publisher" field after "id" in getDataFormItems with published given by PubSub service
Goffi <goffi@goffi.org>
parents: 2421
diff changeset
257 ('label', 'publisher'),('text', item_elt.getAttribute('publisher',''), u'publisher')),
2398
3ff9d7a7fe71 core (XMLUI): filters can now be used when converting data form to XMLUI:
Goffi <goffi@goffi.org>
parents: 2395
diff changeset
258 filters = filters,
2381
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
259 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
260 items_xmlui.append(xmlui)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
261 break
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
262 defer.returnValue((items_xmlui, metadata))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
263
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
264
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
265 def _sendDataFormItem(self, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
266 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
267 service = None if not service else jid.JID(service)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
268 if schema:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
269 schema = generic.parseXml(schema.encode('utf-8'))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
270 else:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
271 schema = None
2378
3704cb959ae8 plugin schema: deserialise is now the last kw argument
Goffi <goffi@goffi.org>
parents: 2371
diff changeset
272 d = self.sendDataFormItem(client, service, nodeIdentifier, values, schema, item_id or None, extra, deserialise=True)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
273 d.addCallback(lambda ret: ret or u'')
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
274 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
275
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
276 @defer.inlineCallbacks
2378
3704cb959ae8 plugin schema: deserialise is now the last kw argument
Goffi <goffi@goffi.org>
parents: 2371
diff changeset
277 def sendDataFormItem(self, client, service, nodeIdentifier, values, schema=None, item_id=None, extra=None, deserialise=False):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
278 """Publish an item as a dataform when we know that there is a schema
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
279
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2398
diff changeset
280 @param values(dict[key(unicode), [iterable[object], object]]): values set for the form
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
281 if not iterable, will be put in a list
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
282 @param schema(domish.Element, data_form.Form, None): data schema
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
283 None to retrieve data schema from node (need to do a additional XMPP call)
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
284 Schema is needed to construct data form to publish
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
285 @param deserialise(bool): if True, data are list of unicode and must be deserialized according to expected type
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
286 This is done in this method and not directly in _sendDataFormItem because we need to know the data type
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
287 which is in the form, not availablable in _sendDataFormItem
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
288 other parameters as the same as for [self._p.sendItem]
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2398
diff changeset
289 @return (unicode): id of the created item
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
290 """
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
291 form = yield self.getSchemaForm(client, service, nodeIdentifier, schema, form_type='submit')
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
292
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
293 for name, values_list in values.iteritems():
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
294 try:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
295 field = form.fields[name]
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
296 except KeyError:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
297 log.warning(_(u"field {name} doesn't exist, ignoring it").format(name=name))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
298 continue
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
299 if isinstance(values_list, basestring) or not isinstance(values_list, Iterable):
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
300 values_list = [values_list]
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
301 if deserialise:
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
302 if field.fieldType == 'boolean':
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
303 values_list = [C.bool(v) for v in values_list]
2421
b7e24ce97a06 plugin schema: separate lines of text-multi in different values as requested by XEP-0004
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
304 elif field.fieldType == 'text-multi':
b7e24ce97a06 plugin schema: separate lines of text-multi in different values as requested by XEP-0004
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
305 # for text-multi, lines must be put on separate values
b7e24ce97a06 plugin schema: separate lines of text-multi in different values as requested by XEP-0004
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
306 values_list = list(itertools.chain(*[v.splitlines() for v in values_list]))
b7e24ce97a06 plugin schema: separate lines of text-multi in different values as requested by XEP-0004
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
307
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
308 elif 'jid' in field.fieldType:
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
309 values_list = [jid.JID(v) for v in values_list]
2388
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
310 if 'list' in field.fieldType:
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
311 # for lists, we check that given values are allowed in form
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
312 allowed_values = [o.value for o in field.options]
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
313 values_list = [v for v in values_list if v in allowed_values]
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
314 if not values_list:
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
315 # if values don't map to allowed values, we use default ones
4c883d1c3e81 plugin schema: when publishing a schema, check that values of list are allowed, else use default value
Goffi <goffi@goffi.org>
parents: 2386
diff changeset
316 values_list = field.values
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
317 field.values = values_list
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
318
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
319 yield self._p.sendItem(client, service, nodeIdentifier, form.toElement(), item_id, extra)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
320
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
321 ## filters ##
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
322 # filters useful for data form to XMLUI conversion #
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
323
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
324 def valueOrPublisherFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
325 """Replace missing value by publisher's user part"""
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
326 if not args[0]:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
327 # value is not filled: we use user part of publisher (if we have it)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
328 try:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
329 publisher = jid.JID(form_xmlui.named_widgets['publisher'].value)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
330 except (KeyError, RuntimeError):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
331 pass
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
332 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
333 args[0] = publisher.user.capitalize()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
334 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
335
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
336 def textbox2ListFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
337 """Split lines of a textbox in a list
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
338
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
339 main use case is using a textbox for labels
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
340 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
341 if widget_type != u'textbox':
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
342 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
343 widget_type = u'list'
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
344 options = [o for o in args.pop(0).split(u'\n') if o]
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
345 kwargs = {'options': options,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
346 'name': kwargs.get('name'),
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
347 'styles': (u'noselect', u'extensible', u'reducible')}
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
348 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
349
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
350 def dateFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
351 """Convert a string with a date to a unix timestamp"""
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
352 if widget_type != u'string' or not args[0]:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
353 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
354 # we convert XMPP date to timestamp
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
355 try:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
356 args[0] = unicode(utils.date_parse(args[0]))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
357 except Exception as e:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
358 log.warning(_(u"Can't parse date field: {msg}").format(msg=e))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
359 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
360
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
361 ## Helper methods ##
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
362
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
363 def prepareBridgeGet(self, service, node, max_items, sub_id, extra_dict, profile_key):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
364 """Parse arguments received from bridge *Get methods and return higher level data
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
365
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
366 @return (tuple): (client, service, node, max_items, extra, sub_id) usable for internal methods
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
367 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
368 client = self.host.getClient(profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
369 service = jid.JID(service) if service else None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
370 if not node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
371 node = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
372 max_items = None if max_items == C.NO_LIMIT else max_items
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
373 if not sub_id:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
374 sub_id = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
375 extra = self._p.parseExtra(extra_dict)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
376
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
377 return client, service, node, max_items, extra, sub_id
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
378
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
379 def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, default_node=None, form_ns=None, filters=None, profile_key=C.PROF_KEY_NONE):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
380 """Bridge method to retrieve data from node with schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
381
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
382 this method is a helper so dependant plugins can use it directly
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
383 when adding *Get methods
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
384 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
385 client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(service, node, max_items, sub_id, extra_dict, profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
386 d = self.getDataFormItems(client, service, node or None,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
387 max_items=max_items,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
388 item_ids=item_ids,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
389 sub_id=sub_id,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
390 rsm_request=extra.rsm_request,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
391 extra=extra.extra,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
392 default_node=default_node,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
393 form_ns=form_ns,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
394 filters=filters)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
395 d.addCallback(self._p.serItemsData)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
396 return d
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
397
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
398 def prepareBridgeSet(self, service, node, schema, item_id, extra, profile_key):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
399 """Parse arguments received from bridge *Set methods and return higher level data
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
400
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
401 @return (tuple): (client, service, node, schema, item_id, extra) usable for internal methods
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
402 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
403 client = self.host.getClient(profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
404 service = None if not service else jid.JID(service)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
405 if schema:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
406 schema = generic.parseXml(schema.encode('utf-8'))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
407 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
408 schema = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
409 if extra and u'update' in extra:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
410 extra[u'update'] = C.bool(extra[u'update'])
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
411 return client, service, node or None, schema, item_id or None, extra
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
412
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
413 def _set(self, service, node, values, schema=None, item_id=None, extra=None, default_node=None, form_ns=None, fill_author=True, profile_key=C.PROF_KEY_NONE):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
414 """Bridge method to set item in node with schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
415
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
416 this method is a helper so dependant plugins can use it directly
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
417 when adding *Set methods
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
418 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
419 client, service, node, schema, item_id, extra = self.prepareBridgeSet(service, node, schema, item_id, extra)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
420 d = self.set(client, service, node, values, schema, item_id, extra,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
421 deserialise=True,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
422 form_ns=form_ns,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
423 default_node=default_node,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
424 fill_author=fill_author)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
425 d.addCallback(lambda ret: ret or u'')
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
426 return d
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
427
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
428 @defer.inlineCallbacks
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
429 def set(self, client, service, node, values, schema, item_id, extra, deserialise, form_ns, default_node=None, fill_author=True):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
430 """Set an item in a node with a schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
431
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
432 This method can be used directly by *Set methods added by dependant plugin
2554
0062d3e79d12 plugin uri finder, jp (merge-request): labels handling:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
433 @param values(dict[key(unicode), [iterable[object]|object]]): values of the items
0062d3e79d12 plugin uri finder, jp (merge-request): labels handling:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
434 if value is not iterable, it will be put in a list
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
435 'created' and 'updated' will be forced to current time:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
436 - 'created' is set if item_id is None, i.e. if it's a new ticket
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
437 - 'updated' is set everytime
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
438 @param extra(dict, None): same as for [XEP-0060.sendItem] with additional keys:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
439 - update(bool): if True, get previous item data to merge with current one
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
440 if True, item_id must be None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
441 @param form_ns (unicode, None): namespace of the form
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
442 needed when an update is done
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
443 @param default_node(unicode, None): value to use if node is not set
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
444 other arguments are same as for [self._s.sendDataFormItem]
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
445 @return (unicode): id of the created item
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
446 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
447 if not node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
448 if default_node is None:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
449 raise ValueError(_(u"default_node must be set if node is not set"))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
450 node = default_node
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
451 now = utils.xmpp_date()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
452 if not item_id:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
453 values['created'] = now
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
454 elif extra.get(u'update', False):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
455 if item_id is None:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
456 raise exceptions.DataError(_(u'if extra["update"] is set, item_id must be set too'))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
457 try:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
458 # we get previous item
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
459 items_data = yield self._p.getItems(client, service, node, item_ids=[item_id])
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
460 item_elt = items_data[0][0]
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
461 except Exception as e:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
462 log.warning(_(u"Can't get previous item, update ignored: {reason}").format(
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
463 reason = e))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
464 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
465 # and parse it
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
466 form = data_form.findForm(item_elt, form_ns)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
467 if form is None:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
468 log.warning(_(u"Can't parse previous item, update ignored: data form not found").format(
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
469 reason = e))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
470 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
471 for name, field in form.fields.iteritems():
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
472 if name not in values:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
473 values[name] = u'\n'.join(unicode(v) for v in field.values)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
474
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
475 values['updated'] = now
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
476 if fill_author:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
477 if not values.get('author'):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
478 identity = yield self._i.getIdentity(client, client.jid)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
479 values['author'] = identity['nick']
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
480 if not values.get('author_jid'):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
481 values['author_jid'] = client.jid.full()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
482 item_id = yield self.sendDataFormItem(client, service, node, values, schema, item_id, extra, deserialise)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
483 defer.returnValue(item_id)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
484
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
485
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
486 class SchemaHandler(XMPPHandler):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
487 implements(iwokkel.IDisco)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
488
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
489 def getDiscoInfo(self, requestor, service, nodeIdentifier=''):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
490 return [disco.DiscoFeature(NS_SCHEMA)]
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
491
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
492 def getDiscoItems(self, requestor, service, nodeIdentifier=''):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
493 return []