From 787818dfbd9700494515468815226eab184de520 Mon Sep 17 00:00:00 2001 From: Jan Orel Date: Tue, 15 Jan 2019 10:39:58 +0100 Subject: [PATCH] development: [PyOne] prevent dropping empty values in template --- src/oca/python/pyone/util.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/oca/python/pyone/util.py b/src/oca/python/pyone/util.py index 64859c8e29..45cf719ef5 100644 --- a/src/oca/python/pyone/util.py +++ b/src/oca/python/pyone/util.py @@ -1,6 +1,6 @@ # Copyright 2018 www.privaz.io Valletech AB # Copyright 2002-2018, OpenNebula Project, OpenNebula Systems -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -46,7 +46,8 @@ def cast2one(param): root = list(param.values())[0] if isinstance(root, dict): # We return this dictionary as XML - return dicttoxml.dicttoxml(param, root=False, attr_type=False).decode('utf8') + return dicttoxml.dicttoxml(param, root=False, attr_type=False, + cdata=True).decode('utf8') else: # We return this dictionary as attribute=value vector ret = u"" @@ -72,6 +73,14 @@ def one2dict(element): return element._root +def none2emptystr(d): + for k,v in d.items(): + if type(v) == OrderedDict: + none2emptystr(v) + elif v == None: + d[k] = "" + + def child2dict(element): ''' Creates a dictionary from the documentTree obtained from a binding Element. @@ -93,6 +102,9 @@ def child2dict(element): if ret[tagName] == None: ret[tagName] = OrderedDict() + # Replace 'None' values returned by xmltodict by "" + none2emptystr(ret) + # return the contents dictionary, but save a reference ret[tagName]._root = ret return ret[tagName]