[IMP] res.users: enable cache on context_get, used by the _() translation function
[odoo/odoo.git] / addons / base_report_designer / base_report_designer.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import osv
23 from openerp_sxw2rml import sxw2rml
24 from StringIO import StringIO
25 import base64
26 import pooler
27 import addons
28  
29
30 class report_xml(osv.osv):
31     _inherit = 'ir.actions.report.xml'
32
33     def sxwtorml(self, cr, uid, file_sxw, file_type):
34         '''
35         The use of this function is to get rml file from sxw file.
36         '''
37         sxwval = StringIO(base64.decodestring(file_sxw))
38         if file_type=='sxw':
39             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_oo2rml.xsl'),'rb')
40         if file_type=='odt':
41             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_odt2rml.xsl'),'rb')
42         return  {'report_rml_content': str(sxw2rml(sxwval, xsl=fp.read()))}
43
44     def upload_report(self, cr, uid, report_id, file_sxw, file_type, context=None):
45         '''
46         Untested function
47         '''
48         pool = pooler.get_pool(cr.dbname)
49         sxwval = StringIO(base64.decodestring(file_sxw))
50         if file_type=='sxw':
51             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_oo2rml.xsl'),'rb')
52         if file_type=='odt':
53             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_odt2rml.xsl'),'rb')
54         report = pool.get('ir.actions.report.xml').write(cr, uid, [report_id], {
55             'report_sxw_content': base64.decodestring(file_sxw), 
56             'report_rml_content': str(sxw2rml(sxwval, xsl=fp.read())), 
57         })
58         pool.get('ir.actions.report.xml').register_all(cr)
59         return True
60
61     def report_get(self, cr, uid, report_id, context=None):
62         if context is None:
63             context = {}
64         # skip osv.fields.sanitize_binary_value() because we want the raw bytes in all cases
65         context.update(bin_raw=True)
66         report = self.browse(cr, uid, report_id, context=context)
67         sxw_data = report.report_sxw_content
68         rml_data = report.report_rml_content
69         if isinstance(sxw_data, unicode):
70             sxw_data = sxw_data.encode("iso-8859-1", "replace")
71         if isinstance(rml_data, unicode):
72             rml_data = rml_data.encode("iso-8859-1", "replace")
73         return {
74             'file_type' : report.report_type,
75             'report_sxw_content': sxw_data and base64.encodestring(sxw_data) or False,
76             'report_rml_content': rml_data and base64.encodestring(rml_data) or False
77         }
78
79 report_xml()
80
81 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
82