[IMP] report_webkit: some improvements including a demo report and a better default...
[odoo/odoo.git] / addons / report_webkit / ir_report.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2010 Camptocamp SA (http://www.camptocamp.com) 
5 # All Right Reserved
6 #
7 # Author : Nicolas Bessi (Camptocamp)
8 #
9 # WARNING: This program as such is intended to be used by professional
10 # programmers who take the whole responsability of assessing all potential
11 # consequences resulting from its eventual inadequacies and bugs
12 # End users who are looking for a ready-to-use solution with commercial
13 # garantees and support are strongly adviced to contract a Free Software
14 # Service Company
15 #
16 # This program is Free Software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29 #
30 ##############################################################################
31
32 from openerp.osv import fields, osv
33 import openerp.report.interface
34 from openerp.report.report_sxw import rml_parse
35
36 from webkit_report import WebKitParser
37
38 def register_report(name, model, tmpl_path, parser=rml_parse):
39     """Register the report into the services"""
40     name = 'report.%s' % name
41     if name in openerp.report.interface.report_int._reports:
42         service = openerp.report.interface.report_int._reports[name]
43         if isinstance(service, WebKitParser):
44             #already instantiated properly, skip it
45             return
46         if hasattr(service, 'parser'):
47             parser = service.parser
48         del openerp.report.interface.report_int._reports[name]
49     WebKitParser(name, model, tmpl_path, parser=parser)
50
51
52 class ReportXML(osv.osv):
53
54     def __init__(self, pool, cr):
55         super(ReportXML, self).__init__(pool, cr)
56
57     def register_all(self,cursor):
58         value = super(ReportXML, self).register_all(cursor)
59         cursor.execute("SELECT * FROM ir_act_report_xml WHERE report_type = 'webkit'")
60         records = cursor.dictfetchall()
61         for record in records:
62             register_report(record['report_name'], record['model'], record['report_rml'])
63         return value
64
65     def unlink(self, cursor, user, ids, context=None):
66         """Delete report and unregister it"""
67         trans_obj = self.pool.get('ir.translation')
68         trans_ids = trans_obj.search(
69             cursor,
70             user,
71             [('type', '=', 'report'), ('res_id', 'in', ids)]
72         )
73         trans_obj.unlink(cursor, user, trans_ids)
74
75         # Warning: we cannot unregister the services at the moment
76         # because they are shared across databases. Calling a deleted
77         # report will fail so it's ok.
78
79         res = super(ReportXML, self).unlink(
80                                             cursor,
81                                             user,
82                                             ids,
83                                             context
84                                         )
85         return res
86
87     def create(self, cursor, user, vals, context=None):
88         "Create report and register it"
89         res = super(ReportXML, self).create(cursor, user, vals, context)
90         if vals.get('report_type','') == 'webkit':
91             # I really look forward to virtual functions :S
92             register_report(
93                         vals['report_name'],
94                         vals['model'],
95                         vals.get('report_rml', False)
96                         )
97         return res
98
99     def write(self, cr, uid, ids, vals, context=None):
100         "Edit report and manage it registration"
101         if isinstance(ids, (int, long)):
102             ids = [ids,]
103         for rep in self.browse(cr, uid, ids, context=context):
104             if rep.report_type != 'webkit':
105                 continue
106             if vals.get('report_name', False) and \
107                 vals['report_name'] != rep.report_name:
108                 report_name = vals['report_name']
109             else:
110                 report_name = rep.report_name
111
112             register_report(
113                         report_name,
114                         vals.get('model', rep.model),
115                         vals.get('report_rml', rep.report_rml)
116                         )
117         res = super(ReportXML, self).write(cr, uid, ids, vals, context)
118         return res
119
120     _name = 'ir.actions.report.xml'
121     _inherit = 'ir.actions.report.xml'
122     _columns = {
123         'webkit_header':  fields.property(
124                                             'ir.header_webkit',
125                                             type='many2one',
126                                             relation='ir.header_webkit',
127                                             string='Webkit Header',
128                                             help="The header linked to the report",
129                                             view_load=True,
130                                             required=True
131                                         ),
132         'webkit_debug' : fields.boolean('Webkit debug', help="Enable the webkit engine debugger"),
133         'report_webkit_data': fields.text('Webkit Template', help="This template will be used if the main report file is not found"),
134         'precise_mode':fields.boolean('Precise Mode', help='This mode allow more precise element \
135                                                             position as each object is printed on a separate HTML.\
136                                                             but memory and disk usage is wider')
137     }
138
139 ReportXML()
140
141 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: