[FIX] board: keep group by in context when adding to dashboard
[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 import base64
23 import openerp.modules.registry
24 from openerp.osv import osv
25 from openerp_sxw2rml import sxw2rml
26 from StringIO import StringIO
27 from openerp import pooler
28 from openerp import addons
29  
30
31 class report_xml(osv.osv):
32     _inherit = 'ir.actions.report.xml'
33
34     def sxwtorml(self, cr, uid, file_sxw, file_type):
35         '''
36         The use of this function is to get rml file from sxw file.
37         '''
38         sxwval = StringIO(base64.decodestring(file_sxw))
39         if file_type=='sxw':
40             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_oo2rml.xsl'),'rb')
41         if file_type=='odt':
42             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_odt2rml.xsl'),'rb')
43         return  {'report_rml_content': str(sxw2rml(sxwval, xsl=fp.read()))}
44
45     def upload_report(self, cr, uid, report_id, file_sxw, file_type, context=None):
46         '''
47         Untested function
48         '''
49         pool = pooler.get_pool(cr.dbname)
50         sxwval = StringIO(base64.decodestring(file_sxw))
51         if file_type=='sxw':
52             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_oo2rml.xsl'),'rb')
53         if file_type=='odt':
54             fp = open(addons.get_module_resource('base_report_designer','openerp_sxw2rml', 'normalized_odt2rml.xsl'),'rb')
55         report = pool.get('ir.actions.report.xml').write(cr, uid, [report_id], {
56             'report_sxw_content': base64.decodestring(file_sxw), 
57             'report_rml_content': str(sxw2rml(sxwval, xsl=fp.read())), 
58         })
59
60         # FIXME: this should be moved to an override of the ir.actions.report_xml.create() method
61         cr.commit()
62         pool.get('ir.actions.report.xml').register_all(cr)
63         openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname)
64
65         return True
66
67     def report_get(self, cr, uid, report_id, context=None):
68         if context is None:
69             context = {}
70         # skip osv.fields.sanitize_binary_value() because we want the raw bytes in all cases
71         context.update(bin_raw=True)
72         report = self.browse(cr, uid, report_id, context=context)
73         sxw_data = report.report_sxw_content
74         rml_data = report.report_rml_content
75         if isinstance(sxw_data, unicode):
76             sxw_data = sxw_data.encode("iso-8859-1", "replace")
77         if isinstance(rml_data, unicode):
78             rml_data = rml_data.encode("iso-8859-1", "replace")
79         return {
80             'file_type' : report.report_type,
81             'report_sxw_content': sxw_data and base64.encodestring(sxw_data) or False,
82             'report_rml_content': rml_data and base64.encodestring(rml_data) or False
83         }
84
85 report_xml()
86
87 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
88