comparison sat/plugins/plugin_misc_lists.py @ 3472:e12e9e1535d3

tools (xml_tools): new `dataForm2dataDict` and `dataDict2dataForm`: those methods convert a serialisable dict format to `wokkel.data_form.Form` and vice versa
author Goffi <goffi@goffi.org>
date Sat, 27 Feb 2021 18:36:38 +0100
parents d897597cfa94
children be6d91572633
comparison
equal deleted inserted replaced
3471:d897597cfa94 3472:e12e9e1535d3
18 import shortuuid 18 import shortuuid
19 from typing import List, Tuple, Optional 19 from typing import List, Tuple, Optional
20 from twisted.internet import defer 20 from twisted.internet import defer
21 from twisted.words.xish import domish 21 from twisted.words.xish import domish
22 from twisted.words.protocols.jabber import jid 22 from twisted.words.protocols.jabber import jid
23 from wokkel import data_form
24 from sat.core.i18n import _, D_ 23 from sat.core.i18n import _, D_
25 from sat.core.xmpp import SatXMPPEntity 24 from sat.core.xmpp import SatXMPPEntity
26 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
26 from sat.tools import xml_tools
27 from sat.tools.common import uri 27 from sat.tools.common import uri
28 from sat.tools.common import data_format 28 from sat.tools.common import data_format
29 from sat.core.log import getLogger 29 from sat.core.log import getLogger
30 30
31 log = getLogger(__name__) 31 log = getLogger(__name__)
453 ) -> Tuple[jid.JID, str]: 453 ) -> Tuple[jid.JID, str]:
454 """Create a list from a template""" 454 """Create a list from a template"""
455 name = name.strip() 455 name = name.strip()
456 if not name: 456 if not name:
457 name = shortuuid.uuid() 457 name = shortuuid.uuid()
458 template = TEMPLATES[template_id] 458 fields = TEMPLATES[template_id]["fields"].copy()
459 459 fields.insert(
460 fields = [ 460 0,
461 data_form.Field(fieldType="hidden", var=NS_TICKETS_TYPE, value=template_id) 461 {"type": "hidden", "name": NS_TICKETS_TYPE, "value": template_id}
462 ] 462 )
463 for field_data in template['fields']: 463 schema = xml_tools.dataDict2dataForm(
464 field_type = field_data.get('type', 'text-single') 464 {"namespace": APP_NS_TICKETS, "fields": fields}
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() 465 ).toElement()
491 466
492 service = client.jid.userhostJID() 467 service = client.jid.userhostJID()
493 node = self._s.getSubmittedNS(f"{APP_NS_TICKETS}_{name}") 468 node = self._s.getSubmittedNS(f"{APP_NS_TICKETS}_{name}")
494 options = { 469 options = {