Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_lists.py @ 3469:c063cbb2ad6e
plugin lists: register `tickets_type` namespace
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 27 Feb 2021 18:31:47 +0100 |
parents | 54b9cdbeb335 |
children | ca76767185e3 |
rev | line source |
---|---|
3458 | 1 #!/usr/bin/env python3 |
2 | |
3 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) | |
4 | |
5 # This program is free software: you can redistribute it and/or modify | |
6 # it under the terms of the GNU Affero General Public License as published by | |
7 # the Free Software Foundation, either version 3 of the License, or | |
8 # (at your option) any later version. | |
9 | |
10 # This program is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU Affero General Public License for more details. | |
14 | |
15 # You should have received a copy of the GNU Affero General Public License | |
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
3460 | 18 import shortuuid |
3463
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
19 from typing import List, Tuple, Optional |
3460 | 20 from twisted.internet import defer |
21 from twisted.words.xish import domish | |
22 from twisted.words.protocols.jabber import jid | |
23 from wokkel import data_form | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
24 from sat.core.i18n import _, D_ |
3463
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
25 from sat.core.xmpp import SatXMPPEntity |
3458 | 26 from sat.core.constants import Const as C |
27 from sat.tools.common import uri | |
3460 | 28 from sat.tools.common import data_format |
3458 | 29 from sat.core.log import getLogger |
30 | |
31 log = getLogger(__name__) | |
32 | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
33 # XXX: this plugin was formely named "tickets", thus the namespace keeps this |
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
34 # name |
3458 | 35 APP_NS_TICKETS = "org.salut-a-toi.tickets:0" |
3460 | 36 NS_TICKETS_TYPE = "org.salut-a-toi.tickets#type:0" |
3458 | 37 |
38 PLUGIN_INFO = { | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
39 C.PI_NAME: _("Pubsub Lists"), |
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
40 C.PI_IMPORT_NAME: "LISTS", |
3458 | 41 C.PI_TYPE: "EXP", |
42 C.PI_PROTOCOLS: [], | |
3463
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
43 C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0346", "XEP-0277", "IDENTITY", |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
44 "PUBSUB_INVITATION"], |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
45 C.PI_MAIN: "PubsubLists", |
3458 | 46 C.PI_HANDLER: "no", |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
47 C.PI_DESCRIPTION: _("""Pubsub lists management plugin"""), |
3458 | 48 } |
49 | |
3460 | 50 TEMPLATES = { |
51 "todo": { | |
52 "name": D_("TODO List"), | |
53 "icon": "check", | |
54 "fields": [ | |
55 {"name": "title"}, | |
56 {"name": "author"}, | |
57 {"name": "created"}, | |
58 {"name": "updated"}, | |
59 {"name": "time_limit"}, | |
60 {"name": "labels", "type": "text-multi"}, | |
61 { | |
62 "name": "status", | |
63 "label": D_("status"), | |
64 "type": "list-single", | |
65 "options": [ | |
66 { | |
67 "label": D_("to do"), | |
68 "value": "todo" | |
69 }, | |
70 { | |
71 "label": D_("in progress"), | |
72 "value": "in_progress" | |
73 }, | |
74 { | |
75 "label": D_("done"), | |
76 "value": "done" | |
77 }, | |
78 ], | |
79 "value": "todo" | |
80 }, | |
81 { | |
82 "name": "priority", | |
83 "label": D_("priority"), | |
84 "type": "list-single", | |
85 "options": [ | |
86 { | |
87 "label": D_("major"), | |
88 "value": "major" | |
89 }, | |
90 { | |
91 "label": D_("normal"), | |
92 "value": "normal" | |
93 }, | |
94 { | |
95 "label": D_("minor"), | |
96 "value": "minor" | |
97 }, | |
98 ], | |
99 "value": "normal" | |
100 }, | |
101 {"name": "body", "type": "xhtml"}, | |
102 {"name": "comments_uri"}, | |
103 ] | |
104 }, | |
105 "shopping": { | |
106 "name": D_("Shopping List"), | |
107 "icon": "basket", | |
108 "fields": [ | |
109 {"name": "name", "label": D_("name")}, | |
110 {"name": "quantity", "label": D_("quantity")}, | |
111 { | |
112 "name": "status", | |
113 "label": D_("status"), | |
114 "type": "list-single", | |
115 "options": [ | |
116 { | |
117 "label": D_("to buy"), | |
118 "value": "to_buy" | |
119 }, | |
120 { | |
121 "label": D_("bought"), | |
122 "value": "bought" | |
123 }, | |
124 ], | |
125 "value": "to_buy" | |
126 }, | |
127 ] | |
128 }, | |
129 "tickets": { | |
130 "name": D_("Tickets"), | |
131 "icon": "clipboard", | |
132 "fields": [ | |
133 {"name": "title"}, | |
134 {"name": "author"}, | |
135 {"name": "created"}, | |
136 {"name": "updated"}, | |
137 {"name": "labels", "type": "text-multi"}, | |
138 { | |
139 "name": "type", | |
140 "label": D_("type"), | |
141 "type": "list-single", | |
142 "options": [ | |
143 { | |
144 "label": D_("bug"), | |
145 "value": "bug" | |
146 }, | |
147 { | |
148 "label": D_("feature request"), | |
149 "value": "feature" | |
150 }, | |
151 ], | |
152 "value": "bug" | |
153 }, | |
154 { | |
155 "name": "status", | |
156 "label": D_("status"), | |
157 "type": "list-single", | |
158 "options": [ | |
159 { | |
160 "label": D_("queued"), | |
161 "value": "queued" | |
162 }, | |
163 { | |
164 "label": D_("started"), | |
165 "value": "started" | |
166 }, | |
167 { | |
168 "label": D_("review"), | |
169 "value": "review" | |
170 }, | |
171 { | |
172 "label": D_("closed"), | |
173 "value": "closed" | |
174 }, | |
175 ], | |
176 "value": "queued" | |
177 }, | |
178 { | |
179 "name": "priority", | |
180 "label": D_("priority"), | |
181 "type": "list-single", | |
182 "options": [ | |
183 { | |
184 "label": D_("major"), | |
185 "value": "major" | |
186 }, | |
187 { | |
188 "label": D_("normal"), | |
189 "value": "normal" | |
190 }, | |
191 { | |
192 "label": D_("minor"), | |
193 "value": "minor" | |
194 }, | |
195 ], | |
196 "value": "normal" | |
197 }, | |
198 {"name": "body", "type": "xhtml"}, | |
199 {"name": "comments_uri"}, | |
200 ] | |
201 } | |
202 } | |
203 | |
3458 | 204 |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
205 class PubsubLists: |
3458 | 206 |
207 def __init__(self, host): | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
208 log.info(_("Pubsub lists plugin initialization")) |
3458 | 209 self.host = host |
210 self._s = self.host.plugins["XEP-0346"] | |
211 self.namespace = self._s.getSubmittedNS(APP_NS_TICKETS) | |
3461
02a8d227d5bb
plugin misc lists: register app NS for "tickets" NS instead of XEP-0346 submitted one
Goffi <goffi@goffi.org>
parents:
3460
diff
changeset
|
212 host.registerNamespace("tickets", APP_NS_TICKETS) |
3469
c063cbb2ad6e
plugin lists: register `tickets_type` namespace
Goffi <goffi@goffi.org>
parents:
3464
diff
changeset
|
213 host.registerNamespace("tickets_type", NS_TICKETS_TYPE) |
3463
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
214 self.host.plugins["PUBSUB_INVITATION"].register( |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
215 APP_NS_TICKETS, self |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
216 ) |
3458 | 217 self._p = self.host.plugins["XEP-0060"] |
218 self._m = self.host.plugins["XEP-0277"] | |
219 host.bridge.addMethod( | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
220 "listGet", |
3458 | 221 ".plugin", |
222 in_sign="ssiassa{ss}s", | |
223 out_sign="s", | |
224 method=lambda service, node, max_items, items_ids, sub_id, extra, profile_key: | |
225 self._s._get( | |
226 service, | |
227 node, | |
228 max_items, | |
229 items_ids, | |
230 sub_id, | |
231 extra, | |
232 default_node=self.namespace, | |
233 form_ns=APP_NS_TICKETS, | |
234 filters={ | |
235 "author": self._s.valueOrPublisherFilter, | |
236 "created": self._s.dateFilter, | |
237 "updated": self._s.dateFilter, | |
3460 | 238 "time_limit": self._s.dateFilter, |
3458 | 239 }, |
240 profile_key=profile_key), | |
241 async_=True, | |
242 ) | |
243 host.bridge.addMethod( | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
244 "listSet", |
3458 | 245 ".plugin", |
246 in_sign="ssa{sas}ssss", | |
247 out_sign="s", | |
248 method=self._set, | |
249 async_=True, | |
250 ) | |
251 host.bridge.addMethod( | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
252 "listSchemaGet", |
3458 | 253 ".plugin", |
254 in_sign="sss", | |
255 out_sign="s", | |
256 method=lambda service, nodeIdentifier, profile_key: self._s._getUISchema( | |
257 service, nodeIdentifier, default_node=self.namespace, | |
258 profile_key=profile_key), | |
259 async_=True, | |
260 ) | |
3460 | 261 host.bridge.addMethod( |
3464
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
262 "listsList", |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
263 ".plugin", |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
264 in_sign="sss", |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
265 out_sign="s", |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
266 method=self._listsList, |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
267 async_=True, |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
268 ) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
269 host.bridge.addMethod( |
3460 | 270 "listTemplatesNamesGet", |
271 ".plugin", | |
272 in_sign="ss", | |
273 out_sign="s", | |
274 method=self._getTemplatesNames, | |
275 ) | |
276 host.bridge.addMethod( | |
277 "listTemplateGet", | |
278 ".plugin", | |
279 in_sign="sss", | |
280 out_sign="s", | |
281 method=self._getTemplate, | |
282 ) | |
283 host.bridge.addMethod( | |
284 "listTemplateCreate", | |
285 ".plugin", | |
286 in_sign="ssss", | |
287 out_sign="(ss)", | |
288 method=self._createTemplate, | |
289 async_=True, | |
290 ) | |
3458 | 291 |
3463
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
292 async def onInvitationPreflight( |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
293 self, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
294 client: SatXMPPEntity, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
295 namespace: str, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
296 name: str, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
297 extra: dict, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
298 service: jid.JID, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
299 node: str, |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
300 item_id: Optional[str], |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
301 item_elt: domish.Element |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
302 ) -> None: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
303 try: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
304 schema = await self._s.getSchemaForm(client, service, node) |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
305 except Exception as e: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
306 log.warning(f"Can't retrive node schema as {node!r} [{service}]: {e}") |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
307 else: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
308 try: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
309 field_type = schema[NS_TICKETS_TYPE] |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
310 except KeyError: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
311 log.debug("no type found in list schema") |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
312 else: |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
313 list_elt = extra["element"] = domish.Element((APP_NS_TICKETS, "list")) |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
314 list_elt["type"] = field_type |
483bcfeb11c9
plugin misc list: register lists for pubsub invitations
Goffi <goffi@goffi.org>
parents:
3461
diff
changeset
|
315 |
3458 | 316 def _set(self, service, node, values, schema=None, item_id=None, extra='', |
317 profile_key=C.PROF_KEY_NONE): | |
318 client, service, node, schema, item_id, extra = self._s.prepareBridgeSet( | |
319 service, node, schema, item_id, extra, profile_key | |
320 ) | |
321 d = defer.ensureDeferred(self.set( | |
322 client, service, node, values, schema, item_id, extra, deserialise=True | |
323 )) | |
324 d.addCallback(lambda ret: ret or "") | |
325 return d | |
326 | |
3460 | 327 async def set( |
328 self, client, service, node, values, schema=None, item_id=None, extra=None, | |
329 deserialise=False, form_ns=APP_NS_TICKETS | |
330 ): | |
3458 | 331 """Publish a tickets |
332 | |
333 @param node(unicode, None): Pubsub node to use | |
334 None to use default tickets node | |
335 @param values(dict[key(unicode), [iterable[object]|object]]): values of the ticket | |
336 | |
337 if value is not iterable, it will be put in a list | |
338 'created' and 'updated' will be forced to current time: | |
339 - 'created' is set if item_id is None, i.e. if it's a new ticket | |
340 - 'updated' is set everytime | |
341 @param extra(dict, None): same as for [XEP-0060.sendItem] with additional keys: | |
342 - update(bool): if True, get previous item data to merge with current one | |
343 if True, item_id must be None | |
344 other arguments are same as for [self._s.sendDataFormItem] | |
345 @return (unicode): id of the created item | |
346 """ | |
347 if not node: | |
348 node = self.namespace | |
349 | |
350 if not item_id: | |
351 comments_service = await self._m.getCommentsService(client, service) | |
352 | |
353 # we need to use uuid for comments node, because we don't know item id in | |
354 # advance (we don't want to set it ourselves to let the server choose, so we | |
355 # can have a nicer id if serial ids is activated) | |
356 comments_node = self._m.getCommentsNode( | |
357 node + "_" + str(shortuuid.uuid()) | |
358 ) | |
359 options = { | |
360 self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN, | |
361 self._p.OPT_PERSIST_ITEMS: 1, | |
362 self._p.OPT_MAX_ITEMS: -1, | |
363 self._p.OPT_DELIVER_PAYLOADS: 1, | |
364 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, | |
365 self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN, | |
366 } | |
367 await self._p.createNode(client, comments_service, comments_node, options) | |
368 values["comments_uri"] = uri.buildXMPPUri( | |
369 "pubsub", | |
370 subtype="microblog", | |
371 path=comments_service.full(), | |
372 node=comments_node, | |
373 ) | |
374 | |
375 return await self._s.set( | |
376 client, service, node, values, schema, item_id, extra, deserialise, form_ns | |
377 ) | |
3460 | 378 |
3464
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
379 def _listsList(self, service, node, profile): |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
380 service = jid.JID(service) if service else None |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
381 node = node or None |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
382 client = self.host.getClient(profile) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
383 d = defer.ensureDeferred(self.listsList(client, service, node)) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
384 d.addCallback(data_format.serialise) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
385 return d |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
386 |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
387 async def listsList( |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
388 self, client, service: Optional[jid.JID], node: Optional[str]=None |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
389 ) -> List[dict]: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
390 """Retrieve list of pubsub lists registered in personal interests |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
391 |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
392 @return list: list of lists metadata |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
393 """ |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
394 items, metadata = await self.host.plugins['LIST_INTEREST'].listInterests( |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
395 client, service, node, namespace=APP_NS_TICKETS) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
396 lists = [] |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
397 for item in items: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
398 interest_elt = item.interest |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
399 if interest_elt is None: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
400 log.warning(f"invalid interest for {client.profile}: {item.toXml}") |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
401 continue |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
402 if interest_elt.getAttribute("namespace") != APP_NS_TICKETS: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
403 continue |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
404 pubsub_elt = interest_elt.pubsub |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
405 list_data = { |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
406 "id": item["id"], |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
407 "name": interest_elt["name"], |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
408 "service": pubsub_elt["service"], |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
409 "node": pubsub_elt["node"], |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
410 "creator": C.bool(pubsub_elt.getAttribute("creator", C.BOOL_FALSE)), |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
411 } |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
412 try: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
413 list_elt = next(pubsub_elt.elements(APP_NS_TICKETS, "list")) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
414 except StopIteration: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
415 pass |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
416 else: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
417 list_type = list_data["type"] = list_elt["type"] |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
418 if list_type in TEMPLATES: |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
419 list_data["icon_name"] = TEMPLATES[list_type]["icon"] |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
420 lists.append(list_data) |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
421 |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
422 return lists |
54b9cdbeb335
plugin lists: new `listsList` method to retrieve lists from personal interests
Goffi <goffi@goffi.org>
parents:
3463
diff
changeset
|
423 |
3460 | 424 def _getTemplatesNames(self, language, profile): |
425 client = self.host.getClient(profile) | |
426 return data_format.serialise(self.getTemplatesNames(client, language)) | |
427 | |
428 def getTemplatesNames(self, client, language: str) -> list: | |
429 """Retrieve well known list templates""" | |
430 | |
431 templates = [{"id": tpl_id, "name": d["name"], "icon": d["icon"]} | |
432 for tpl_id, d in TEMPLATES.items()] | |
433 return templates | |
434 | |
435 def _getTemplate(self, name, language, profile): | |
436 client = self.host.getClient(profile) | |
437 return data_format.serialise(self.getTemplate(client, name, language)) | |
438 | |
439 def getTemplate(self, client, name: str, language: str) -> dict: | |
440 """Retrieve a well known template""" | |
441 return TEMPLATES[name] | |
442 | |
443 def _createTemplate(self, template_id, name, access_model, profile): | |
444 client = self.host.getClient(profile) | |
445 d = defer.ensureDeferred(self.createTemplate( | |
446 client, template_id, name, access_model | |
447 )) | |
448 d.addCallback(lambda node_data: (node_data[0].full(), node_data[1])) | |
449 return d | |
450 | |
451 async def createTemplate( | |
452 self, client, template_id: str, name: str, access_model: str | |
453 ) -> Tuple[jid.JID, str]: | |
454 """Create a list from a template""" | |
455 name = name.strip() | |
456 if not name: | |
457 name = shortuuid.uuid() | |
458 template = TEMPLATES[template_id] | |
459 | |
460 fields = [ | |
461 data_form.Field(fieldType="hidden", var=NS_TICKETS_TYPE, value=template_id) | |
462 ] | |
463 for field_data in template['fields']: | |
464 field_type = field_data.get('type', 'text-single') | |
465 kwargs = { | |
466 "fieldType": field_type, | |
467 "var": field_data["name"], | |
468 "label": field_data.get('label'), | |
469 "value": field_data.get("value"), | |
470 } | |
471 if field_type == "xhtml": | |
472 kwargs.update({ | |
473 "fieldType": None, | |
474 "ext_type": "xml", | |
475 }) | |
476 if kwargs["value"] is None: | |
477 kwargs["value"] = domish.Element((C.NS_XHTML, "div")) | |
478 elif "options" in field_data: | |
479 kwargs["options"] = [ | |
480 data_form.Option(o["value"], o.get("label")) | |
481 for o in field_data["options"] | |
482 ] | |
483 field = data_form.Field(**kwargs) | |
484 fields.append(field) | |
485 | |
486 schema = data_form.Form( | |
487 "form", | |
488 formNamespace=APP_NS_TICKETS, | |
489 fields=fields | |
490 ).toElement() | |
491 | |
492 service = client.jid.userhostJID() | |
493 node = self._s.getSubmittedNS(f"{APP_NS_TICKETS}_{name}") | |
494 options = { | |
495 self._p.OPT_ACCESS_MODEL: access_model | |
496 } | |
497 await self._p.createNode(client, service, node, options) | |
498 await self._s.setSchema(client, service, node, schema) | |
499 list_elt = domish.Element((APP_NS_TICKETS, "list")) | |
500 list_elt["type"] = template_id | |
501 try: | |
502 await self.host.plugins['LIST_INTEREST'].registerPubsub( | |
503 client, APP_NS_TICKETS, service, node, creator=True, | |
504 name=name, element=list_elt) | |
505 except Exception as e: | |
506 log.warning(f"Can't add list to interests: {e}") | |
507 return service, node |