~openerp-commiter/openobject-addons/trunk-extra-addons

5051.1.1 by Omar (Pexego)
[ADD] pxgo_openoffice_reports ported to 6.0
1
# -*- coding: utf-8 -*-
2
# -*- encoding: utf-8 -*-
3
##############################################################################
4
#
5
#    OpenOffice Reports
6
#    Copyright (C) 2009 Pexego Sistemas Informáticos. All Rights Reserved
7
#    $Id$
8
#
9
#    This program is free software: you can redistribute it and/or modify
10
#    it under the terms of the GNU General Public License as published by
11
#    the Free Software Foundation, either version 3 of the License, or
12
#    (at your option) any later version.
13
#
14
#    This program is distributed in the hope that it will be useful,
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
#    GNU General Public License for more details.
18
#
19
#    You should have received a copy of the GNU General Public License
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
#
22
##############################################################################
23
24
"""
25
Extends report_xml to add new report types to the report_type selection.
26
"""
27
__author__ = "Borja López Soilán (Pexego)"
28
29
import os
30
import base64
31
from osv import osv, fields
32
import openoffice_report
33
from tools.translate import _
34
35
class report_xml_file(osv.osv):
36
    _name = 'ir.actions.report.xml.file'
37
    _columns = {
38
        'file': fields.binary('File', required=True, filters="*.odt,*.pdf,*.html,*.doc,*.rtf,*.txt,*.ods,*.xls,*.csv,*.odp,*.ppt,*.swf", help=''),
39
        'filename': fields.char('File Name', size=256, required=False, help=''),
40
        'report_id': fields.many2one('ir.actions.report.xml', 'Report', required=True, ondelete='cascade', help=''),
41
        'default': fields.boolean('Default', help=''),
42
    }
43
    def create(self, cr, uid, vals, context=None):
44
        result = super(report_xml_file,self).create(cr, uid, vals, context)
45
        self.pool.get('ir.actions.report.xml').update(cr, uid, [vals['report_id']], context)
46
        return result
47
48
    def write(self, cr, uid, ids, vals, context=None):
49
        result = super(report_xml_file,self).write(cr, uid, ids, vals, context)
50
        for attachment in self.browse(cr, uid, ids, context):
51
            self.pool.get('ir.actions.report.xml').update(cr, uid, [attachment.report_id.id], context)
52
        return result
53
54
report_xml_file()
55
56
class report_xml(osv.osv):
57
    """
58
    Extends report_xml to add new report types to the report_type selection.
59
60
    You may declare reports of the new types like this
61
        <report id="report_REPORTNAME"
62
            ... />
63
        <record model="ir.actions.report.xml" id="report_REPORTNAME">
64
            <field name="report_type">oo-odt</field>
65
        </record>
66
    """
67
    
68
    _inherit = 'ir.actions.report.xml'
69
70
    _columns = {
71
        'report_type': fields.selection([
72
                ('pdf', 'pdf'),
73
                ('html', 'html'),
74
                ('raw', 'raw'),
75
                ('sxw', 'sxw'),
76
                ('odt', 'odt'),
77
                ('html2html','Html from html'),
78
                ('oo-pdf', 'OpenOffice - pdf output'),
79
                ('oo-html', 'OpenOffice - html output'),
80
                ('oo-odt', 'OpenOffice - odt output'),
81
                ('oo-doc', 'OpenOffice - doc output'),
82
                ('oo-rtf', 'OpenOffice - rtf output'),
83
                ('oo-txt', 'OpenOffice - txt output'),
84
                ('oo-ods', 'OpenOffice - ods output'),
85
                ('oo-xls', 'OpenOffice - xls output'),
86
                ('oo-csv', 'OpenOffice - csv output'),
87
                ('oo-odp', 'OpenOffice - odp output'),
88
                ('oo-ppt', 'OpenOffice - ppt output'),
89
                ('oo-swf', 'OpenOffice - swf output'),
90
            ], string='Type', required=True),
91
        'openoffice_file_ids': fields.one2many('ir.actions.report.xml.file', 'report_id', 'Files', help=''),
92
        'openoffice_model_id': fields.many2one('ir.model', 'Model', help=''),
93
        'openoffice_report': fields.boolean('Is OpenOffice Report?', help=''),
94
    }
95
96
    def create(self, cr, uid, vals, context=None):
97
        if context and context.get('openoffice_report'):
98
            vals['model'] = self.pool.get('ir.model').browse(cr, uid, vals['openoffice_model_id'], context).model
99
            vals['type'] = 'ir.actions.report.xml'
100
#            vals['report_type'] = 'pdf'
101
            vals['openoffice_report'] = True
102
            vals['header'] = False
103
104
        return super(report_xml, self).create(cr, uid, vals, context)
105
106
    def write(self, cr, uid, ids, vals, context=None):
107
        if context and context.get('openoffice_report'):
108
            if 'openoffice_model_id' in vals:
109
                vals['model'] = self.pool.get('ir.model').browse(cr, uid, vals['openoffice_model_id'], context).model
110
            vals['type'] = 'ir.actions.report.xml'
111
#            vals['report_type'] = 'pdf'
112
            vals['openoffice_report'] = True
113
            vals['header'] = False
114
115
        return super(report_xml, self).write(cr, uid, ids, vals, context)
116
117
    def unlink(self, cr, uid, ids, context=None):
118
        """Deletes ir_values register when you delete any ir.actions.report.xml"""
119
        if context is None: context = {}
120
        for report in self.browse(cr, uid, ids):
121
            values = self.pool.get('ir.values').search(cr, uid, [('value','=','ir.actions.report.xml,%s'% report.id)])
122
            if values:
123
                self.pool.get('ir.values').unlink(cr, uid, values)
124
125
        return super(report_xml, self).unlink(cr, uid, ids, context=context)
126
127
    def update(self, cr, uid, ids, context={}):
128
        """Browse attachments and store .odt into pxgo_openoffixe_reprots/custom_reports
129
        directory. Also add or update ir.values data so they're shown on model views."""
130
        for report in self.browse(cr, uid, ids):
131
            has_default = False
132
133
            for attachment in report.openoffice_file_ids:
134
                content = attachment.file
135
                fileName = attachment.filename
136
137
                if not fileName or not content:
138
                    continue
139
140
                path = self.save_file( fileName, content )
141
                for extension in ['.odt','.pdf','.html','.doc','.rtf','.txt','.ods','.xls','.csv','.odp','.ppt','.swf']:
142
                    if extension in fileName:
143
                        if attachment.default:
144
                            if has_default:
145
                                raise osv.except_osv(_('Error'), _('There is more than one report marked as default'))
146
147
                            has_default = True
148
                            # Update path into report_rml field.
149
                            self.write(cr, uid, [report.id], {
150
                                'report_rml': path
151
                            })
152
153
                            valuesId = self.pool.get('ir.values').search(cr, uid, [('value','=','ir.actions.report.xml,%s'% report.id)])
154
                            data = {
155
                                'name': report.name,
156
                                'model': report.model,
157
                                'key': 'action',
158
                                'object': True,
159
                                'key2': 'client_print_multi',
160
                                'value': 'ir.actions.report.xml,%s'% report.id
161
                            }
162
                            
163
                            if not valuesId:
164
                                valuesId = self.pool.get('ir.values').create(cr, uid, data, context=context)
165
                            else:
166
                                self.pool.get('ir.values').write(cr, uid, valuesId, data, context=context)
167
                                valuesId = valuesId[0]
168
169
            if not has_default:
170
                raise osv.except_osv(_('Error'), _('No report has been marked as default.'))
171
172
            # Ensure the report is registered so it can be used immediately
5051.1.2 by Omar (Pexego)
[FIX] register_openoffice_report not defined
173
            openoffice_report.openoffice_report( report.report_name, report.model )
5051.1.1 by Omar (Pexego)
[ADD] pxgo_openoffice_reports ported to 6.0
174
            
175
        return True
176
177
    def save_file(self, name, value):
178
        """save the file to pxgo_openoffice_reports/custom_reports"""
179
        path = os.path.abspath( os.path.dirname(__file__) )
180
        path += '/custom_reports/%s' % name
181
        f = open( path, 'wb+' )
182
        try:
183
            f.write( base64.decodestring( value ) )
184
        finally:
185
            f.close()
186
        path = 'pxgo_openoffice_reports/custom_reports/%s' % name
187
        return path
188
189
report_xml()