1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
25 from lxml import etree
36 # encode a value to a string in utf8 and converts XML entities
39 if isinstance(val, str):
41 elif isinstance(val, unicode):
42 str_utf8 = val.encode('utf-8')
45 return str_utf8.replace('&', '&').replace('<','<').replace('>','>')
47 class report_int(netsvc.Service):
48 def __init__(self, name, audience='*'):
49 assert not self.exists(name), 'The report "%s" already exists!' % name
50 super(report_int, self).__init__(name, audience)
51 if name[0:7]<>'report.':
52 raise Exception, 'ConceptionError, bad report name, should start with "report."'
55 self.name2 = '.'.join(name.split('.')[1:])
57 #self.joinGroup('report')
58 self.exportMethod(self.create)
60 def create(self, cr, uid, ids, datas, context=None):
64 Class to automatically build a document using the transformation process:
65 XML -> DATAS -> RML -> PDF
67 using a XSL:RML transformation
69 class report_rml(report_int):
70 def __init__(self, name, table, tmpl, xsl):
71 super(report_rml, self).__init__(name)
78 'pdf': self.create_pdf,
79 'html': self.create_html,
80 'raw': self.create_raw,
81 'sxw': self.create_sxw,
82 'txt': self.create_txt,
83 'odt': self.create_odt,
84 'html2html' : self.create_html2html,
85 'makohtml2html' :self.create_makohtml2html,
88 def create(self, cr, uid, ids, datas, context):
89 xml = self.create_xml(cr, uid, ids, datas, context)
90 xml = tools.ustr(xml).encode('utf8')
91 report_type = datas.get('report_type', 'pdf')
92 if report_type == 'raw':
93 return (xml,report_type)
94 rml = self.create_rml(cr, xml, uid, context)
95 pool = pooler.get_pool(cr.dbname)
96 ir_actions_report_xml_obj = pool.get('ir.actions.report.xml')
97 report_xml_ids = ir_actions_report_xml_obj.search(cr, uid, [('report_name', '=', self.name[7:])], context=context)
98 self.title = report_xml_ids and ir_actions_report_xml_obj.browse(cr,uid,report_xml_ids)[0].name or 'OpenERP Report'
99 create_doc = self.generators[report_type]
100 pdf = create_doc(rml, title=self.title)
101 return (pdf, report_type)
103 def create_xml(self, cr, uid, ids, datas, context=None):
106 doc = print_xml.document(cr, uid, datas, {})
107 self.bin_datas.update( doc.bin_datas or {})
108 doc.parse(self.tmpl, ids, self.table, context)
111 return self.post_process_xml_data(cr, uid, xml, context)
113 def post_process_xml_data(self, cr, uid, xml, context=None):
116 # find the position of the 3rd tag
117 # (skip the <?xml ...?> and the "root" tag)
118 iter = re.finditer('<[^>]*>', xml)
123 doc = print_xml.document(cr, uid, {}, {})
124 tmpl_path = addons.get_module_resource('base', 'report', 'corporate_defaults.xml')
125 doc.parse(tmpl_path, [uid], 'res.users', context)
126 corporate_header = doc.xml_get()
129 # find the position of the tag after the <?xml ...?> tag
130 iter = re.finditer('<[^>]*>', corporate_header)
134 return xml[:pos_xml] + corporate_header[pos_header:] + xml[pos_xml:]
137 # TODO: The translation doesn't work for "<tag t="1">textext<tag> tex</tag>text</tag>"
139 def create_rml(self, cr, xml, uid, context=None):
142 pool = pooler.get_pool(cr.dbname)
143 ir_translation_obj = pool.get('ir.translation')
145 # In some case we might not use xsl ...
149 stylesheet = etree.parse(tools.file_open(self.xsl))
150 xsl_path, _ = os.path.split(self.xsl)
151 for import_child in stylesheet.findall('./import'):
152 if 'href' in import_child.attrib:
153 imp_file = import_child.get('href')
154 _, imp_file = tools.file_open(imp_file, subdir=xsl_path, pathinfo=True)
155 import_child.set('href', urllib.quote(str(imp_file)))
157 #TODO: get all the translation in one query. That means we have to:
158 # * build a list of items to translate,
159 # * issue the query to translate them,
160 # * (re)build/update the stylesheet with the translated items
162 def translate(doc, lang):
163 for node in doc.xpath('//*[@t]'):
164 translation = ir_translation_obj._get_source(cr, uid, self.name2, 'xsl', lang, node.text)
166 node.text = translation
168 if context.get('lang', False):
169 translate(stylesheet, context['lang'])
171 transform = etree.XSLT(stylesheet)
172 xml = etree.tostring(
173 transform(etree.fromstring(xml)))
177 def create_pdf(self, rml, localcontext = None, logo=None, title=None):
180 if self.tmpl=='' and not self.pageCount:
184 localcontext.update({'header':self.pageCount})
187 self.bin_datas['logo'] = logo
189 if 'logo' in self.bin_datas:
190 del self.bin_datas['logo']
191 obj = render.rml(rml, localcontext, self.bin_datas, tools.config['root_path'],title)
196 def create_html(self, rml, localcontext = None, logo=None, title=None):
197 obj = render.rml2html(rml, localcontext, self.bin_datas)
201 def create_txt(self, rml,localcontext, logo=None, title=None):
202 obj = render.rml2txt(rml, localcontext, self.bin_datas)
204 return obj.get().encode('utf-8')
206 def create_html2html(self, rml, localcontext = None, logo=None, title=None):
207 obj = render.html2html(rml, localcontext, self.bin_datas)
212 def create_raw(self,rml, localcontext = None, logo=None, title=None):
213 obj = render.odt2odt(etree.XML(rml),localcontext)
215 return etree.tostring(obj.get())
217 def create_sxw(self,rml,localcontext = None):
218 obj = render.odt2odt(rml,localcontext)
222 def create_odt(self,rml,localcontext = None):
223 obj = render.odt2odt(rml,localcontext)
227 def create_makohtml2html(self,html,localcontext = None):
228 obj = render.makohtml2html(html,localcontext)
233 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: