comparison src/tools/xml_tools.py @ 760:73a0077f80cc

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 ! /!\
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:19:18 +0100
parents 93bd868b8fb6
children 2f8d72226bc0
comparison
equal deleted inserted replaced
759:93bd868b8fb6 760:73a0077f80cc
217 217
218 218
219 class XMLUI(object): 219 class XMLUI(object):
220 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML""" 220 """This class is used to create a user interface (form/window/parameters/etc) using SàT XML"""
221 221
222 def __init__(self, panel_type, layout="vertical", title=None, submit_id=None): 222 def __init__(self, panel_type, layout="vertical", title=None, submit_id=None, session_id=None):
223 """Init SàT XML Panel 223 """Init SàT XML Panel
224 @param panel_type: one of 224 @param panel_type: one of
225 - window (new window) 225 - window (new window)
226 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons) 226 - form (formulaire, depend of the frontend, usually a panel with cancel/submit buttons)
227 - param (parameters, presentatio depend of the frontend) 227 - param (parameters, presentatio depend of the frontend)
232 (usually one for a label, the next for the element) 232 (usually one for a label, the next for the element)
233 - tabs: elemens are in categories with tabs (notebook) 233 - tabs: elemens are in categories with tabs (notebook)
234 @param title: title or default if None 234 @param title: title or default if None
235 @param submit_id: callback id to call for panel_type we can submit (form, param) 235 @param submit_id: callback id to call for panel_type we can submit (form, param)
236 """ 236 """
237 if not panel_type in ['window', 'form', 'param']: 237 if panel_type not in ['window', 'form', 'param']:
238 error(_("Unknown panel type [%s]") % panel_type) 238 raise exceptions.DataError(_("Unknown panel type [%s]") % panel_type)
239 assert(False) 239 if panel_type == 'form' and submit_id is None:
240 raise exceptions.DataError(_("form XMLUI need a submit_id"))
240 self.type_ = panel_type 241 self.type_ = panel_type
241 impl = minidom.getDOMImplementation() 242 impl = minidom.getDOMImplementation()
242 243
243 self.doc = impl.createDocument(None, "sat_xmlui", None) 244 self.doc = impl.createDocument(None, "sat_xmlui", None)
244 top_element = self.doc.documentElement 245 top_element = self.doc.documentElement
245 top_element.setAttribute("type", panel_type) 246 top_element.setAttribute("type", panel_type)
246 if title: 247 if title:
247 top_element.setAttribute("title", title) 248 top_element.setAttribute("title", title)
248 if submit_id: 249 if submit_id:
249 top_element.setAttribute("submit", submit_id) 250 top_element.setAttribute("submit", submit_id)
251 if session_id is not None:
252 top_element.setAttribute("session_id", session_id)
250 self.parentTabsLayout = None # used only we have 'tabs' layout 253 self.parentTabsLayout = None # used only we have 'tabs' layout
251 self.currentCategory = None # used only we have 'tabs' layout 254 self.currentCategory = None # used only we have 'tabs' layout
252 self.currentLayout = None 255 self.currentLayout = None
253 self.changeLayout(layout) 256 self.changeLayout(layout)
254 257
255 def __del__(self): 258 def __del__(self):
256 self.doc.unlink() 259 self.doc.unlink()
260
261 def setSessionId(self, session_id):
262 assert(session_id)
263 top_element = self.doc.documentElement
264 top_element.setAttribute("session_id", session_id)
265
257 266
258 def _createLayout(self, layout, parent=None): 267 def _createLayout(self, layout, parent=None):
259 """Create a layout element 268 """Create a layout element
260 @param type: layout type (cf init doc) 269 @param type: layout type (cf init doc)
261 @parent: parent element or None 270 @parent: parent element or None
413 def addOptions(self, options, parent): 422 def addOptions(self, options, parent):
414 """Add options to a multi-values element (e.g. list) 423 """Add options to a multi-values element (e.g. list)
415 @param parent: multi-values element""" 424 @param parent: multi-values element"""
416 for option in options: 425 for option in options:
417 opt = self.doc.createElement('option') 426 opt = self.doc.createElement('option')
418 opt.setAttribute('value', option) 427 if isinstance(option, basestring):
428 value, label = option, option
429 elif isinstance(option, tuple):
430 value, label = option
431 opt.setAttribute('value', value)
432 opt.setAttribute('label', label)
419 parent.appendChild(opt) 433 parent.appendChild(opt)
420 434
421 # Advanced list 435 # Advanced list
422 436
423 def addHeaders(self, headers, parent): 437 def addHeaders(self, headers, parent):