improved
[odoo/odoo.git] / bin / report / print_xml.py
index 63b3424..f349e10 100644 (file)
@@ -1,22 +1,21 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
-#
+#    
 #    OpenERP, Open Source Management Solution
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
 
@@ -24,6 +23,7 @@ import os,types
 from lxml import etree
 import netsvc
 import tools
+from tools.safe_eval import safe_eval
 import print_fnc
 import copy
 from osv.orm import browse_null, browse_record
@@ -112,7 +112,7 @@ class document(object):
 #Pinky: Why not this ? eval(expr, browser) ?
 #       name = browser.name
 #       data_dict = browser._data[self.get_value(browser, 'id')]
-        return eval(expr, {}, {'obj': record})
+        return safe_eval(expr, {}, {'obj': record})
 
     def parse_node(self, node, parent, browser, datas=None):
             attrs = self.node_attrs_get(node)
@@ -122,8 +122,7 @@ class document(object):
 #TODO: test this
                     if value == '' and 'default' in attrs:
                         value = attrs['default']
-                    el = etree.Element(node.tag)
-                    parent.append(el)
+                    el = etree.SubElement(parent, node.tag)
                     el.text = tounicode(value)
 #TODO: test this
                     for key, value in attrs.iteritems():
@@ -155,31 +154,27 @@ class document(object):
                             fp.write(dt)
                             i = str(len(self.bin_datas))
                             self.bin_datas[i] = fp
-                            el = etree.Element(node.tag)
+                            el = etree.SubElement(parent, node.tag)
                             el.text = i
-                            parent.append(el)
 
                 elif attrs['type']=='data':
 #TODO: test this
                     txt = self.datas.get('form', {}).get(attrs['name'], '')
-                    el = etree.Element(node.tag)
+                    el = etree.SubElement(parent, node.tag)
                     el.text = txt
-                    parent.append(el)
 
                 elif attrs['type']=='function':
                     if attrs['name'] in self.func:
                         txt = self.func[attrs['name']](node)
                     else:
                         txt = print_fnc.print_fnc(attrs['name'], node)
-                    el = etree.Element(node.tag)
+                    el = etree.SubElement(parent, node.tag)
                     el.text = txt
-                    parent.append(el)
 
                 elif attrs['type']=='eval':
                     value = self.eval(browser, attrs['expr'])
-                    el = etree.Element(node.tag)
+                    el = etree.SubElement(parent, node.tag)
                     el.text = str(value)
-                    parent.append(el)
 
                 elif attrs['type']=='fields':
                     fields = attrs['name'].split(',')
@@ -197,8 +192,7 @@ class document(object):
 
                     v_list = [vals[k] for k in keys]
                     for v in v_list:
-                        el = etree.Element(node.tag)
-                        parent.append(el)
+                        el = etree.SubElement(parent, node.tag)
                         for el_cld in node:
                             self.parse_node(el_cld, el, v)
 
@@ -209,7 +203,6 @@ class document(object):
                         args = [self.eval(browser, arg) for arg in attrs['args'].split(',')]
                     else:
                         args = []
-
                     # get the object
                     if attrs.has_key('model'):
                         obj = self.pool.get(attrs['model'])
@@ -232,8 +225,8 @@ class document(object):
                     newdatas = getattr(obj, attrs['name'])(self.cr, self.uid, ids, *args)
 
                     def parse_result_tree(node, parent, datas):
-                            el = etree.Element(node.tag)
-                            parent.append(el)
+                        if not node.tag == etree.Comment:
+                            el = etree.SubElement(parent, node.tag)
                             atr = self.node_attrs_get(node)
                             if 'value' in atr:
                                 if not isinstance(datas[atr['value']], (str, unicode)):
@@ -251,25 +244,26 @@ class document(object):
 
                 elif attrs['type']=='zoom':
                     value = self.get_value(browser, attrs['name'])
-
                     if value:
                         if not isinstance(value, list):
                             v_list = [value]
                         else:
                             v_list = value
                         for v in v_list:
-                            el = etree.Element(node.tag)
-                            parent.append(el)
+                            el = etree.SubElement(parent, node.tag)
                             for el_cld in node:
                                 self.parse_node(el_cld, el, v)
             else:
                 # if there is no "type" attribute in the node, copy it to the xml data and parse its childs
-                for el_cld in node:
-                    self.parse_node(el_cld, parent, browser)
-
+                if not node.tag == etree.Comment:
+                    if node.tag == parent.tag:
+                        el = parent
+                    else:
+                        el = etree.SubElement(parent, node.tag)
+                    for el_cld in node:
+                        self.parse_node(el_cld,el, browser)
     def xml_get(self):
-        #return self.doc.toxml('utf-8')
-        return etree.tostring(self.doc,encoding="utf-8",xml_declaration=True)
+        return etree.tostring(self.doc,encoding="utf-8",xml_declaration=True,pretty_print=True)
 
     def parse_tree(self, ids, model, context=None):
         if not context:
@@ -282,7 +276,6 @@ class document(object):
             context={}
         # parses the xml template to memory
         self.dom = etree.XML(xml)
-
         # create the xml data from the xml template
         self.parse_tree(ids, model, context)