Mercurial > libervia-backend
annotate sat/tools/common/template_xmlui.py @ 3515:2dce411c2647
plugin misc forums: use rich content in createTopic
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 01 May 2021 18:23:42 +0200 |
parents | be6d91572633 |
children | 185601bc0cd3 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2362 | 3 |
4 # SAT: a jabber client | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2362 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ template XMLUI parsing | |
21 | |
22 XMLUI classes from this modules can then be iterated to create the template | |
23 """ | |
24 | |
3271
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
25 from functools import partial |
2362 | 26 from sat.core.log import getLogger |
27 from sat_frontends.tools import xmlui | |
3271
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
28 from sat_frontends.tools import jid |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
29 try: |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
30 from jinja2 import Markup as safe |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
31 except ImportError: |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
32 # Safe marks XHTML values as usable as it. |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
33 # If jinja2 is not there, we can use a simple lamba |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
34 safe = lambda x: x |
2362 | 35 |
36 | |
3271
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
37 log = getLogger(__name__) |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
38 |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
39 |
2362 | 40 ## Widgets ## |
41 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 |
2362 | 43 class Widget(object): |
3028 | 44 category = "widget" |
2362 | 45 type = None |
46 enabled = True | |
47 read_only = True | |
48 | |
49 def __init__(self, xmlui_parent): | |
50 self.xmlui_parent = xmlui_parent | |
51 | |
52 @property | |
53 def name(self): | |
54 return self._xmlui_name | |
55 | |
56 | |
57 class ValueWidget(Widget): | |
58 def __init__(self, xmlui_parent, value): | |
59 super(ValueWidget, self).__init__(xmlui_parent) | |
60 self.value = value | |
61 | |
2367
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
62 @property |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
63 def values(self): |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
64 return [self.value] |
9878635586f3
template (xmlui): added values property to be able to use always values even when there is only one value
Goffi <goffi@goffi.org>
parents:
2362
diff
changeset
|
65 |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
66 @property |
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
67 def labels(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 # helper property, there is not label for ValueWidget |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
69 # but using labels make rendering more easy (one single method to call) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 # values are actually returned |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
71 return [self.value] |
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
72 |
2362 | 73 |
74 class InputWidget(ValueWidget): | |
75 def __init__(self, xmlui_parent, value, read_only=False): | |
76 super(InputWidget, self).__init__(xmlui_parent, value) | |
77 self.read_only = read_only | |
78 | |
79 | |
80 class OptionsWidget(Widget): | |
81 def __init__(self, xmlui_parent, options, selected, style): | |
82 super(OptionsWidget, self).__init__(xmlui_parent) | |
83 self.options = options | |
84 self.selected = selected | |
85 self.style = style | |
86 | |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
87 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
88 def values(self): |
2455
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
89 for value, label in self.items: |
7b02372f8734
template (xmlui): added labels property in ValueWidget (actually return values) and values property in OptionsWidget
Goffi <goffi@goffi.org>
parents:
2435
diff
changeset
|
90 yield value |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
91 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
92 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
93 def labels(self): |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
94 """return only labels from self.items""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
95 for value, label in self.items: |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
96 yield label |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
97 |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
98 @property |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
99 def items(self): |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
100 """return suitable items, according to style""" |
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
101 no_select = self.no_select |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 for value, label in self.options: |
2400
8253ea069781
template(XMLUI): added items property to ListWidget:
Goffi <goffi@goffi.org>
parents:
2397
diff
changeset
|
103 if no_select or value in self.selected: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 yield value, label |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
105 |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
106 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
107 def inline(self): |
3028 | 108 return "inline" in self.style |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
109 |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
110 @property |
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
111 def no_select(self): |
3028 | 112 return "noselect" in self.style |
2397
7fff98d64ab5
core (XMLUI), template(XMLUI): added flags to ListWidget:
Goffi <goffi@goffi.org>
parents:
2379
diff
changeset
|
113 |
2362 | 114 |
115 class EmptyWidget(xmlui.EmptyWidget, Widget): | |
116 def __init__(self, _xmlui_parent): | |
117 Widget.__init__(self) | |
118 | |
119 | |
120 class TextWidget(xmlui.TextWidget, ValueWidget): | |
3028 | 121 type = "text" |
2362 | 122 |
123 | |
3271
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
124 class JidWidget(xmlui.JidWidget, ValueWidget): |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
125 type = "jid" |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
126 |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
127 def __init__(self, xmlui_parent, value): |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
128 self.value = jid.JID(value) |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
129 |
abca25af06d7
plugin pubsub schema, tools (common/template xmlui): use a JID for publisher:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
130 |
2362 | 131 class LabelWidget(xmlui.LabelWidget, ValueWidget): |
3028 | 132 type = "label" |
2362 | 133 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
134 @property |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
135 def for_name(self): |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
136 try: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
137 return self._xmlui_for_name |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
138 except AttributeError: |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
139 return None |
2362 | 140 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
141 |
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
142 class StringWidget(xmlui.StringWidget, InputWidget): |
3028 | 143 type = "string" |
2362 | 144 |
145 | |
2435
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
146 class JidInputWidget(xmlui.JidInputWidget, StringWidget): |
3028 | 147 type = "jid" |
2435
49884c579266
template (XMLUI): added JidInputWidget
Goffi <goffi@goffi.org>
parents:
2419
diff
changeset
|
148 |
2379
42a54cbc1872
template (xmlui): new properties + inheritance fix:
Goffi <goffi@goffi.org>
parents:
2367
diff
changeset
|
149 class TextBoxWidget(xmlui.TextWidget, InputWidget): |
3028 | 150 type = "textbox" |
2362 | 151 |
152 | |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
153 class XHTMLBoxWidget(xmlui.XHTMLBoxWidget, InputWidget): |
3028 | 154 type = "xhtmlbox" |
2783
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
155 |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
156 def __init__(self, xmlui_parent, value, read_only=False): |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
157 # XXX: XHTMLBoxWidget value must be cleaned (harmful XHTML must be removed) |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
158 # This is normally done in the backend, the frontends should not need to |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
159 # worry about it. |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
160 super(XHTMLBoxWidget, self).__init__( |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
161 xmlui_parent=xmlui_parent, value=safe(value), read_only=read_only) |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
162 |
442ab697f831
frontends, jp, templates: added XHTMLBox widget:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
163 |
2362 | 164 class ListWidget(xmlui.ListWidget, OptionsWidget): |
3028 | 165 type = "list" |
2362 | 166 |
167 | |
168 ## Containers ## | |
169 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 |
2362 | 171 class Container(object): |
3028 | 172 category = "container" |
2362 | 173 type = None |
174 | |
175 def __init__(self, xmlui_parent): | |
176 self.xmlui_parent = xmlui_parent | |
177 self.children = [] | |
178 | |
179 def __iter__(self): | |
180 return iter(self.children) | |
181 | |
182 def _xmluiAppend(self, widget): | |
183 self.children.append(widget) | |
184 | |
2419
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
185 def _xmluiRemove(self, widget): |
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
186 self.children.remove(widget) |
c38c54c47e16
frontends (xmlui): added an attribute to ignore some widgets (and their label) in create
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
187 |
2362 | 188 |
189 class VerticalContainer(xmlui.VerticalContainer, Container): | |
3028 | 190 type = "vertical" |
2362 | 191 |
192 | |
193 class PairsContainer(xmlui.PairsContainer, Container): | |
3028 | 194 type = "pairs" |
2362 | 195 |
196 | |
197 class LabelContainer(xmlui.PairsContainer, Container): | |
3028 | 198 type = "label" |
2362 | 199 |
200 | |
201 ## Factory ## | |
202 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 |
2362 | 204 class WidgetFactory(object): |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
205 |
2362 | 206 def __getattr__(self, attr): |
207 if attr.startswith("create"): | |
208 cls = globals()[attr[6:]] | |
209 return cls | |
210 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 |
2362 | 212 ## Core ## |
213 | |
214 | |
215 class XMLUIPanel(xmlui.XMLUIPanel): | |
216 widget_factory = WidgetFactory() | |
217 | |
218 def show(self, *args, **kwargs): | |
219 raise NotImplementedError | |
220 | |
221 | |
222 class XMLUIDialog(xmlui.XMLUIDialog): | |
223 dialog_factory = WidgetFactory() | |
224 | |
225 def __init__(*args, **kwargs): | |
226 raise NotImplementedError | |
227 | |
228 | |
2669
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
229 create = partial(xmlui.create, class_map={ |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
230 xmlui.CLASS_PANEL: XMLUIPanel, |
bdb8276fd2da
frontends (xmlui): class_map is now an arg of create function:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
231 xmlui.CLASS_DIALOG: XMLUIDialog}) |