memory (params): fixes loading user value and building params XML for jids_list type

This commit is contained in:
souliane 2015-12-15 17:43:36 +01:00
commit 4d3d6fb8b8

View file

@ -371,23 +371,25 @@ class Params(object):
if node.getAttribute('type') == 'int': if node.getAttribute('type') == 'int':
return int(value_to_use) return int(value_to_use)
elif node.getAttribute('type') == 'list': elif node.getAttribute('type') == 'list':
assert not value_to_use # only accept <option selected...> and not <param value...> if not value_to_use: # no user defined value, take default value from the XML
options = [option for option in node.childNodes if option.nodeName == 'option'] options = [option for option in node.childNodes if option.nodeName == 'option']
selected = [option for option in options if option.getAttribute('selected') == 'true'] selected = [option for option in options if option.getAttribute('selected') == 'true']
cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name') cat, param = node.parentNode.getAttribute('name'), node.getAttribute('name')
if len(selected) == 1: if len(selected) == 1:
value_to_use = selected[0].getAttribute('value') value_to_use = selected[0].getAttribute('value')
log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") % log.info(_("Unset parameter (%(cat)s, %(param)s) of type list will use the default option '%(value)s'") %
{'cat': cat, 'param': param, 'value': value_to_use}) {'cat': cat, 'param': param, 'value': value_to_use})
return value_to_use return value_to_use
if len(selected) == 0: if len(selected) == 0:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param}) log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has no default option!') % {'cat': cat, 'param': param})
else: else:
log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param}) log.error(_(u'Parameter (%(cat)s, %(param)s) of type list has more than one default option!') % {'cat': cat, 'param': param})
raise exceptions.DataError raise exceptions.DataError
elif node.getAttribute('type') == 'jids_list': elif node.getAttribute('type') == 'jids_list':
assert not value_to_use # only accept <jid>...</jid> and not <param value...> if value_to_use:
jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")] jids = value_to_use.split('\t') # FIXME: it's not good to use tabs as separator !
else: # no user defined value, take default value from the XML
jids = [getText(jid_) for jid_ in node.getElementsByTagName("jid")]
to_delete = [] to_delete = []
for idx, value in enumerate(jids): for idx, value in enumerate(jids):
try: try:
@ -689,15 +691,17 @@ class Params(object):
except NotFoundErr: except NotFoundErr:
pass pass
elif dest_params[name].getAttribute('type') == 'jids_list': elif dest_params[name].getAttribute('type') == 'jids_list':
jids = [getText(jid_) for jid_ in dest_params[name].getElementsByTagName("jid")] jids = profile_value.split('\t')
for jid_ in jids: for jid_elt in dest_params[name].getElementsByTagName("jid"):
dest_params[name].removeChild(jid_elt) # remove all default
for jid_ in jids: # rebuilt the children with use values
try: try:
jid.JID(jid_) jid.JID(jid_)
except (RuntimeError, jid.InvalidFormat, AttributeError): except (RuntimeError, jid.InvalidFormat, AttributeError):
log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_)) log.warning(u"Incorrect jid value found in jids list: [{}]".format(jid_))
else: else:
jid_elt = prof_xml.createElement('jid') jid_elt = prof_xml.createElement('jid')
jid_elt.appendChild(prof_xml.createTextNode()) jid_elt.appendChild(prof_xml.createTextNode(jid_))
dest_params[name].appendChild(jid_elt) dest_params[name].appendChild(jid_elt)
else: else:
dest_params[name].setAttribute('value', profile_value) dest_params[name].setAttribute('value', profile_value)