backend, frontends: XMLUI refactoring:

- session_id is resent in result data if found in incoming data
    - list now use distinct labels and values (label is the one displayed, value is the one used for everything else)
    - misc refactoring

/!\ urwid SàtExt must be updated ! /!\
This commit is contained in:
Goffi 2013-12-24 15:19:18 +01:00
commit 80a7423f02
3 changed files with 46 additions and 21 deletions

View file

@ -105,7 +105,7 @@ class XMLUI(urwid.WidgetWrap):
self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
elif type_=="list":
style=[] if elem.getAttribute("multi")=='yes' else ['single']
ctrl = sat_widgets.List(options=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style, on_change=self.onParamChange if self.type == "param" else None)
ctrl = sat_widgets.List(options=[(option.getAttribute("value"), option.getAttribute("label")) for option in elem.getElementsByTagName("option")], style=style, on_change=self.onParamChange if self.type == "param" else None)
ctrl.selectValue(elem.getAttribute("value"))
self.ctrl_list[name] = ({'type':type_, 'control':ctrl})
elif type_=="button":
@ -178,6 +178,7 @@ class XMLUI(urwid.WidgetWrap):
top=cat_dom.documentElement
self.type = top.getAttribute("type")
self.title = top.getAttribute("title") or self.title
self.session_id = top.getAttribute("session_id") or None
self.submit_id = top.getAttribute("submit") or None
if top.nodeName != "sat_xmlui" or not self.type in ['form', 'param', 'window']:
raise InvalidXMLUI
@ -251,24 +252,27 @@ class XMLUI(urwid.WidgetWrap):
self.param_changed.add(widget)
def onFormSubmitted(self, button):
data = []
selected_values = []
for ctrl_name in self.ctrl_list:
ctrl = self.ctrl_list[ctrl_name]
if isinstance(ctrl['control'], sat_widgets.List):
data.append((ctrl_name, u'\t'.join(ctrl['control'].getSelectedValues())))
selected_values.append((ctrl_name, u'\t'.join([option.value for option in ctrl['control'].getSelectedValues()])))
elif isinstance(ctrl['control'], urwid.CheckBox):
data.append((ctrl_name, "true" if ctrl['control'].get_state() else "false"))
selected_values.append((ctrl_name, "true" if ctrl['control'].get_state() else "false"))
else:
data.append((ctrl_name, ctrl['control'].get_edit_text()))
selected_values.append((ctrl_name, ctrl['control'].get_edit_text()))
if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
raise NotImplementedError
elif 'callback' in self.misc: # FIXME: this part is not needed anymore
try:
self.misc['callback'](data, submit_id=self.submit_id, *self.misc['callback_args'])
self.misc['callback'](selected_values, submit_id=self.submit_id, *self.misc['callback_args'])
except KeyError:
self.misc['callback'](data, submit_id=self.submit_id)
self.misc['callback'](selected_values, submit_id=self.submit_id)
elif self.submit_id is not None:
self.host.launchAction(self.submit_id, dict(data), profile_key=self.host.profile)
data = dict(selected_values)
if self.session_id is not None:
data["session_id"] = self.session_id
self.host.launchAction(self.submit_id, data, profile_key=self.host.profile)
else:
warning (_("The form data is not sent back, the type is not managed properly"))

View file

@ -95,8 +95,10 @@ class XMLUI(wx.Frame):
_proportion = 1
elif type=="list":
style=wx.LB_MULTIPLE if elem.getAttribute("multi")=='yes' else wx.LB_SINGLE
ctrl = wx.ListBox(parent, -1, choices=[option.getAttribute("value") for option in elem.getElementsByTagName("option")], style=style)
self.ctrl_list[name] = ({'type':type, 'control':ctrl})
_options = [(option.getAttribute("label"), option.getAttribute("value")) for option in elem.getElementsByTagName("option")]
attr_map = {label: value for label, value in _options}
ctrl = wx.ListBox(parent, -1, choices=[option[0] for option in _options], style=style)
self.ctrl_list[name] = ({'type':type, 'control':ctrl, 'attr_map': attr_map})
_proportion = 1
elif type=="button":
callback_id = elem.getAttribute("callback_id")
@ -167,6 +169,7 @@ class XMLUI(wx.Frame):
top= cat_dom.documentElement
self.type = top.getAttribute("type")
self.title = top .getAttribute("title")
self.session_id = top.getAttribute("session_id") or None
self.submit_id = top.getAttribute("submit") or None
if self.title:
self.SetTitle(self.title)
@ -211,23 +214,27 @@ class XMLUI(wx.Frame):
def onFormSubmitted(self, event):
"""Called when submit button is clicked"""
debug(_("Submitting form"))
data = []
selected_values = []
for ctrl_name in self.ctrl_list:
ctrl = self.ctrl_list[ctrl_name]
if isinstance(ctrl['control'], wx.ListBox):
data.append((ctrl_name, ctrl['control'].GetStringSelection()))
label = ctrl['control'].GetStringSelection()
value = ctrl['attr_map'][label]
selected_values.append((ctrl_name, value))
elif isinstance(ctrl['control'], wx.CheckBox):
data.append((ctrl_name, "true" if ctrl['control'].GetValue() else "false"))
selected_values.append((ctrl_name, "true" if ctrl['control'].GetValue() else "false"))
else:
data.append((ctrl_name, ctrl['control'].GetValue()))
selected_values.append((ctrl_name, ctrl['control'].GetValue()))
if self.misc.has_key('action_back'): #FIXME FIXME FIXME: WTF ! Must be cleaned
id = self.misc['action_back']("SUBMIT",self.misc['target'], data)
id = self.misc['action_back']("SUBMIT",self.misc['target'], selected_values)
self.host.current_action_ids.add(id)
elif self.misc.has_key('callback'):
self.misc['callback'](data)
self.misc['callback'](selected_values)
elif self.submit_id is not None:
data = dict(selected_values)
if self.session_id is not None:
data["session_id"] = self.session_id
self.host.launchAction(self.submit_id, data, profile_key=self.host.profile)
else:
warning (_("The form data is not sent back, the type is not managed properly"))

View file

@ -219,7 +219,7 @@ class Field(object):
class XMLUI(object):
"""This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
def __init__(self, panel_type, layout="vertical", title=None, submit_id=None):
def __init__(self, panel_type, layout="vertical", title=None, submit_id=None, session_id=None):
"""Init SàT XML Panel
@param panel_type: one of
- window (new window)
@ -234,9 +234,10 @@ class XMLUI(object):
@param title: title or default if None
@param submit_id: callback id to call for panel_type we can submit (form, param)
"""
if not panel_type in ['window', 'form', 'param']:
error(_("Unknown panel type [%s]") % panel_type)
assert(False)
if panel_type not in ['window', 'form', 'param']:
raise exceptions.DataError(_("Unknown panel type [%s]") % panel_type)
if panel_type == 'form' and submit_id is None:
raise exceptions.DataError(_("form XMLUI need a submit_id"))
self.type_ = panel_type
impl = minidom.getDOMImplementation()
@ -247,6 +248,8 @@ class XMLUI(object):
top_element.setAttribute("title", title)
if submit_id:
top_element.setAttribute("submit", submit_id)
if session_id is not None:
top_element.setAttribute("session_id", session_id)
self.parentTabsLayout = None # used only we have 'tabs' layout
self.currentCategory = None # used only we have 'tabs' layout
self.currentLayout = None
@ -255,6 +258,12 @@ class XMLUI(object):
def __del__(self):
self.doc.unlink()
def setSessionId(self, session_id):
assert(session_id)
top_element = self.doc.documentElement
top_element.setAttribute("session_id", session_id)
def _createLayout(self, layout, parent=None):
"""Create a layout element
@param type: layout type (cf init doc)
@ -415,7 +424,12 @@ class XMLUI(object):
@param parent: multi-values element"""
for option in options:
opt = self.doc.createElement('option')
opt.setAttribute('value', option)
if isinstance(option, basestring):
value, label = option, option
elif isinstance(option, tuple):
value, label = option
opt.setAttribute('value', value)
opt.setAttribute('label', label)
parent.appendChild(opt)
# Advanced list