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
54 #self.joinGroup('report')
55 self.exportMethod(self.create)
57 def create(self, cr, uid, ids, datas, context=None):
61 Class to automatically build a document using the transformation process:
62 XML -> DATAS -> RML -> PDF
64 using a XSL:RML transformation
66 class report_rml(report_int):
67 def __init__(self, name, table, tmpl, xsl):
68 super(report_rml, self).__init__(name)
70 self.internal_header=False
75 'pdf': self.create_pdf,
76 'html': self.create_html,
77 'raw': self.create_raw,
78 'sxw': self.create_sxw,
79 'txt': self.create_txt,
80 'odt': self.create_odt,
81 'html2html' : self.create_html2html,
82 'makohtml2html' :self.create_makohtml2html,
85 def create(self, cr, uid, ids, datas, context):
86 xml = self.create_xml(cr, uid, ids, datas, context)
87 xml = tools.ustr(xml).encode('utf8')
88 report_type = datas.get('report_type', 'pdf')
89 if report_type == 'raw':
90 return (xml,report_type)
91 rml = self.create_rml(cr, xml, uid, context)
92 pool = pooler.get_pool(cr.dbname)
93 ir_actions_report_xml_obj = pool.get('ir.actions.report.xml')
94 report_xml_ids = ir_actions_report_xml_obj.search(cr, uid, [('report_name', '=', self.name[7:])], context=context)
95 self.title = report_xml_ids and ir_actions_report_xml_obj.browse(cr,uid,report_xml_ids)[0].name or 'OpenERP Report'
96 create_doc = self.generators[report_type]
97 pdf = create_doc(rml, title=self.title)
98 return (pdf, report_type)
100 def create_xml(self, cr, uid, ids, datas, context=None):
103 doc = print_xml.document(cr, uid, datas, {})
104 self.bin_datas.update( doc.bin_datas or {})
105 doc.parse(self.tmpl, ids, self.table, context)
108 return self.post_process_xml_data(cr, uid, xml, context)
110 def post_process_xml_data(self, cr, uid, xml, context=None):
114 # find the position of the 3rd tag
115 # (skip the <?xml ...?> and the "root" tag)
116 iter = re.finditer('<[^>]*>', xml)
121 doc = print_xml.document(cr, uid, {}, {})
122 tmpl_path = openerp.modules.get_module_resource('base', 'report', 'corporate_defaults.xml')
123 doc.parse(tmpl_path, [uid], 'res.users', context)
124 corporate_header = doc.xml_get()
127 # find the position of the tag after the <?xml ...?> tag
128 iter = re.finditer('<[^>]*>', corporate_header)
132 return xml[:pos_xml] + corporate_header[pos_header:] + xml[pos_xml:]
135 # TODO: The translation doesn't work for "<tag t="1">textext<tag> tex</tag>text</tag>"
137 def create_rml(self, cr, xml, uid, context=None):
138 if self.tmpl=='' and not self.internal_header:
139 self.internal_header=True
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_file = tools.file_open(self.xsl)
151 stylesheet = etree.parse(stylesheet_file)
152 xsl_path, _ = os.path.split(self.xsl)
153 for import_child in stylesheet.findall('./import'):
154 if 'href' in import_child.attrib:
155 imp_file = import_child.get('href')
156 _, imp_file = tools.file_open(imp_file, subdir=xsl_path, pathinfo=True)
157 import_child.set('href', urllib.quote(str(imp_file)))
160 stylesheet_file.close()
162 #TODO: get all the translation in one query. That means we have to:
163 # * build a list of items to translate,
164 # * issue the query to translate them,
165 # * (re)build/update the stylesheet with the translated items
167 def translate(doc, lang):
168 for node in doc.xpath('//*[@t]'):
171 translation = ir_translation_obj._get_source(cr, uid, self.name2, 'xsl', lang, node.text)
173 node.text = translation
175 if context.get('lang', False):
176 translate(stylesheet, context['lang'])
178 transform = etree.XSLT(stylesheet)
179 xml = etree.tostring(
180 transform(etree.fromstring(xml)))
184 def create_pdf(self, rml, localcontext = None, logo=None, title=None):
187 localcontext.update({'internal_header':self.internal_header})
189 self.bin_datas['logo'] = logo
191 if 'logo' in self.bin_datas:
192 del self.bin_datas['logo']
193 obj = render.rml(rml, localcontext, self.bin_datas, self._get_path(), title)
197 def create_html(self, rml, localcontext = None, logo=None, title=None):
198 obj = render.rml2html(rml, localcontext, self.bin_datas)
202 def create_txt(self, rml,localcontext, logo=None, title=None):
203 obj = render.rml2txt(rml, localcontext, self.bin_datas)
205 return obj.get().encode('utf-8')
207 def create_html2html(self, rml, localcontext = None, logo=None, title=None):
208 obj = render.html2html(rml, localcontext, self.bin_datas)
213 def create_raw(self,rml, localcontext = None, logo=None, title=None):
214 obj = render.odt2odt(etree.XML(rml),localcontext)
216 return etree.tostring(obj.get())
218 def create_sxw(self,rml,localcontext = None):
219 obj = render.odt2odt(rml,localcontext)
223 def create_odt(self,rml,localcontext = None):
224 obj = render.odt2odt(rml,localcontext)
228 def create_makohtml2html(self,html,localcontext = None):
229 obj = render.makohtml2html(html,localcontext)
235 ret.append(self.tmpl.replace(os.path.sep, '/').rsplit('/',1)[0]) # Same dir as the report rml
236 ret.append(tools.config['root_path'])
239 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: