annotate sat/plugins/plugin_exp_pubsub_schema.py @ 2672:0bed6df9ee5d

jp: fixed bad call to quit
author Goffi <goffi@goffi.org>
date Fri, 14 Sep 2018 16:44:34 +0200
parents 56f94936df1e
children 3480d4fdf83a
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
2612
3e4e78de9cca tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents: 2603
diff changeset
25 from sat.tools.common import date_utils
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import jid
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
28 from twisted.internet import defer
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
30
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 log = getLogger(__name__)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from wokkel import disco, iwokkel
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from wokkel import data_form
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from wokkel import generic
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from zope.interface import implements
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
36 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
37 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
38 import itertools
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
40 NS_SCHEMA = "https://salut-a-toi/protocol/schema:0"
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 PLUGIN_INFO = {
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_NAME: "PubSub Schema",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_IMPORT_NAME: "PUBSUB_SCHEMA",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_TYPE: "EXP",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_PROTOCOLS: [],
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
47 C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"],
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MAIN: "PubsubSchema",
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
50 C.PI_DESCRIPTION: _("""Handle Pubsub data schemas"""),
2350
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
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 class PubsubSchema(object):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def __init__(self, host):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 log.info(_(u"PubSub Schema initialization"))
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.host = host
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
58 self._p = self.host.plugins["XEP-0060"]
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
59 self._i = self.host.plugins["IDENTITY"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
60 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
61 "psSchemaGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
62 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
63 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
64 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
65 method=self._getSchema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
66 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
67 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
68 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
69 "psSchemaSet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
70 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
71 in_sign="ssss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
72 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
73 method=self._setSchema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
74 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
75 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
76 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
77 "psSchemaUIGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
78 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
79 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
80 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
81 method=utils.partial(self._getUISchema, default_node=None),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
82 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
83 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
84 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
85 "psItemsFormGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
86 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
87 in_sign="ssssiassa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
88 out_sign="(asa{ss})",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
89 method=self._getDataFormItems,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
90 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
91 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
92 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
93 "psItemFormSend",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
94 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
95 in_sign="ssa{sas}ssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
96 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
97 method=self._sendDataFormItem,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
98 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
99 )
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def getHandler(self, client):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return SchemaHandler()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def _getSchemaBridgeCb(self, schema_elt):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if schema_elt is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
106 return u""
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return schema_elt.toXml()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 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
110 client = self.host.getClient(profile_key)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 service = None if not service else jid.JID(service)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 d = self.getSchema(client, service, nodeIdentifier)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 d.addCallback(self._getSchemaBridgeCb)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114 return d
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 def _getSchemaCb(self, iq_elt):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
118 schema_elt = next(iq_elt.elements(NS_SCHEMA, "schema"))
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
119 except StopIteration:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
120 raise exceptions.DataError("missing <schema> element")
2395
713cedc99752 plugin pubsub schema: allow any data_form schema:
Goffi <goffi@goffi.org>
parents: 2388
diff changeset
121 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
122 x_elt = next(schema_elt.elements((data_form.NS_X_DATA, "x")))
2395
713cedc99752 plugin pubsub schema: allow any data_form schema:
Goffi <goffi@goffi.org>
parents: 2388
diff changeset
123 except StopIteration:
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 # there is not schema on this node
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return None
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return x_elt
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def getSchema(self, client, service, nodeIdentifier):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """retrieve PubSub node schema
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 @param service(jid.JID, None): jid of PubSub service
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 None to use our PEP
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @param nodeIdentifier(unicode): node to get schema from
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134 @return (domish.Element, None): schema (<x> element)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 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
136 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
137 iq_elt = client.IQ(u"get")
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if service is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
139 iq_elt["to"] = service.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
140 pubsub_elt = iq_elt.addElement((NS_SCHEMA, "pubsub"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
141 schema_elt = pubsub_elt.addElement((NS_SCHEMA, "schema"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
142 schema_elt["node"] = nodeIdentifier
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143 d = iq_elt.send()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144 d.addCallback(self._getSchemaCb)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
145 return d
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
147 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
148 def getSchemaForm(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
149 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
150 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
151 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
152 nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
153 schema=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
154 form_type="form",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
155 copy_form=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
156 ):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
157 """get data form from node's schema
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
158
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
159 @param service(None, jid.JID): PubSub service
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
160 @param nodeIdentifier(unicode): node
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
161 @param schema(domish.Element, data_form.Form, None): node schema
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
162 if domish.Element, will be converted to data form
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
163 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
164 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
165 @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
166 @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
167 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
168 @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
169 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
170 """
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
171 if schema is None:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
172 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
173 schema = yield self.getSchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
174 if schema is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
175 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
176 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
177 u"no schema specified, and this node has no schema either, we can't construct the data form"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
178 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
179 )
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
180 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
181 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
182 schema = copy.deepcopy(schema)
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
183 defer.returnValue(schema)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
184
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
185 try:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
186 form = data_form.Form.fromElement(schema)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
187 except data_form.Error as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
188 raise exceptions.DataError(_(u"Invalid Schema: {msg}").format(msg=e))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
189 form.formType = form_type
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
190 defer.returnValue(form)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
191
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
192 def schema2XMLUI(self, schema_elt):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
193 form = data_form.Form.fromElement(schema_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
194 xmlui = xml_tools.dataForm2XMLUI(form, "")
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
195 return xmlui
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
196
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
197 def _getUISchema(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
198 self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
199 ):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
200 if not nodeIdentifier:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
201 if not default_node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
202 raise ValueError(_(u"nodeIndentifier needs to be set"))
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
203 nodeIdentifier = default_node
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
204 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
205 service = None if not service else jid.JID(service)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
206 d = self.getUISchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
207 d.addCallback(lambda xmlui: xmlui.toXml())
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
208 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
209
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
210 def getUISchema(self, client, service, nodeIdentifier):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
211 d = self.getSchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
212 d.addCallback(self.schema2XMLUI)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
213 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
214
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 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
216 client = self.host.getClient(profile_key)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 service = None if not service else jid.JID(service)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
218 schema = generic.parseXml(schema.encode("utf-8"))
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219 return self.setSchema(client, service, nodeIdentifier, schema)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
220
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
221 def setSchema(self, client, service, nodeIdentifier, schema):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
222 """set or replace PubSub node schema
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
223
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 @param schema(domish.Element, None): schema to set
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
225 None if schema need to be removed
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
226 """
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 iq_elt = client.IQ()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
228 if service is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
229 iq_elt["to"] = service.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
230 pubsub_elt = iq_elt.addElement((NS_SCHEMA, "pubsub"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
231 schema_elt = pubsub_elt.addElement((NS_SCHEMA, "schema"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
232 schema_elt["node"] = nodeIdentifier
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233 if schema is not None:
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
234 schema_elt.addChild(schema)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235 return iq_elt.send()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
237 def _getDataFormItems(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
238 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
239 form_ns="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
240 service="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
241 node="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
242 schema="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
243 max_items=10,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
244 item_ids=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
245 sub_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
246 extra_dict=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
247 profile_key=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
248 ):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
249 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
250 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
251 if not node:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
252 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
253 if schema:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
254 schema = generic.parseXml(schema.encode("utf-8"))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
255 else:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
256 schema = None
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
257 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
258 extra = self._p.parseExtra(extra_dict)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
259 d = self.getDataFormItems(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
260 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
261 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
262 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
263 schema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
264 max_items or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
265 item_ids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
266 sub_id or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
267 extra.rsm_request,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
268 extra.extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
269 form_ns=form_ns or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
270 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
271 d.addCallback(self._p.serItemsData)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
272 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
273
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
274 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
275 def getDataFormItems(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
276 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
277 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
278 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
279 nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
280 schema=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
281 max_items=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
282 item_ids=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
283 sub_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
284 rsm_request=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
285 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
286 default_node=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
287 form_ns=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
288 filters=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
289 ):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
290 """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
291
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
292 @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
293 if None, it will be retrieved from node
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
294 @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
295 @param form_ns (unicode, None): namespace of the form
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
296 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
297 @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
298 other parameters as the same as for [getItems]
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
299 @return (list[unicode]): XMLUI of the forms
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
300 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
301 it will be skipped
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
302 @raise ValueError: one argument is invalid
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
303 """
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
304 if not nodeIdentifier:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
305 if not default_node:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
306 raise ValueError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
307 _(u"default_node must be set if nodeIdentifier is not set")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
308 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
309 nodeIdentifier = default_node
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
310 # we need the initial form to get options of fields when suitable
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
311 schema_form = yield self.getSchemaForm(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
312 client, service, nodeIdentifier, schema, form_type="result", copy_form=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
313 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
314 items_data = yield self._p.getItems(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
315 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
316 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
317 nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
318 max_items,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
319 item_ids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
320 sub_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
321 rsm_request,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
322 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
323 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
324 items, metadata = items_data
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
325 items_xmlui = []
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
326 for item_elt in items:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
327 for x_elt in item_elt.elements((data_form.NS_X_DATA, u"x")):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
328 form = data_form.Form.fromElement(x_elt)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
329 if form_ns and form.formNamespace != form_ns:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
330 continue
2381
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
331 xmlui = xml_tools.dataFormResult2XMLUI(
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
332 form,
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
333 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
334 # 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
335 # are not checked
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
336 prepend=(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
337 ("label", "id"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
338 ("text", item_elt["id"], u"id"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
339 ("label", "publisher"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
340 ("text", item_elt.getAttribute("publisher", ""), u"publisher"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
341 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
342 filters=filters,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
343 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
344 items_xmlui.append(xmlui)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
345 break
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
346 defer.returnValue((items_xmlui, metadata))
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
347
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
348 def _sendDataFormItem(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
349 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
350 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
351 nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
352 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
353 schema=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
354 item_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
355 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
356 profile_key=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
357 ):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
358 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
359 service = None if not service else jid.JID(service)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
360 if schema:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
361 schema = generic.parseXml(schema.encode("utf-8"))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
362 else:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
363 schema = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
364 d = self.sendDataFormItem(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
365 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
366 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
367 nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
368 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
369 schema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
370 item_id or None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
371 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
372 deserialise=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
373 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
374 d.addCallback(lambda ret: ret or u"")
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
375 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
376
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
377 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
378 def sendDataFormItem(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
379 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
380 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
381 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
382 nodeIdentifier,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
383 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
384 schema=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
385 item_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
386 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
387 deserialise=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
388 ):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
389 """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
390
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2398
diff changeset
391 @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
392 if not iterable, will be put in a list
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
393 @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
394 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
395 Schema is needed to construct data form to publish
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
396 @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
397 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
398 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
399 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
400 @return (unicode): id of the created item
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
401 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
402 form = yield self.getSchemaForm(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
403 client, service, nodeIdentifier, schema, form_type="submit"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
404 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
405
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
406 for name, values_list in values.iteritems():
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
407 try:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
408 field = form.fields[name]
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
409 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
410 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
411 _(u"field {name} doesn't exist, ignoring it").format(name=name)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
412 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
413 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
414 if isinstance(values_list, basestring) or not isinstance(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
415 values_list, Iterable
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
416 ):
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
417 values_list = [values_list]
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
418 if deserialise:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
419 if field.fieldType == "boolean":
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
420 values_list = [C.bool(v) for v in values_list]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
421 elif field.fieldType == "text-multi":
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
422 # for text-multi, lines must be put on separate values
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
423 values_list = list(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
424 itertools.chain(*[v.splitlines() for v in values_list])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
425 )
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
426
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
427 elif "jid" in field.fieldType:
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
428 values_list = [jid.JID(v) for v in values_list]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
429 if "list" in field.fieldType:
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
430 # 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
431 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
432 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
433 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
434 # 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
435 values_list = field.values
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
436 field.values = values_list
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
437
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
438 yield self._p.sendItem(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
439 client, service, nodeIdentifier, form.toElement(), item_id, extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
440 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
441
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
442 ## filters ##
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
443 # filters useful for data form to XMLUI conversion #
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
444
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
445 def valueOrPublisherFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
446 """Replace missing value by publisher's user part"""
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
447 if not args[0]:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
448 # 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
449 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
450 publisher = jid.JID(form_xmlui.named_widgets["publisher"].value)
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
451 except (KeyError, RuntimeError):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
452 pass
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
453 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
454 args[0] = publisher.user.capitalize()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
455 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
456
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
457 def textbox2ListFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
458 """Split lines of a textbox in a list
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
459
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
460 main use case is using a textbox for labels
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
461 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
462 if widget_type != u"textbox":
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
463 return widget_type, args, kwargs
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
464 widget_type = u"list"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
465 options = [o for o in args.pop(0).split(u"\n") if o]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
466 kwargs = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
467 "options": options,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
468 "name": kwargs.get("name"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
469 "styles": (u"noselect", u"extensible", u"reducible"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
470 }
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
471 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
472
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
473 def dateFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
474 """Convert a string with a date to a unix timestamp"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
475 if widget_type != u"string" or not args[0]:
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
476 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
477 # we convert XMPP date to timestamp
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
478 try:
2612
3e4e78de9cca tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
Goffi <goffi@goffi.org>
parents: 2603
diff changeset
479 args[0] = unicode(date_utils.date_parse(args[0]))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
480 except Exception as e:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
481 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
482 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
483
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
484 ## Helper methods ##
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
485
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
486 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
487 """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
488
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
489 @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
490 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
491 client = self.host.getClient(profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
492 service = jid.JID(service) if service else None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
493 if not node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
494 node = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
495 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
496 if not sub_id:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
497 sub_id = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
498 extra = self._p.parseExtra(extra_dict)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
499
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
500 return client, service, node, max_items, extra, sub_id
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
501
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
502 def _get(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
503 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
504 service="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
505 node="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
506 max_items=10,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
507 item_ids=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
508 sub_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
509 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
510 default_node=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
511 form_ns=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
512 filters=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
513 profile_key=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
514 ):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
515 """Bridge method to retrieve data from node with schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
516
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
517 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
518 when adding *Get methods
2603
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
519 extra can have the key "labels_as_list" which is a hack to convert
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
520 labels from textbox to list in XMLUI, which usually render better
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
521 in final UI.
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
522 """
2603
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
523 if filters is None:
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
524 filters = {}
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
525 if extra is None:
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
526 extra = {}
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
527 # XXX: Q&D way to get list for labels when displaying them, but text when we
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
528 # have to modify them
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
529 if C.bool(extra.get("labels_as_list", C.BOOL_FALSE)):
2603
5d4ac5415b40 plugins schema, merge-requests, tickets: convert labels from textbox to list only when "labels_as_list" is set in extra parameters.
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
530 filters = filters.copy()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
531 filters[u"labels"] = self.textbox2ListFilter
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
532 client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
533 service, node, max_items, sub_id, extra, profile_key
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
534 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
535 d = self.getDataFormItems(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
536 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
537 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
538 node or None,
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
539 max_items=max_items,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
540 item_ids=item_ids,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
541 sub_id=sub_id,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
542 rsm_request=extra.rsm_request,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
543 extra=extra.extra,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
544 default_node=default_node,
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
545 form_ns=form_ns,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
546 filters=filters,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
547 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
548 d.addCallback(self._p.serItemsData)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
549 return d
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
550
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
551 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
552 """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
553
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
554 @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
555 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
556 client = self.host.getClient(profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
557 service = None if not service else jid.JID(service)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
558 if schema:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
559 schema = generic.parseXml(schema.encode("utf-8"))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
560 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
561 schema = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
562 if extra and u"update" in extra:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
563 extra[u"update"] = C.bool(extra[u"update"])
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
564 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
565
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
566 def _set(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
567 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
568 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
569 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
570 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
571 schema=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
572 item_id=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
573 extra=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
574 default_node=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
575 form_ns=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
576 fill_author=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
577 profile_key=C.PROF_KEY_NONE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
578 ):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
579 """Bridge method to set item in node with schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
580
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
581 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
582 when adding *Set methods
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
583 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
584 client, service, node, schema, item_id, extra = self.prepareBridgeSet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
585 service, node, schema, item_id, extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
586 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
587 d = self.set(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
588 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
589 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
590 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
591 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
592 schema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
593 item_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
594 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
595 deserialise=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
596 form_ns=form_ns,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
597 default_node=default_node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
598 fill_author=fill_author,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
599 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
600 d.addCallback(lambda ret: ret or u"")
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
601 return d
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
602
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
603 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
604 def set(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
605 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
606 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
607 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
608 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
609 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
610 schema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
611 item_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
612 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
613 deserialise,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
614 form_ns,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
615 default_node=None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
616 fill_author=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
617 ):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
618 """Set an item in a node with a schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
619
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
620 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
621 @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
622 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
623 'created' and 'updated' will be forced to current time:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
624 - '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
625 - 'updated' is set everytime
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
626 @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
627 - 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
628 if True, item_id must be None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
629 @param form_ns (unicode, None): namespace of the form
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
630 needed when an update is done
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
631 @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
632 other arguments are same as for [self._s.sendDataFormItem]
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
633 @return (unicode): id of the created item
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
634 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
635 if not node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
636 if default_node is None:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
637 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
638 node = default_node
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
639 now = utils.xmpp_date()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
640 if not item_id:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
641 values["created"] = now
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
642 elif extra.get(u"update", False):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
643 if item_id is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
644 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
645 _(u'if extra["update"] is set, item_id must be set too')
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
646 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
647 try:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
648 # we get previous item
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
649 items_data = yield self._p.getItems(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
650 client, service, node, item_ids=[item_id]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
651 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
652 item_elt = items_data[0][0]
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
653 except Exception as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
654 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
655 _(u"Can't get previous item, update ignored: {reason}").format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
656 reason=e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
657 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
658 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
659 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
660 # and parse it
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
661 form = data_form.findForm(item_elt, form_ns)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
662 if form is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
663 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
664 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
665 u"Can't parse previous item, update ignored: data form not found"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
666 ).format(reason=e)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
667 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
668 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
669 for name, field in form.fields.iteritems():
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
670 if name not in values:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
671 values[name] = u"\n".join(unicode(v) for v in field.values)
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
672
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
673 values["updated"] = now
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
674 if fill_author:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
675 if not values.get("author"):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
676 identity = yield self._i.getIdentity(client, client.jid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
677 values["author"] = identity["nick"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
678 if not values.get("author_jid"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
679 values["author_jid"] = client.jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
680 item_id = yield self.sendDataFormItem(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
681 client, service, node, values, schema, item_id, extra, deserialise
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
682 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
683 defer.returnValue(item_id)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
684
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
685
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
686 class SchemaHandler(XMPPHandler):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
687 implements(iwokkel.IDisco)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
688
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
689 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
690 return [disco.DiscoFeature(NS_SCHEMA)]
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
691
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
692 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
693 return []