core (XMLUI): created a read_only attribute for InputWidget + new JidInputWidget

This commit is contained in:
Goffi 2014-06-25 14:01:55 +02:00
commit 2cc6d6549f

View file

@ -34,10 +34,11 @@ SAT_PARAM_SEPARATOR = "_XMLUI_PARAM_" # used to have unique elements names
# Helper functions
def _dataFormField2XMLUIData(field):
def _dataFormField2XMLUIData(field, read_only=False):
""" Get data needed to create an XMLUI's Widget from Wokkel's data_form's Field
field can be modified (if it's fixed and it has no value)
@param field: data_form.Field (it uses field.value, field.fieldType, field.label and field.var)
@param read_only: if True and it make sens, create a read only input widget
@return: widget_type, widget_args, widget_kwargs
"""
@ -55,15 +56,19 @@ def _dataFormField2XMLUIData(field):
widget_args[0] = field.value
elif field.fieldType == 'text-single':
widget_type = "string"
widget_kwargs['read_only'] = read_only
elif field.fieldType == 'text-multi':
widget_type = "textbox"
widget_args[0] = u'\n'.join(field.values)
widget_kwargs['read_only'] = read_only
elif field.fieldType == 'text-private':
widget_type = "password"
widget_kwargs['read_only'] = read_only
elif field.fieldType == 'boolean':
widget_type = "bool"
if widget_args[0] is None:
widget_args[0] = 'false'
widget_kwargs['read_only'] = read_only
elif field.fieldType == 'list-single':
widget_type = "list"
widget_kwargs["options"] = [(option.value, option.label or option.value) for option in field.options]
@ -72,13 +77,14 @@ def _dataFormField2XMLUIData(field):
else:
log.error(u"FIXME FIXME FIXME: Type [%s] is not managed yet by SàT" % field.fieldType)
widget_type = "string"
widget_kwargs['read_only'] = read_only
if field.var:
widget_kwargs["name"] = field.var
return widget_type, widget_args, widget_kwargs
def dataForm2Widgets(form_ui, form):
def dataForm2Widgets(form_ui, form, read_only=False):
"""Complete an existing XMLUI with widget converted frot XEP-0004 data forms
@param form_ui: XMLUI instance
@ -91,7 +97,7 @@ def dataForm2Widgets(form_ui, form):
form_ui.changeContainer("pairs")
for field in form.fieldList:
widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field)
widget_type, widget_args, widget_kwargs = _dataFormField2XMLUIData(field, read_only)
label = field.label or field.var
if label:
form_ui.addLabel(label)
@ -102,14 +108,14 @@ def dataForm2Widgets(form_ui, form):
return form_ui
def dataForm2XMLUI(form, submit_id, session_id=None):
def dataForm2XMLUI(form, submit_id, session_id=None, read_only=False):
"""Take a data form (xep-0004, Wokkel's implementation) and convert it to a SàT XML
@param submit_id: callback id to call when submitting form
@param session_id: id to return with the data
"""
form_ui = XMLUI("form", "vertical", submit_id=submit_id, session_id=session_id)
return dataForm2Widgets(form_ui, form)
return dataForm2Widgets(form_ui, form, read_only=read_only)
def dataFormResult2AdvancedList(xmlui, form_xml):
"""Take a raw data form (not parsed by XEP-0004) and convert it to an advanced list
@ -164,7 +170,7 @@ def dataFormResult2XMLUI(form_elt, session_id=None):
dataFormResult2AdvancedList(xml_ui, form_elt)
except exceptions.DataError:
parsed_form = data_form.Form.fromElement(form_elt)
dataForm2Widgets(xml_ui, parsed_form)
dataForm2Widgets(xml_ui, parsed_form, read_only=True)
return xml_ui
def _cleanValue(value):
@ -592,15 +598,13 @@ class Widget(Element):
InternalDataElement(self, data_elts)
class InputWidget(Widget):
pass
class EmptyWidget(Widget):
"""Place holder widget"""
type = 'empty'
class TextWidget(Widget):
"""Used for blob of text"""
type = 'text'
def __init__(self, xmlui, value, name=None, parent=None):
@ -610,6 +614,9 @@ class TextWidget(Widget):
class LabelWidget(Widget):
"""Used for one line blob of text,
most of time to display the desciption or name of the next widget
"""
type='label'
def __init__(self, xmlui, label, name=None, parent=None):
@ -618,6 +625,7 @@ class LabelWidget(Widget):
class JidWidget(Widget):
"""Used to display a Jabber ID, some specific methods can be added"""
type='jid'
def __init__(self, xmlui, jid, name=None, parent=None):
@ -648,11 +656,24 @@ class DividerWidget(Widget):
self.elem.setAttribute('style', style)
### Inputs ###
class InputWidget(Widget):
"""Input widget are widget which can accept user inputs,
used mainly in forms
"""
def __init__(self, xmlui, name=None, parent=None, read_only=False):
super(InputWidget, self).__init__(xmlui, name, parent)
if read_only:
self.elem.setAttribute('read_only', 'true')
class StringWidget(InputWidget):
type = 'string'
def __init__(self, xmlui, value=None, name=None, parent=None):
super(StringWidget, self).__init__(xmlui, name, parent)
def __init__(self, xmlui, value=None, name=None, parent=None, read_only=False):
super(StringWidget, self).__init__(xmlui, name, parent, read_only=read_only)
if value:
self.elem.setAttribute('value', value)
@ -665,10 +686,14 @@ class TextBoxWidget(StringWidget):
type = 'textbox'
class JidInputWidget(StringWidget):
type = 'jid_input'
class BoolWidget(InputWidget):
type = 'bool'
def __init__(self, xmlui, value='false', name=None, parent=None):
def __init__(self, xmlui, value='false', name=None, parent=None, read_only=False):
if isinstance(value, bool):
value = 'true' if value else 'false'
elif value == '0':
@ -677,7 +702,7 @@ class BoolWidget(InputWidget):
value='true'
if not value in ('true', 'false'):
raise exceptions.DataError(_("Value must be 0, 1, false or true"))
super(BoolWidget, self).__init__(xmlui, name, parent)
super(BoolWidget, self).__init__(xmlui, name, parent, read_only=read_only)
self.elem.setAttribute('value', value)