annotate sat/plugins/plugin_xep_0346.py @ 3708:09112b1c3e05

plugin events: store `interest_id` in data: ID of the item in list of interest is needed to correctly modify (and notably retract) an event.
author Goffi <goffi@goffi.org>
date Fri, 12 Nov 2021 17:18:23 +0100
parents 422049bb81d3
children 909b56b115ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
3 # SàT plugin for XEP-0346
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3473
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # 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
7 # 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
8 # 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
9 # (at your option) any later version.
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # 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
12 # 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
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # 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
17 # 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
18
3614
422049bb81d3 plugin XEP-0346: fix deprecated import
Goffi <goffi@goffi.org>
parents: 3509
diff changeset
19 from collections.abc import Iterable
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
20 import itertools
3473
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
21 from typing import Optional
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
22 from zope.interface import implementer
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
23 from twisted.words.protocols.jabber import jid
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
24 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
25 from twisted.words.xish import domish
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
26 from twisted.internet import defer
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
27 from wokkel import disco, iwokkel
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
28 from wokkel import data_form
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
29 from wokkel import generic
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.core.i18n import _
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.core import exceptions
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.core.constants import Const as C
3473
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
33 from sat.core.xmpp import SatXMPPEntity
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
34 from sat.tools import xml_tools
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
35 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
36 from sat.tools.common import date_utils
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
37 from sat.tools.common import data_format
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
39
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 log = getLogger(__name__)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
42 NS_FDP = "urn:xmpp:fdp:0"
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
43 TEMPLATE_PREFIX = "fdp/template/"
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
44 SUBMITTED_PREFIX = "fdp/submitted/"
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 PLUGIN_INFO = {
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
47 C.PI_NAME: "Form Discovery and Publishing",
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
48 C.PI_IMPORT_NAME: "XEP-0346",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
49 C.PI_TYPE: "EXP",
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_PROTOCOLS: [],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
51 C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
52 C.PI_MAIN: "PubsubSchema",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
53 C.PI_HANDLER: "yes",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
54 C.PI_DESCRIPTION: _("""Handle Pubsub data schemas"""),
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 }
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 class PubsubSchema(object):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
60 log.info(_("PubSub Schema initialization"))
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.host = host
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
62 self._p = self.host.plugins["XEP-0060"]
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
63 self._i = self.host.plugins["IDENTITY"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
64 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
65 "psSchemaGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
66 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
67 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
68 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
69 method=self._getSchema,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
70 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
71 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
72 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
73 "psSchemaSet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
74 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
75 in_sign="ssss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
76 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
77 method=self._setSchema,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
78 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
79 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
80 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
81 "psSchemaUIGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
82 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
83 in_sign="sss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
84 out_sign="s",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
85 method=lambda service, nodeIdentifier, profile_key: self._getUISchema(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
86 service, nodeIdentifier, default_node=None, profile_key=profile_key),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
87 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
88 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
89 host.bridge.addMethod(
3473
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
90 "psSchemaDictGet",
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
91 ".plugin",
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
92 in_sign="sss",
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
93 out_sign="s",
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
94 method=self._getSchemaDict,
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
95 async_=True,
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
96 )
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
97 host.bridge.addMethod(
3509
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
98 "psSchemaApplicationNSGet",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
99 ".plugin",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
100 in_sign="s",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
101 out_sign="s",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
102 method=self.getApplicationNS,
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
103 )
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
104 host.bridge.addMethod(
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
105 "psSchemaTemplateNodeGet",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
106 ".plugin",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
107 in_sign="s",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
108 out_sign="s",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
109 method=self.getTemplateNS,
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
110 )
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
111 host.bridge.addMethod(
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
112 "psSchemaSubmittedNodeGet",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
113 ".plugin",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
114 in_sign="s",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
115 out_sign="s",
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
116 method=self.getSubmittedNS,
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
117 )
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
118 host.bridge.addMethod(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
119 "psItemsFormGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
120 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
121 in_sign="ssssiassa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
122 out_sign="(asa{ss})",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
123 method=self._getDataFormItems,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
124 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
125 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
126 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
127 "psItemFormSend",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
128 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
129 in_sign="ssa{sas}ssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
130 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
131 method=self._sendDataFormItem,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
132 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
133 )
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
134
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def getHandler(self, client):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
136 return SchemaHandler()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
138 def getApplicationNS(self, namespace):
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
139 """Retrieve application namespace, i.e. namespace without FDP prefix"""
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
140 if namespace.startswith(SUBMITTED_PREFIX):
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
141 namespace = namespace[len(SUBMITTED_PREFIX):]
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
142 elif namespace.startswith(TEMPLATE_PREFIX):
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
143 namespace = namespace[len(TEMPLATE_PREFIX):]
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
144 return namespace
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
145
3509
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
146 def getTemplateNS(self, namespace: str) -> str:
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
147 """Returns node used for data template (i.e. schema)"""
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
148 app_ns = self.getApplicationNS(namespace)
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
149 return f"{TEMPLATE_PREFIX}{app_ns}"
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
150
3457
6791103de47d plugin XEP-0346: use submitted namespace for node:
Goffi <goffi@goffi.org>
parents: 3452
diff changeset
151 def getSubmittedNS(self, namespace: str) -> str:
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
152 """Returns node to use to submit forms"""
3457
6791103de47d plugin XEP-0346: use submitted namespace for node:
Goffi <goffi@goffi.org>
parents: 3452
diff changeset
153 return f"{SUBMITTED_PREFIX}{self.getApplicationNS(namespace)}"
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
154
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def _getSchemaBridgeCb(self, schema_elt):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
156 if schema_elt is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
157 return ""
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158 return schema_elt.toXml()
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
159
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160 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
161 client = self.host.getClient(profile_key)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162 service = None if not service else jid.JID(service)
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
163 d = defer.ensureDeferred(self.getSchema(client, service, nodeIdentifier))
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
164 d.addCallback(self._getSchemaBridgeCb)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
165 return d
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
166
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
167 async def getSchema(self, client, service, nodeIdentifier):
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 """retrieve PubSub node schema
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 @param service(jid.JID, None): jid of PubSub service
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171 None to use our PEP
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172 @param nodeIdentifier(unicode): node to get schema from
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173 @return (domish.Element, None): schema (<x> element)
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
174 None if no schema has been set on this node
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 """
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
176 app_ns = self.getApplicationNS(nodeIdentifier)
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
177 node_id = f"{TEMPLATE_PREFIX}{app_ns}"
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
178 items_data = await self._p.getItems(client, service, node_id, max_items=1)
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
179 try:
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
180 schema = next(items_data[0][0].elements(data_form.NS_X_DATA, 'x'))
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
181 except IndexError:
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
182 schema = None
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
183 except StopIteration:
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
184 log.warning(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
185 f"No schema found in item of {service!r} at node {nodeIdentifier!r}: "
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
186 f"\n{items_data[0][0].toXml()}"
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
187 )
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
188 schema = None
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
189 return schema
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
190
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
191 async def getSchemaForm(self, client, service, nodeIdentifier, schema=None,
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
192 form_type="form", copy_form=True):
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
193 """Get data form from node's schema
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
194
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
195 @param service(None, jid.JID): PubSub service
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
196 @param nodeIdentifier(unicode): node
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
197 @param schema(domish.Element, data_form.Form, None): node schema
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
198 if domish.Element, will be converted to data form
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
199 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
200 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
201 @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
202 @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
203 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
204 @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
205 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
206 """
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
207 if schema is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
208 log.debug(_("unspecified schema, we need to request it"))
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
209 schema = await self.getSchema(client, service, nodeIdentifier)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
210 if schema is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
211 raise exceptions.DataError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
212 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
213 "no schema specified, and this node has no schema either, we can't construct the data form"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
214 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
215 )
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
216 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
217 if copy_form:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
218 # XXX: we don't use deepcopy as it will do an infinite loop if a
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
219 # domish.Element is present in the form fields (happens for
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
220 # XEP-0315 data forms XML Element)
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
221 schema = data_form.Form(
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
222 formType = schema.formType,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
223 title = schema.title,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
224 instructions = schema.instructions[:],
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
225 formNamespace = schema.formNamespace,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
226 fields = schema.fieldList,
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
227 )
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
228 return schema
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
229
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
230 try:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
231 form = data_form.Form.fromElement(schema)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
232 except data_form.Error as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
233 raise exceptions.DataError(_("Invalid Schema: {msg}").format(msg=e))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
234 form.formType = form_type
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
235 return form
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
236
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
237 def schema2XMLUI(self, schema_elt):
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
238 form = data_form.Form.fromElement(schema_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
239 xmlui = xml_tools.dataForm2XMLUI(form, "")
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
240 return xmlui
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
241
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
242 def _getUISchema(self, service, nodeIdentifier, default_node=None,
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
243 profile_key=C.PROF_KEY_NONE):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
244 if not nodeIdentifier:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
245 if not default_node:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
246 raise ValueError(_("nodeIndentifier needs to be set"))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
247 nodeIdentifier = default_node
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
248 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
249 service = None if not service else jid.JID(service)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
250 d = self.getUISchema(client, service, nodeIdentifier)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
251 d.addCallback(lambda xmlui: xmlui.toXml())
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
252 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
253
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
254 def getUISchema(self, client, service, nodeIdentifier):
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
255 d = defer.ensureDeferred(self.getSchema(client, service, nodeIdentifier))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
256 d.addCallback(self.schema2XMLUI)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
257 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
258
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
259 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
260 client = self.host.getClient(profile_key)
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
261 service = None if not service else jid.JID(service)
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
262 schema = generic.parseXml(schema.encode())
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
263 return defer.ensureDeferred(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
264 self.setSchema(client, service, nodeIdentifier, schema)
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
265 )
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
267 async def setSchema(self, client, service, nodeIdentifier, schema):
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
268 """Set or replace PubSub node schema
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
270 @param schema(domish.Element, None): schema to set
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
271 None if schema need to be removed
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272 """
3509
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
273 node_id = self.getTemplateNS(nodeIdentifier)
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
274 node_options = {
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
275 self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
276 self._p.OPT_PERSIST_ITEMS: 1,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
277 self._p.OPT_MAX_ITEMS: 1,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
278 self._p.OPT_DELIVER_PAYLOADS: 1,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
279 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
280 self._p.OPT_PUBLISH_MODEL: self._p.PUBLISH_MODEL_PUBLISHERS,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
281 }
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
282 await self._p.createIfNewNode(client, service, node_id, node_options)
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
283 await self._p.sendItem(client, service, node_id, schema, self._p.ID_SINGLETON)
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284
3473
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
285 def _getSchemaDict(self, service, nodeIdentifier, profile):
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
286 service = None if not service else jid.JID(service)
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
287 client = self.host.getClient(profile)
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
288 d = defer.ensureDeferred(self.getSchemaDict(client, service, nodeIdentifier))
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
289 d.addCallback(data_format.serialise)
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
290 return d
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
291
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
292 async def getSchemaDict(
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
293 self,
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
294 client: SatXMPPEntity,
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
295 service: Optional[jid.JID],
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
296 nodeIdentifier: str) -> dict:
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
297 """Retrieve a node schema and format it a simple dictionary
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
298
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
299 The dictionary is made so it can be easily serialisable
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
300 """
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
301 schema_form = await self.getSchemaForm(client, service, nodeIdentifier)
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
302 return xml_tools.dataForm2dataDict(schema_form)
cc065c13052c plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents: 3460
diff changeset
303
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
304 def _getDataFormItems(self, form_ns="", service="", node="", schema="", max_items=10,
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
305 item_ids=None, sub_id=None, extra_dict=None,
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
306 profile_key=C.PROF_KEY_NONE):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
307 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
308 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
309 if not node:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
310 raise exceptions.DataError(_("empty node is not allowed"))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
311 if schema:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
312 schema = generic.parseXml(schema.encode("utf-8"))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
313 else:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
314 schema = None
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
315 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
316 extra = self._p.parseExtra(extra_dict)
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
317 d = defer.ensureDeferred(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
318 self.getDataFormItems(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
319 client,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
320 service,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
321 node,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
322 schema,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
323 max_items or None,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
324 item_ids,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
325 sub_id or None,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
326 extra.rsm_request,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
327 extra.extra,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
328 form_ns=form_ns or None,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
329 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
330 )
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2784
diff changeset
331 d.addCallback(self._p.transItemsData)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
332 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
333
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
334 async def getDataFormItems(self, client, service, nodeIdentifier, schema=None,
2760
3480d4fdf83a plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
335 max_items=None, item_ids=None, sub_id=None, rsm_request=None,
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
336 extra=None, default_node=None, form_ns=None, filters=None):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
337 """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
338
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
339 @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
340 if None, it will be retrieved from node
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
341 @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
342 @param form_ns (unicode, None): namespace of the form
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
343 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
344 @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
345 other parameters as the same as for [getItems]
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
346 @return (list[unicode]): XMLUI of the forms
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
347 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
348 it will be skipped
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
349 @raise ValueError: one argument is invalid
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
350 """
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
351 if not nodeIdentifier:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
352 if not default_node:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
353 raise ValueError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
354 _("default_node must be set if nodeIdentifier is not set")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
355 )
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
356 nodeIdentifier = default_node
3457
6791103de47d plugin XEP-0346: use submitted namespace for node:
Goffi <goffi@goffi.org>
parents: 3452
diff changeset
357 submitted_ns = self.getSubmittedNS(nodeIdentifier)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
358 # we need the initial form to get options of fields when suitable
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
359 schema_form = await self.getSchemaForm(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
360 client, service, nodeIdentifier, schema, form_type="result", copy_form=False
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
361 )
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
362 items_data = await self._p.getItems(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
363 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
364 service,
3457
6791103de47d plugin XEP-0346: use submitted namespace for node:
Goffi <goffi@goffi.org>
parents: 3452
diff changeset
365 submitted_ns,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
366 max_items,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
367 item_ids,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
368 sub_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
369 rsm_request,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
370 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
371 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
372 items, metadata = items_data
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
373 items_xmlui = []
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
374 for item_elt in items:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
375 for x_elt in item_elt.elements((data_form.NS_X_DATA, "x")):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
376 form = data_form.Form.fromElement(x_elt)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
377 if form_ns and form.formNamespace != form_ns:
3460
d4a71a1dac88 plugin misc lists: templates:
Goffi <goffi@goffi.org>
parents: 3457
diff changeset
378 log.debug(
d4a71a1dac88 plugin misc lists: templates:
Goffi <goffi@goffi.org>
parents: 3457
diff changeset
379 f"form's namespace ({form.formNamespace!r}) differs from expected"
d4a71a1dac88 plugin misc lists: templates:
Goffi <goffi@goffi.org>
parents: 3457
diff changeset
380 f"{form_ns!r}"
d4a71a1dac88 plugin misc lists: templates:
Goffi <goffi@goffi.org>
parents: 3457
diff changeset
381 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
382 continue
3271
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
383 prepend = [
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
384 ("label", "id"),
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
385 ("text", item_elt["id"], "id"),
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
386 ("label", "publisher"),
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
387 ]
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
388 try:
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
389 publisher = jid.JID(item_elt['publisher'])
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
390 except (KeyError, jid.InvalidFormat):
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
391 pass
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
392 else:
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
393 prepend.append(("jid", publisher, "publisher"))
2381
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
394 xmlui = xml_tools.dataFormResult2XMLUI(
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
395 form,
72c30e73a9a5 plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents: 2378
diff changeset
396 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
397 # 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
398 # are not checked
3271
abca25af06d7 plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
399 prepend=prepend,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
400 filters=filters,
2958
e2cb04b381bb tools (xml_tools): added "read_only" argument in dataFormResult2XMLUI (and use it in plugin pubsub_schema)
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
401 read_only=False,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
402 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
403 items_xmlui.append(xmlui)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
404 break
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
405 return (items_xmlui, metadata)
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
406
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
407 def _sendDataFormItem(self, service, nodeIdentifier, values, schema=None,
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
408 item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
409 client = self.host.getClient(profile_key)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
410 service = None if not service else jid.JID(service)
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
411 if schema:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
412 schema = generic.parseXml(schema.encode("utf-8"))
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
413 else:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
414 schema = None
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
415 d = defer.ensureDeferred(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
416 self.sendDataFormItem(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
417 client,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
418 service,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
419 nodeIdentifier,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
420 values,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
421 schema,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
422 item_id or None,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
423 extra,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
424 deserialise=True,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
425 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
426 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
427 d.addCallback(lambda ret: ret or "")
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
428 return d
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
429
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
430 async def sendDataFormItem(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
431 self, client, service, nodeIdentifier, values, schema=None, item_id=None,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
432 extra=None, deserialise=False):
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
433 """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
434
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
435 @param values(dict[key(unicode), [iterable[object], object]]): values set for the
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
436 form. If not iterable, will be put in a list.
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
437 @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
438 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
439 Schema is needed to construct data form to publish
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
440 @param deserialise(bool): if True, data are list of unicode and must be
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
441 deserialized according to expected type.
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
442 This is done in this method and not directly in _sendDataFormItem because we
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
443 need to know the data type which is in the form, not availablable in
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
444 _sendDataFormItem
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
445 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
446 @return (unicode): id of the created item
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
447 """
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
448 form = await self.getSchemaForm(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
449 client, service, nodeIdentifier, schema, form_type="submit"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
450 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
451
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
452 for name, values_list in values.items():
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
453 try:
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
454 field = form.fields[name]
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
455 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
456 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
457 _("field {name} doesn't exist, ignoring it").format(name=name)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
458 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
459 continue
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
460 if isinstance(values_list, str) or not isinstance(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
461 values_list, Iterable
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
462 ):
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
463 values_list = [values_list]
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
464 if deserialise:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
465 if field.fieldType == "boolean":
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
466 values_list = [C.bool(v) for v in values_list]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
467 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
468 # 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
469 values_list = list(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
470 itertools.chain(*[v.splitlines() for v in values_list])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
471 )
2784
76ebecdb9b1e plugin pubsub schema: handle XHTML fields
Goffi <goffi@goffi.org>
parents: 2774
diff changeset
472 elif xml_tools.isXHTMLField(field):
76ebecdb9b1e plugin pubsub schema: handle XHTML fields
Goffi <goffi@goffi.org>
parents: 2774
diff changeset
473 values_list = [generic.parseXml(v.encode("utf-8"))
76ebecdb9b1e plugin pubsub schema: handle XHTML fields
Goffi <goffi@goffi.org>
parents: 2774
diff changeset
474 for v in values_list]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
475 elif "jid" in (field.fieldType or ""):
2371
2268df8c99bf plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents: 2363
diff changeset
476 values_list = [jid.JID(v) for v in values_list]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
477 if "list" in (field.fieldType or ""):
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
478 # 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
479 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
480 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
481 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
482 # 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
483 values_list = field.values
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
484 elif field.ext_type == 'xml':
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
485 # FIXME: XML elements are not handled correctly, we need to know if we
3071
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
486 # have actual XML/XHTML, or text to escape
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
487 for idx, value in enumerate(values_list[:]):
3071
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
488 if isinstance(value, domish.Element):
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
489 if (field.value and (value.name != field.value.name
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
490 or value.uri != field.value.uri)):
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
491 # the element is not the one expected in form, so we create the right element
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
492 # to wrap the current value
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
493 wrapper_elt = domish.Element((field.value.uri, field.value.name))
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
494 wrapper_elt.addChild(value)
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
495 values_list[idx] = wrapper_elt
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
496 else:
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
497 # we have to convert the value to a domish.Element
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
498 if field.value and field.value.uri == C.NS_XHTML:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
499 div_elt = domish.Element((C.NS_XHTML, 'div'))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
500 div_elt.addContent(str(value))
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
501 values_list[idx] = div_elt
3071
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
502 else:
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
503 # only XHTML fields are handled for now
68d423f4fb55 plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents: 3040
diff changeset
504 raise NotImplementedError
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
505
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
506 field.values = values_list
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
507
3509
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
508 return await self._p.sendItem(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
509 client, service, nodeIdentifier, form.toElement(), item_id, extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
510 )
2363
41c7717b52cd plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents: 2350
diff changeset
511
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
512 ## filters ##
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
513 # filters useful for data form to XMLUI conversion #
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
514
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
515 def valueOrPublisherFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
516 """Replace missing value by publisher's user part"""
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
517 if not args[0]:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
518 # 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
519 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
520 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
521 except (KeyError, RuntimeError):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
522 pass
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
523 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
524 args[0] = publisher.user.capitalize()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
525 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
526
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
527 def textbox2ListFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
528 """Split lines of a textbox in a list
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
529
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
530 main use case is using a textbox for labels
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
531 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
532 if widget_type != "textbox":
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
533 return widget_type, args, kwargs
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
534 widget_type = "list"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
535 options = [o for o in args.pop(0).split("\n") if o]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
536 kwargs = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
537 "options": options,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
538 "name": kwargs.get("name"),
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
539 "styles": ("noselect", "extensible", "reducible"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
540 }
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
541 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
542
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
543 def dateFilter(self, form_xmlui, widget_type, args, kwargs):
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
544 """Convert a string with a date to a unix timestamp"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
545 if widget_type != "string" or not args[0]:
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
546 return widget_type, args, kwargs
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
547 # we convert XMPP date to timestamp
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
548 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
549 args[0] = str(date_utils.date_parse(args[0]))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
550 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
551 log.warning(_("Can't parse date field: {msg}").format(msg=e))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
552 return widget_type, args, kwargs
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 ## Helper 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 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
557 """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
558
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
559 @return (tuple): (client, service, node, max_items, extra, sub_id) usable for
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
560 internal methods
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
561 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
562 client = self.host.getClient(profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
563 service = jid.JID(service) if service else None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
564 if not node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
565 node = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
566 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
567 if not sub_id:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
568 sub_id = None
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
569 extra = self._p.parseExtra(extra_dict)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
570
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
571 return client, service, node, max_items, extra, sub_id
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
572
2760
3480d4fdf83a plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
573 def _get(self, service="", node="", max_items=10, item_ids=None, sub_id=None,
3480d4fdf83a plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
574 extra=None, default_node=None, form_ns=None, filters=None,
3480d4fdf83a plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
575 profile_key=C.PROF_KEY_NONE):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
576 """Bridge method to retrieve data from node with schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
577
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
578 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
579 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
580 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
581 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
582 in final UI.
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
583 """
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
584 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
585 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
586 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
587 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
588 # 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
589 # have to modify them
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
590 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
591 filters = filters.copy()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
592 filters["labels"] = self.textbox2ListFilter
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
593 client, service, node, max_items, extra, sub_id = self.prepareBridgeGet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
594 service, node, max_items, sub_id, extra, profile_key
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
595 )
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
596 d = defer.ensureDeferred(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
597 self.getDataFormItems(
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
598 client,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
599 service,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
600 node or None,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
601 max_items=max_items,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
602 item_ids=item_ids,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
603 sub_id=sub_id,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
604 rsm_request=extra.rsm_request,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
605 extra=extra.extra,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
606 default_node=default_node,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
607 form_ns=form_ns,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
608 filters=filters,
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
609 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
610 )
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2784
diff changeset
611 d.addCallback(self._p.transItemsData)
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
612 d.addCallback(lambda data: data_format.serialise(data))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
613 return d
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
614
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
615 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
616 """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
617
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
618 @return (tuple): (client, service, node, schema, item_id, extra) usable for
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
619 internal methods
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
620 """
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
621 client = self.host.getClient(profile_key)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
622 service = None if not service else jid.JID(service)
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
623 if schema:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
624 schema = generic.parseXml(schema.encode("utf-8"))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
625 else:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
626 schema = None
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
627 extra = data_format.deserialise(extra)
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
628 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
629
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
630 @defer.inlineCallbacks
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
631 def copyMissingValues(self, client, service, node, item_id, form_ns, values):
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
632 """Retrieve values existing in original item and missing in update
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
633
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
634 Existing item will be retrieve, and values not already specified in values will
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
635 be filled
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
636 @param service: same as for [XEP_0060.getItems]
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
637 @param node: same as for [XEP_0060.getItems]
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
638 @param item_id(unicode): id of the item to retrieve
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
639 @param form_ns (unicode, None): namespace of the form
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
640 @param values(dict): values to fill
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
641 This dict will be modified *in place* to fill value present in existing
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
642 item and missing in the dict.
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
643 """
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
644 try:
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
645 # we get previous item
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
646 items_data = yield self._p.getItems(
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
647 client, service, node, item_ids=[item_id]
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
648 )
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
649 item_elt = items_data[0][0]
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
650 except Exception as e:
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
651 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
652 _("Can't get previous item, update ignored: {reason}").format(
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
653 reason=e
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
654 )
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
655 )
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
656 else:
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
657 # and parse it
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
658 form = data_form.findForm(item_elt, form_ns)
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
659 if form is None:
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
660 log.warning(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
661 _("Can't parse previous item, update ignored: data form not found")
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
662 )
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
663 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
664 for name, field in form.fields.items():
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
665 if name not in values:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
666 values[name] = "\n".join(str(v) for v in field.values)
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
667
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
668 def _set(self, service, node, values, schema=None, item_id=None, extra=None,
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
669 default_node=None, form_ns=None, fill_author=True,
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
670 profile_key=C.PROF_KEY_NONE):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
671 """Bridge method to set item in node with schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
672
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
673 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
674 when adding *Set methods
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
675 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
676 client, service, node, schema, item_id, extra = self.prepareBridgeSet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
677 service, node, schema, item_id, extra
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
678 )
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
679 d = defer.ensureDeferred(self.set(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
680 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
681 service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
682 node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
683 values,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
684 schema,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
685 item_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
686 extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
687 deserialise=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
688 form_ns=form_ns,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
689 default_node=default_node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
690 fill_author=fill_author,
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
691 ))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
692 d.addCallback(lambda ret: ret or "")
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
693 return d
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
694
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
695 async def set(
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
696 self, client, service, node, values, schema, item_id, extra, deserialise,
2774
95321f233387 plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
697 form_ns, default_node=None, fill_author=True):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
698 """Set an item in a node with a schema
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
699
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
700 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
701 @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
702 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
703 'created' and 'updated' will be forced to current time:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
704 - '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
705 - 'updated' is set everytime
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
706 @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
707 - update(bool): if True, get previous item data to merge with current one
3509
b977c74f9c85 plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
708 if True, item_id must be set
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
709 @param form_ns (unicode, None): namespace of the form
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
710 needed when an update is done
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
711 @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
712 other arguments are same as for [self._s.sendDataFormItem]
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
713 @return (unicode): id of the created item
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
714 """
2959
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
715 if extra is None:
989b622faff6 plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents: 2958
diff changeset
716 extra = {}
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
717 if not node:
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
718 if default_node is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
719 raise ValueError(_("default_node must be set if node is not set"))
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
720 node = default_node
3457
6791103de47d plugin XEP-0346: use submitted namespace for node:
Goffi <goffi@goffi.org>
parents: 3452
diff changeset
721 node = self.getSubmittedNS(node)
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
722 now = utils.xmpp_date()
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
723 if not item_id:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
724 values["created"] = now
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
725 elif extra.get("update", False):
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
726 if item_id is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
727 raise exceptions.DataError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
728 _('if extra["update"] is set, item_id must be set too')
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
729 )
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
730 await self.copyMissingValues(client, service, node, item_id, form_ns, values)
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
731
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
732 values["updated"] = now
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
733 if fill_author:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
734 if not values.get("author"):
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
735 id_data = await self._i.getIdentity(client, None, ["nicknames"])
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
736 values["author"] = id_data['nicknames'][0]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
737 if not values.get("author_jid"):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
738 values["author_jid"] = client.jid.full()
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
739 item_id = await self.sendDataFormItem(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
740 client, service, node, values, schema, item_id, extra, deserialise
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
741 )
3309
71761e9fb984 plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents: 3271
diff changeset
742 return item_id
2471
544c4d2fec45 plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents: 2426
diff changeset
743
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
744
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2959
diff changeset
745 @implementer(iwokkel.IDisco)
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
746 class SchemaHandler(XMPPHandler):
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
747
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
748 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
3452
bb0225aaf4e6 plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents: 3309
diff changeset
749 return [disco.DiscoFeature(NS_FDP)]
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
750
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
751 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
2350
388226e9c3ff plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
752 return []