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
26 import openerp.netsvc as netsvc
27 import openerp.pooler as pooler
29 import openerp.tools as tools
30 import openerp.modules
36 # coerce any type to a unicode string (to preserve non-ascii characters)
37 # and escape XML entities
40 unicode_value = tools.ustr(value)
41 return unicode_value.replace('&', '&').replace('<','<').replace('>','>')
43 class report_int(netsvc.Service):
44 def __init__(self, name, audience='*'):
45 assert not self.exists(name), 'The report "%s" already exists!' % name
46 super(report_int, self).__init__(name, audience)
47 if name[0:7]<>'report.':
48 raise Exception, 'ConceptionError, bad report name, should start with "report."'
51 self.name2 = '.'.join(name.split('.')[1:])
52 # TODO the reports have methods with a 'title' kwarg that is redundant with this attribute
55 def create(self, cr, uid, ids, datas, context=None):
59 Class to automatically build a document using the transformation process:
60 XML -> DATAS -> RML -> PDF
62 using a XSL:RML transformation
64 class report_rml(report_int):
65 def __init__(self, name, table, tmpl, xsl):
66 super(report_rml, self).__init__(name)
68 self.internal_header=False
73 'pdf': self.create_pdf,
74 'html': self.create_html,
75 'raw': self.create_raw,
76 'sxw': self.create_sxw,
77 'txt': self.create_txt,
78 'odt': self.create_odt,
79 'html2html' : self.create_html2html,
80 'makohtml2html' :self.create_makohtml2html,
83 def create(self, cr, uid, ids, datas, context):
84 xml = self.create_xml(cr, uid, ids, datas, context)
85 xml = tools.ustr(xml).encode('utf8')
86 report_type = datas.get('report_type', 'pdf')
87 if report_type == 'raw':
88 return (xml,report_type)
89 rml = self.create_rml(cr, xml, uid, context)
90 pool = pooler.get_pool(cr.dbname)
91 ir_actions_report_xml_obj = pool.get('ir.actions.report.xml')
92 report_xml_ids = ir_actions_report_xml_obj.search(cr, uid, [('report_name', '=', self.name[7:])], context=context)
93 self.title = report_xml_ids and ir_actions_report_xml_obj.browse(cr,uid,report_xml_ids)[0].name or 'OpenERP Report'
94 create_doc = self.generators[report_type]
95 pdf = create_doc(rml, title=self.title)
96 return (pdf, report_type)
98 def create_xml(self, cr, uid, ids, datas, context=None):
101 doc = print_xml.document(cr, uid, datas, {})
102 self.bin_datas.update( doc.bin_datas or {})
103 doc.parse(self.tmpl, ids, self.table, context)
106 return self.post_process_xml_data(cr, uid, xml, context)
108 def post_process_xml_data(self, cr, uid, xml, context=None):
112 # find the position of the 3rd tag
113 # (skip the <?xml ...?> and the "root" tag)
114 iter = re.finditer('<[^>]*>', xml)
119 doc = print_xml.document(cr, uid, {}, {})
120 tmpl_path = openerp.modules.get_module_resource('base', 'report', 'corporate_defaults.xml')
121 doc.parse(tmpl_path, [uid], 'res.users', context)
122 corporate_header = doc.xml_get()
125 # find the position of the tag after the <?xml ...?> tag
126 iter = re.finditer('<[^>]*>', corporate_header)
130 return xml[:pos_xml] + corporate_header[pos_header:] + xml[pos_xml:]
133 # TODO: The translation doesn't work for "<tag t="1">textext<tag> tex</tag>text</tag>"
135 def create_rml(self, cr, xml, uid, context=None):
136 if self.tmpl=='' and not self.internal_header:
137 self.internal_header=True
140 pool = pooler.get_pool(cr.dbname)
141 ir_translation_obj = pool.get('ir.translation')
143 # In some case we might not use xsl ...
147 stylesheet_file = tools.file_open(self.xsl)
149 stylesheet = etree.parse(stylesheet_file)
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)))
158 stylesheet_file.close()
160 #TODO: get all the translation in one query. That means we have to:
161 # * build a list of items to translate,
162 # * issue the query to translate them,
163 # * (re)build/update the stylesheet with the translated items
165 def translate(doc, lang):
166 for node in doc.xpath('//*[@t]'):
169 translation = ir_translation_obj._get_source(cr, uid, self.name2, 'xsl', lang, node.text)
171 node.text = translation
173 if context.get('lang', False):
174 translate(stylesheet, context['lang'])
176 transform = etree.XSLT(stylesheet)
177 xml = etree.tostring(
178 transform(etree.fromstring(xml)))
182 def create_pdf(self, rml, localcontext = None, logo=None, title=None):
185 localcontext.update({'internal_header':self.internal_header})
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, self._get_path(), title)
195 def create_html(self, rml, localcontext = None, logo=None, title=None):
196 obj = render.rml2html(rml, localcontext, self.bin_datas)
200 def create_txt(self, rml,localcontext, logo=None, title=None):
201 obj = render.rml2txt(rml, localcontext, self.bin_datas)
203 return obj.get().encode('utf-8')
205 def create_html2html(self, rml, localcontext = None, logo=None, title=None):
206 obj = render.html2html(rml, localcontext, self.bin_datas)
211 def create_raw(self,rml, localcontext = None, logo=None, title=None):
212 obj = render.odt2odt(etree.XML(rml),localcontext)
214 return etree.tostring(obj.get())
216 def create_sxw(self,rml,localcontext = None):
217 obj = render.odt2odt(rml,localcontext)
221 def create_odt(self,rml,localcontext = None):
222 obj = render.odt2odt(rml,localcontext)
226 def create_makohtml2html(self,html,localcontext = None):
227 obj = render.makohtml2html(html,localcontext)
233 ret.append(self.tmpl.replace(os.path.sep, '/').rsplit('/',1)[0]) # Same dir as the report rml
234 ret.append(tools.config['root_path'])
237 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: