Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0346.py @ 4332:71c939e34ca6
XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author | Syndace <me@syndace.dev> |
---|---|
date | Sat, 13 Jul 2024 18:28:28 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 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 | 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 | 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 | 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 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
30 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
31 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
32 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
33 from libervia.backend.core.xmpp import SatXMPPEntity |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.tools import xml_tools |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.tools import utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
36 from libervia.backend.tools.common import date_utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
37 from libervia.backend.tools.common import data_format |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
38 from libervia.backend.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 | 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 | 51 C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"], |
52 C.PI_MAIN: "PubsubSchema", | |
53 C.PI_HANDLER: "yes", | |
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 | 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"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
64 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
65 "ps_schema_get", |
2624
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", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
69 method=self._get_schema, |
3028 | 70 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
71 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
72 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
73 "ps_schema_set", |
2624
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="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
77 method=self._set_schema, |
3028 | 78 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
79 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
80 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
81 "ps_schema_ui_get", |
2624
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", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
85 method=lambda service, nodeIdentifier, profile_key: self._get_ui_schema( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
86 service, nodeIdentifier, default_node=None, profile_key=profile_key |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
87 ), |
3028 | 88 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
89 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
90 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
91 "ps_schema_dict_get", |
3473
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
92 ".plugin", |
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
93 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
|
94 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
95 method=self._get_schema_dict, |
3473
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
96 async_=True, |
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
97 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
98 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
99 "ps_schema_application_ns_get", |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
100 ".plugin", |
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
101 in_sign="s", |
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
102 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
103 method=self.get_application_ns, |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
104 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
105 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
106 "ps_schema_template_node_get", |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
107 ".plugin", |
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
108 in_sign="s", |
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
109 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
110 method=self.get_template_ns, |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
111 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
112 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
113 "ps_schema_submitted_node_get", |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
114 ".plugin", |
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
115 in_sign="s", |
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
116 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
117 method=self.get_submitted_ns, |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
118 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
119 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
120 "ps_items_form_get", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
121 ".plugin", |
3586
5f65f4e9f8cb
plugin XEP-0060: getItems extra is now serialised dict
Goffi <goffi@goffi.org>
parents:
3584
diff
changeset
|
122 in_sign="ssssiassss", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
123 out_sign="(asa{ss})", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
124 method=self._get_data_form_items, |
3028 | 125 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
126 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
127 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
128 "ps_item_form_send", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
129 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
130 in_sign="ssa{sas}ssa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
131 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
132 method=self._send_data_form_item, |
3028 | 133 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
134 ) |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
136 def get_handler(self, client): |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 return SchemaHandler() |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
139 def get_application_ns(self, namespace): |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
140 """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
|
141 if namespace.startswith(SUBMITTED_PREFIX): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
142 namespace = namespace[len(SUBMITTED_PREFIX) :] |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
143 elif namespace.startswith(TEMPLATE_PREFIX): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
144 namespace = namespace[len(TEMPLATE_PREFIX) :] |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
145 return namespace |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
146 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
147 def get_template_ns(self, namespace: str) -> str: |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
148 """Returns node used for data template (i.e. schema)""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
149 app_ns = self.get_application_ns(namespace) |
3509
b977c74f9c85
plugin XEP-0346: bridge method to retrieve template, submitted, or application namespaces
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
150 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
|
151 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
152 def get_submitted_ns(self, namespace: str) -> str: |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
153 """Returns node to use to submit forms""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
154 return f"{SUBMITTED_PREFIX}{self.get_application_ns(namespace)}" |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
155 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
156 def _get_schema_bridge_cb(self, schema_elt): |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 if schema_elt is None: |
3028 | 158 return "" |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 return schema_elt.toXml() |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
161 def _get_schema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
162 client = self.host.get_client(profile_key) |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 service = None if not service else jid.JID(service) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
164 d = defer.ensureDeferred(self.get_schema(client, service, nodeIdentifier)) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
165 d.addCallback(self._get_schema_bridge_cb) |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 return d |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
168 async def get_schema(self, client, service, nodeIdentifier): |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 """retrieve PubSub node schema |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 @param service(jid.JID, None): jid of PubSub service |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 None to use our PEP |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 @param nodeIdentifier(unicode): node to get schema from |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 @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
|
175 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
|
176 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
177 app_ns = self.get_application_ns(nodeIdentifier) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
178 node_id = f"{TEMPLATE_PREFIX}{app_ns}" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
179 items_data = await self._p.get_items(client, service, node_id, max_items=1) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
180 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
181 schema = next(items_data[0][0].elements(data_form.NS_X_DATA, "x")) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
182 except IndexError: |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
183 schema = None |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
184 except StopIteration: |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
185 log.warning( |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
186 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
|
187 f"\n{items_data[0][0].toXml()}" |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
188 ) |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
189 schema = None |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
190 return schema |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
192 async def get_schema_form( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
193 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
194 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
195 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
196 nodeIdentifier, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
197 schema=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
198 form_type="form", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
199 copy_form=True, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
200 ): |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
201 """Get data form from node's schema |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
202 |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
203 @param service(None, jid.JID): PubSub service |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
204 @param nodeIdentifier(unicode): node |
2371
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
205 @param schema(domish.Element, data_form.Form, None): node schema |
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
206 if domish.Element, will be converted to data form |
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
207 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
|
208 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
|
209 @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
|
210 @param copy_form(bool): if True and if schema is already a data_form.Form, will deep copy it before returning |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
211 needed when the form is reused and it will be modified (e.g. in send_data_form_item) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
212 @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
|
213 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
|
214 """ |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
215 if schema is None: |
3028 | 216 log.debug(_("unspecified schema, we need to request it")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
217 schema = await self.get_schema(client, service, nodeIdentifier) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
218 if schema is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
219 raise exceptions.DataError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
220 _( |
3028 | 221 "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
|
222 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
223 ) |
2371
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
224 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
|
225 if copy_form: |
3040 | 226 # XXX: we don't use deepcopy as it will do an infinite loop if a |
227 # domish.Element is present in the form fields (happens for | |
228 # XEP-0315 data forms XML Element) | |
229 schema = data_form.Form( | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
230 formType=schema.formType, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
231 title=schema.title, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
232 instructions=schema.instructions[:], |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
233 formNamespace=schema.formNamespace, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
234 fields=schema.fieldList, |
3040 | 235 ) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
236 return schema |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
237 |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
238 try: |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
239 form = data_form.Form.fromElement(schema) |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
240 except data_form.Error as e: |
3028 | 241 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
|
242 form.formType = form_type |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
243 return form |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
244 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
245 def schema_2_xmlui(self, schema_elt): |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
246 form = data_form.Form.fromElement(schema_elt) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
247 xmlui = xml_tools.data_form_2_xmlui(form, "") |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
248 return xmlui |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
249 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
250 def _get_ui_schema( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
251 self, service, nodeIdentifier, default_node=None, profile_key=C.PROF_KEY_NONE |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
252 ): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
253 if not nodeIdentifier: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
254 if not default_node: |
3028 | 255 raise ValueError(_("nodeIndentifier needs to be set")) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
256 nodeIdentifier = default_node |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
257 client = self.host.get_client(profile_key) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
258 service = None if not service else jid.JID(service) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
259 d = self.get_ui_schema(client, service, nodeIdentifier) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
260 d.addCallback(lambda xmlui: xmlui.toXml()) |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
261 return d |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
262 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
263 def get_ui_schema(self, client, service, nodeIdentifier): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
264 d = defer.ensureDeferred(self.get_schema(client, service, nodeIdentifier)) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
265 d.addCallback(self.schema_2_xmlui) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
266 return d |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
267 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
268 def _set_schema(self, service, nodeIdentifier, schema, profile_key=C.PROF_KEY_NONE): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
269 client = self.host.get_client(profile_key) |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 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
|
271 schema = generic.parseXml(schema.encode()) |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
272 return defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
273 self.set_schema(client, service, nodeIdentifier, schema) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
274 ) |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
276 async def set_schema(self, client, service, nodeIdentifier, schema): |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
277 """Set or replace PubSub node schema |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 @param schema(domish.Element, None): schema to set |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 None if schema need to be removed |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
282 node_id = self.get_template_ns(nodeIdentifier) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
283 node_options = { |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
284 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
|
285 self._p.OPT_PERSIST_ITEMS: 1, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
286 self._p.OPT_MAX_ITEMS: 1, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
287 self._p.OPT_DELIVER_PAYLOADS: 1, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
288 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
289 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
|
290 } |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
291 await self._p.create_if_new_node(client, service, node_id, node_options) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
292 await self._p.send_item(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
|
293 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
294 def _get_schema_dict(self, service, nodeIdentifier, profile): |
3473
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
295 service = None if not service else jid.JID(service) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
296 client = self.host.get_client(profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
297 d = defer.ensureDeferred(self.get_schema_dict(client, service, nodeIdentifier)) |
3473
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
298 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
|
299 return d |
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
300 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
301 async def get_schema_dict( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
302 self, client: SatXMPPEntity, service: Optional[jid.JID], nodeIdentifier: str |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
303 ) -> dict: |
3473
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
304 """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
|
305 |
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
306 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
|
307 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
308 schema_form = await self.get_schema_form(client, service, nodeIdentifier) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
309 return xml_tools.data_form_2_data_dict(schema_form) |
3473
cc065c13052c
plugin XEP-0346: new `psSchemaDictGet` to get node schema as a serialisable dict
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
310 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
311 def _get_data_form_items( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
312 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
313 form_ns="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
314 service="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
315 node="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
316 schema="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
317 max_items=10, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
318 item_ids=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
319 sub_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
320 extra="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
321 profile_key=C.PROF_KEY_NONE, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
322 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
323 client = self.host.get_client(profile_key) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
324 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
|
325 if not node: |
3028 | 326 raise exceptions.DataError(_("empty node is not allowed")) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
327 if schema: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
328 schema = generic.parseXml(schema.encode("utf-8")) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
329 else: |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
330 schema = None |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
331 max_items = None if max_items == C.NO_LIMIT else max_items |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
332 extra = self._p.parse_extra(data_format.deserialise(extra)) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
333 d = defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
334 self.get_data_form_items( |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
335 client, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
336 service, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
337 node, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
338 schema, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
339 max_items or None, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
340 item_ids, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
341 sub_id or None, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
342 extra.rsm_request, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
343 extra.extra, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
344 form_ns=form_ns or None, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
345 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
346 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
347 d.addCallback(self._p.trans_items_data) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
348 return d |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
349 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
350 async def get_data_form_items( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
351 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
352 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
353 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
354 nodeIdentifier, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
355 schema=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
356 max_items=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
357 item_ids=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
358 sub_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
359 rsm_request=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
360 extra=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
361 default_node=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
362 form_ns=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
363 filters=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
364 ): |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
365 """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
|
366 |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
367 @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
|
368 if None, it will be retrieved from node |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
369 @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
|
370 @param form_ns (unicode, None): namespace of the form |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
371 None to accept everything, even if form has no namespace |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
372 @param filters(dict, None): same as for xml_tools.data_form_result_2_xmlui |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
373 other parameters as the same as for [get_items] |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
374 @return (list[unicode]): XMLUI of the forms |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
375 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
|
376 it will be skipped |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
377 @raise ValueError: one argument is invalid |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
378 """ |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
379 if not nodeIdentifier: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
380 if not default_node: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
381 raise ValueError( |
3028 | 382 _("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
|
383 ) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
384 nodeIdentifier = default_node |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
385 submitted_ns = self.get_submitted_ns(nodeIdentifier) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
386 # we need the initial form to get options of fields when suitable |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
387 schema_form = await self.get_schema_form( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
388 client, service, nodeIdentifier, schema, form_type="result", copy_form=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
389 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
390 items_data = await self._p.get_items( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
391 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
392 service, |
3457
6791103de47d
plugin XEP-0346: use submitted namespace for node:
Goffi <goffi@goffi.org>
parents:
3452
diff
changeset
|
393 submitted_ns, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
394 max_items, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
395 item_ids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
396 sub_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
397 rsm_request, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
398 extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
399 ) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
400 items, metadata = items_data |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
401 items_xmlui = [] |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
402 for item_elt in items: |
3859
3ef988734869
core: fix calls to `domish.Element.elements`:
Goffi <goffi@goffi.org>
parents:
3615
diff
changeset
|
403 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
|
404 form = data_form.Form.fromElement(x_elt) |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
405 if form_ns and form.formNamespace != form_ns: |
3460 | 406 log.debug( |
407 f"form's namespace ({form.formNamespace!r}) differs from expected" | |
408 f"{form_ns!r}" | |
409 ) | |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
410 continue |
3271
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
411 prepend = [ |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
412 ("label", "id"), |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
413 ("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
|
414 ("label", "publisher"), |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
415 ] |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
416 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
417 publisher = jid.JID(item_elt["publisher"]) |
3271
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
418 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
|
419 pass |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
420 else: |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3254
diff
changeset
|
421 prepend.append(("jid", publisher, "publisher")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
422 xmlui = xml_tools.data_form_result_2_xmlui( |
2381
72c30e73a9a5
plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents:
2378
diff
changeset
|
423 form, |
72c30e73a9a5
plugin schema: use new "prepend" argument to put "id" widget first.
Goffi <goffi@goffi.org>
parents:
2378
diff
changeset
|
424 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
|
425 # 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
|
426 # 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
|
427 prepend=prepend, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
428 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
|
429 read_only=False, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
430 ) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
431 items_xmlui.append(xmlui) |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
432 break |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
433 return (items_xmlui, metadata) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
434 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
435 def _send_data_form_item( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
436 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
437 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
438 nodeIdentifier, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
439 values, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
440 schema=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
441 item_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
442 extra=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
443 profile_key=C.PROF_KEY_NONE, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
444 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
445 client = self.host.get_client(profile_key) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
446 service = None if not service else jid.JID(service) |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
447 if schema: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
448 schema = generic.parseXml(schema.encode("utf-8")) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
449 else: |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
450 schema = None |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
451 d = defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
452 self.send_data_form_item( |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
453 client, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
454 service, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
455 nodeIdentifier, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
456 values, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
457 schema, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
458 item_id or None, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
459 extra, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
460 deserialise=True, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
461 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
462 ) |
3028 | 463 d.addCallback(lambda ret: ret or "") |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
464 return d |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
465 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
466 async def send_data_form_item( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
467 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
468 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
469 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
470 nodeIdentifier, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
471 values, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
472 schema=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
473 item_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
474 extra=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
475 deserialise=False, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
476 ): |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
477 """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
|
478 |
2774
95321f233387
plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
479 @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
|
480 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
|
481 @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
|
482 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
|
483 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
|
484 @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
|
485 deserialized according to expected type. |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
486 This is done in this method and not directly in _send_data_form_item because we |
2774
95321f233387
plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
487 need to know the data type which is in the form, not availablable in |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
488 _send_data_form_item |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
489 other parameters as the same as for [self._p.send_item] |
2420
03da3ef5fb5b
plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents:
2398
diff
changeset
|
490 @return (unicode): id of the created item |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
491 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
492 form = await self.get_schema_form( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
493 client, service, nodeIdentifier, schema, form_type="submit" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
494 ) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
495 |
3028 | 496 for name, values_list in values.items(): |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
497 try: |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
498 field = form.fields[name] |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
499 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
500 log.warning( |
3028 | 501 _("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
|
502 ) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
503 continue |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
504 if isinstance(values_list, str) or not isinstance(values_list, Iterable): |
2371
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
505 values_list = [values_list] |
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
506 if deserialise: |
3028 | 507 if field.fieldType == "boolean": |
2371
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
508 values_list = [C.bool(v) for v in values_list] |
3028 | 509 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
|
510 # 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
|
511 values_list = list( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
512 itertools.chain(*[v.splitlines() for v in values_list]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
513 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
514 elif xml_tools.is_xhtml_field(field): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
515 values_list = [ |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
516 generic.parseXml(v.encode("utf-8")) for v in values_list |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
517 ] |
3028 | 518 elif "jid" in (field.fieldType or ""): |
2371
2268df8c99bf
plugin pubsub schema: values handling:
Goffi <goffi@goffi.org>
parents:
2363
diff
changeset
|
519 values_list = [jid.JID(v) for v in values_list] |
3028 | 520 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
|
521 # 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
|
522 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
|
523 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
|
524 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
|
525 # 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
|
526 values_list = field.values |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
527 elif field.ext_type == "xml": |
3040 | 528 # 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
|
529 # have actual XML/XHTML, or text to escape |
3040 | 530 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
|
531 if isinstance(value, domish.Element): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
532 if field.value and ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
533 value.name != field.value.name or value.uri != field.value.uri |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
534 ): |
3071
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
535 # 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
|
536 # to wrap the current value |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
537 wrapper_elt = domish.Element( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
538 (field.value.uri, field.value.name) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
539 ) |
3071
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
540 wrapper_elt.addChild(value) |
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
541 values_list[idx] = wrapper_elt |
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
542 else: |
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
543 # we have to convert the value to a domish.Element |
3040 | 544 if field.value and field.value.uri == C.NS_XHTML: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
545 div_elt = domish.Element((C.NS_XHTML, "div")) |
3040 | 546 div_elt.addContent(str(value)) |
547 values_list[idx] = div_elt | |
3071
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
548 else: |
68d423f4fb55
plugin pubsub schema: fixed XML field handling in `sendDataFormItem`
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
549 # 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
|
550 raise NotImplementedError |
3040 | 551 |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
552 field.values = values_list |
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
553 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
554 return await self._p.send_item( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
555 client, service, nodeIdentifier, form.toElement(), item_id, extra |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
556 ) |
2363
41c7717b52cd
plugin PubSub Schema: schema helper methods:
Goffi <goffi@goffi.org>
parents:
2350
diff
changeset
|
557 |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
558 ## filters ## |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
559 # filters useful for data form to XMLUI conversion # |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
560 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
561 def value_or_publisher_filter(self, form_xmlui, widget_type, args, kwargs): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
562 """Replace missing value by publisher's user part""" |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
563 if not args[0]: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
564 # 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
|
565 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
566 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
|
567 except (KeyError, RuntimeError): |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
568 pass |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
569 else: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
570 args[0] = publisher.user.capitalize() |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
571 return widget_type, args, kwargs |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
572 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
573 def textbox_2_list_filter(self, form_xmlui, widget_type, args, kwargs): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
574 """Split lines of a textbox in a list |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
575 |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
576 main use case is using a textbox for labels |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
577 """ |
3028 | 578 if widget_type != "textbox": |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
579 return widget_type, args, kwargs |
3028 | 580 widget_type = "list" |
581 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
|
582 kwargs = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
583 "options": options, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
584 "name": kwargs.get("name"), |
3028 | 585 "styles": ("noselect", "extensible", "reducible"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
586 } |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
587 return widget_type, args, kwargs |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
588 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
589 def date_filter(self, form_xmlui, widget_type, args, kwargs): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
590 """Convert a string with a date to a unix timestamp""" |
3028 | 591 if widget_type != "string" or not args[0]: |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
592 return widget_type, args, kwargs |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
593 # we convert XMPP date to timestamp |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
594 try: |
3028 | 595 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
|
596 except Exception as e: |
3028 | 597 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
|
598 return widget_type, args, kwargs |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
599 |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
600 ## Helper methods ## |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
601 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
602 def prepare_bridge_get(self, service, node, max_items, sub_id, extra, profile_key): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
603 """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
|
604 |
2774
95321f233387
plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
605 @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
|
606 internal methods |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
607 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
608 client = self.host.get_client(profile_key) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
609 service = jid.JID(service) if service else None |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
610 if not node: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
611 node = None |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
612 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
|
613 if not sub_id: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
614 sub_id = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
615 extra = self._p.parse_extra(extra) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
616 |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
617 return client, service, node, max_items, extra, sub_id |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
618 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
619 def _get( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
620 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
621 service="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
622 node="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
623 max_items=10, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
624 item_ids=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
625 sub_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
626 extra="", |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
627 default_node=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
628 form_ns=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
629 filters=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
630 profile_key=C.PROF_KEY_NONE, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
631 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
632 """bridge method to retrieve data from node with schema |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
633 |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
634 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
|
635 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
|
636 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
|
637 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
|
638 in final UI. |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
639 """ |
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
|
640 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
|
641 filters = {} |
3586
5f65f4e9f8cb
plugin XEP-0060: getItems extra is now serialised dict
Goffi <goffi@goffi.org>
parents:
3584
diff
changeset
|
642 extra = data_format.deserialise(extra) |
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
|
643 # 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
|
644 # have to modify them |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
645 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
|
646 filters = filters.copy() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
647 filters["labels"] = self.textbox_2_list_filter |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
648 client, service, node, max_items, extra, sub_id = self.prepare_bridge_get( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
649 service, node, max_items, sub_id, extra, profile_key |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
650 ) |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
651 d = defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
652 self.get_data_form_items( |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
653 client, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
654 service, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
655 node or None, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
656 max_items=max_items, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
657 item_ids=item_ids, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
658 sub_id=sub_id, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
659 rsm_request=extra.rsm_request, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
660 extra=extra.extra, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
661 default_node=default_node, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
662 form_ns=form_ns, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
663 filters=filters, |
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
664 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
665 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
666 d.addCallback(self._p.trans_items_data) |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3271
diff
changeset
|
667 d.addCallback(lambda data: data_format.serialise(data)) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
668 return d |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
669 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
670 def prepare_bridge_set(self, service, node, schema, item_id, extra, profile_key): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
671 """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
|
672 |
2774
95321f233387
plugin pubsub schema: code formatting minor update
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
673 @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
|
674 internal methods |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
675 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
676 client = self.host.get_client(profile_key) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
677 service = None if not service else jid.JID(service) |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
678 if schema: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
679 schema = generic.parseXml(schema.encode("utf-8")) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
680 else: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
681 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
|
682 extra = data_format.deserialise(extra) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
683 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
|
684 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
685 async def copy_missing_values(self, client, service, node, item_id, form_ns, 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
|
686 """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
|
687 |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
688 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
|
689 be filled |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
690 @param service: same as for [XEP_0060.get_items] |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
691 @param node: same as for [XEP_0060.get_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
|
692 @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
|
693 @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
|
694 @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
|
695 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
|
696 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
|
697 """ |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
698 try: |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
699 # we get previous item |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
700 items_data = await self._p.get_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
|
701 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
|
702 ) |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
703 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
|
704 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
|
705 log.warning( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
706 _("Can't get previous item, update ignored: {reason}").format(reason=e) |
2959
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
707 ) |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
708 else: |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
709 # 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
|
710 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
|
711 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
|
712 log.warning( |
3040 | 713 _("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
|
714 ) |
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
715 else: |
3028 | 716 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
|
717 if name not in values: |
3028 | 718 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
|
719 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
720 def _set( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
721 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
722 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
723 node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
724 values, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
725 schema=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
726 item_id=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
727 extra=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
728 default_node=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
729 form_ns=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
730 fill_author=True, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
731 profile_key=C.PROF_KEY_NONE, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
732 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
733 """bridge method to set item in node with schema |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
734 |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
735 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
|
736 when adding *Set methods |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
737 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
738 client, service, node, schema, item_id, extra = self.prepare_bridge_set( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
739 service, node, schema, item_id, extra |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
740 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
741 d = defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
742 self.set( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
743 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
744 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
745 node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
746 values, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
747 schema, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
748 item_id, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
749 extra, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
750 deserialise=True, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
751 form_ns=form_ns, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
752 default_node=default_node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
753 fill_author=fill_author, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
754 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
755 ) |
3028 | 756 d.addCallback(lambda ret: ret or "") |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
757 return d |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
758 |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3271
diff
changeset
|
759 async def set( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
760 self, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
761 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
762 service, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
763 node, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
764 values, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
765 schema, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
766 item_id, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
767 extra, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
768 deserialise, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
769 form_ns, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
770 default_node=None, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
771 fill_author=True, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
772 ): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
773 """Set an item in a node with a schema |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
774 |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
775 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
|
776 @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
|
777 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
|
778 'created' and 'updated' will be forced to current time: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
779 - '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
|
780 - 'updated' is set everytime |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
781 @param extra(dict, None): same as for [XEP-0060.send_item] with additional keys: |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
782 - 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
|
783 if True, item_id must be set |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
784 @param form_ns (unicode, None): namespace of the form |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
785 needed when an update is done |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
786 @param default_node(unicode, None): value to use if node is not set |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
787 other arguments are same as for [self._s.send_data_form_item] |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
788 @return (unicode): id of the created item |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
789 """ |
2959
989b622faff6
plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
Goffi <goffi@goffi.org>
parents:
2958
diff
changeset
|
790 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
|
791 extra = {} |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
792 if not node: |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
793 if default_node is None: |
3028 | 794 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
|
795 node = default_node |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
796 node = self.get_submitted_ns(node) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
797 now = utils.xmpp_date() |
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
798 if not item_id: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
799 values["created"] = now |
3028 | 800 elif extra.get("update", False): |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
801 if item_id is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
802 raise exceptions.DataError( |
3028 | 803 _('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
|
804 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
805 await self.copy_missing_values( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
806 client, service, node, item_id, form_ns, values |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
807 ) |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
808 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
809 values["updated"] = now |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
810 if fill_author: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
811 if not values.get("author"): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
812 id_data = await self._i.get_identity(client, None, ["nicknames"]) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
813 values["author"] = id_data["nicknames"][0] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
814 if not values.get("author_jid"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
815 values["author_jid"] = client.jid.full() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3859
diff
changeset
|
816 item_id = await self.send_data_form_item( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
817 client, service, node, values, schema, item_id, extra, deserialise |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
818 ) |
3309
71761e9fb984
plugins tickets, merge-requests: `ticketsGet` and `mergeRequestsGet` serialisation:
Goffi <goffi@goffi.org>
parents:
3271
diff
changeset
|
819 return item_id |
2471
544c4d2fec45
plugins schema, merge_requests, tickets*: factorisation
Goffi <goffi@goffi.org>
parents:
2426
diff
changeset
|
820 |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
821 |
3028 | 822 @implementer(iwokkel.IDisco) |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
823 class SchemaHandler(XMPPHandler): |
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
824 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
825 def getDiscoInfo(self, requestor, service, nodeIdentifier=""): |
3452
bb0225aaf4e6
plugin XEP-0346: "Form Discovery and Publishing" implementation:
Goffi <goffi@goffi.org>
parents:
3309
diff
changeset
|
826 return [disco.DiscoFeature(NS_FDP)] |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
827 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2612
diff
changeset
|
828 def getDiscoItems(self, requestor, service, nodeIdentifier=""): |
2350
388226e9c3ff
plugin schema: PubSub node schema, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
829 return [] |