comparison sat/tools/xml_tools.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 80f8635dc66e
children be6d91572633
comparison
equal deleted inserted replaced
3471:d897597cfa94 3472:e12e9e1535d3
190 @param read_only (bool): if True and it makes sense, create a read only input widget 190 @param read_only (bool): if True and it makes sense, create a read only input widget
191 @return: XMLUI instance 191 @return: XMLUI instance
192 """ 192 """
193 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id) 193 form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
194 return dataForm2Widgets(form_ui, form, read_only=read_only) 194 return dataForm2Widgets(form_ui, form, read_only=read_only)
195
196
197 def dataForm2dataDict(form: data_form.Form) -> dict:
198 """Convert data form to a simple dict, easily serialisable
199
200 see dataDict2dataForm for a description of the format
201 """
202 fields = []
203 data_dict = {
204 "fields": fields
205 }
206 if form.formNamespace:
207 data_dict["namespace"] = form.formNamespace
208 for form_field in form.fieldList:
209 field = {"type": form_field.fieldType}
210 fields.append(field)
211 for src_name, dest_name in (
212 ('var', 'name'),
213 ('label', 'label'),
214 ('value', 'value'),
215 ):
216 value = getattr(form_field, src_name, None)
217 if value:
218 field[dest_name] = value
219 if form_field.options:
220 options = field["options"] = []
221 for form_opt in form_field.options:
222 opt = {"value": form_opt.value}
223 if form_opt.label:
224 opt["label"] = form_opt.label
225 options.append(opt)
226
227 if form_field.fieldType is None and form_field.ext_type == "xml":
228 if isinstance(form_field.value, domish.Element):
229 if ((form_field.value.uri == C.NS_XHTML
230 and form_field.value.name == "div")):
231 field["type"] = "xhtml"
232 if form_field.value.children:
233 log.warning(
234 "children are not managed for XHTML fields: "
235 f"{form_field.value.toXml()}"
236 )
237 return data_dict
238
239
240 def dataDict2dataForm(data_dict):
241 """Convert serialisable dict of data to a data form
242
243 The format of the dict is as follow:
244 - an optional "namespace" key with form namespace
245 - a mandatory "fields" key with list of fields as follow:
246 - "type" is mostly the same as data_form.Field.fieldType
247 - "var", "label", and "value" follow same attribude in data_form.Field
248 - "xhtml" is used for "xml" fields with child in the C.NS_XHTML namespace
249 - "options" are list of dict with optional "label" and mandatory "value"
250 following suitable attributes from data_form.Option
251 """
252 # TODO: describe format
253 fields = []
254 for field_data in data_dict["fields"]:
255 field_type = field_data.get('type', 'text-single')
256 kwargs = {
257 "fieldType": field_type,
258 "var": field_data["name"],
259 "label": field_data.get('label'),
260 "value": field_data.get("value"),
261 }
262 if field_type == "xhtml":
263 kwargs.update({
264 "fieldType": None,
265 "ext_type": "xml",
266 })
267 if kwargs["value"] is None:
268 kwargs["value"] = domish.Element((C.NS_XHTML, "div"))
269 elif "options" in field_data:
270 kwargs["options"] = [
271 data_form.Option(o["value"], o.get("label"))
272 for o in field_data["options"]
273 ]
274 field = data_form.Field(**kwargs)
275 fields.append(field)
276 return data_form.Form(
277 "form",
278 formNamespace=data_dict.get("namespace"),
279 fields=fields
280 )
195 281
196 282
197 def dataFormEltResult2XMLUIData(form_xml): 283 def dataFormEltResult2XMLUIData(form_xml):
198 """Parse a data form result (not parsed by Wokkel's XEP-0004 implementation). 284 """Parse a data form result (not parsed by Wokkel's XEP-0004 implementation).
199 285