core (xmlui): new "prepend" argument for dataForm2Widgets and dataFormResult2XMLUI:

This argument allow to create widgets which will be put first in LabelContainer of generated XMLUI.
An iterable of *args for XMLUI.addWidget must be used.
This commit is contained in:
Goffi 2017-10-16 07:36:03 +02:00
commit b8839b97e4

View file

@ -102,12 +102,15 @@ def _dataFormField2XMLUIData(field, read_only=False):
return widget_type, widget_args, widget_kwargs
def dataForm2Widgets(form_ui, form, read_only=False):
def dataForm2Widgets(form_ui, form, read_only=False, prepend=None):
"""Complete an existing XMLUI with widget converted from XEP-0004 data forms.
@param form_ui (XMLUI): XMLUI instance
@param form (data_form.Form): Wokkel's implementation of data form
@param read_only (bool): if True and it makes sense, create a read only input widget
@param prepend(iterable, None): widgets to prepend to main LabelContainer
if not None, must be an iterable of *args for addWidget. Those widgets will
be added first to the container.
@return: the completed XMLUI instance
"""
if form.instructions:
@ -115,6 +118,10 @@ def dataForm2Widgets(form_ui, form, read_only=False):
form_ui.changeContainer("label")
if prepend is not None:
for widget_args in prepend:
form_ui.addWidget(*widget_args)
for field in form.fieldList:
widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field, read_only)
label = field.label or field.var
@ -228,13 +235,14 @@ def dataFormEltResult2XMLUI(form_elt, session_id=None):
dataForm2Widgets(xml_ui, parsed_form, read_only=True)
return xml_ui
def dataFormResult2XMLUI(result_form, base_form, session_id=None):
def dataFormResult2XMLUI(result_form, base_form, session_id=None, prepend=None):
"""Convert data form result to SàT XMLUI.
@param result_form (data_form.Form): result form to convert
@param base_form (data_form.Form): initial form (i.e. of form type "form")
this one is necessary to reconstruct options when needed (e.g. list elements)
@param session_id (unicode): session id to return with the data
@param prepend: same as for [dataForm2Widgets]
@return: XMLUI instance
"""
form = deepcopy(result_form)
@ -245,7 +253,7 @@ def dataFormResult2XMLUI(result_form, base_form, session_id=None):
continue
field.options = base_field.options
xml_ui = XMLUI("window", "vertical", session_id=session_id)
dataForm2Widgets(xml_ui, form, read_only=True)
dataForm2Widgets(xml_ui, form, read_only=True, prepend=prepend)
return xml_ui