[FIX] wall widget: fixed variable not refreshed
[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 osv import osv, fields
33 import netsvc
34 from webkit_report import WebKitParser
35 from report.report_sxw import rml_parse
36
37 def register_report(name, model, tmpl_path, parser=rml_parse):
38     "Register the report into the services"
39     name = 'report.%s' % name
40     if netsvc.Service._services.get(name, False):
41         service = netsvc.Service._services[name]
42         if isinstance(service, WebKitParser):
43             #already instantiated properly, skip it
44             return
45         if hasattr(service, 'parser'):
46             parser = service.parser
47         del netsvc.Service._services[name]
48     WebKitParser(name, model, tmpl_path, parser=parser)
49
50
51 class ReportXML(osv.osv):
52
53     def __init__(self, pool, cr):
54         super(ReportXML, self).__init__(pool, cr)
55
56     def register_all(self,cursor):
57         value = super(ReportXML, self).register_all(cursor)
58         cursor.execute("SELECT * FROM ir_act_report_xml WHERE report_type = 'webkit'")
59         records = cursor.dictfetchall()
60         for record in records:
61             register_report(record['report_name'], record['model'], record['report_rml'])
62         return value
63
64     def unlink(self, cursor, user, ids, context=None):
65         """Delete report and unregister it"""
66         trans_obj = self.pool.get('ir.translation')
67         trans_ids = trans_obj.search(
68             cursor,
69             user,
70             [('type', '=', 'report'), ('res_id', 'in', ids)]
71         )
72         trans_obj.unlink(cursor, user, trans_ids)
73
74         # Warning: we cannot unregister the services at the moment
75         # because they are shared across databases. Calling a deleted
76         # report will fail so it's ok.
77
78         res = super(ReportXML, self).unlink(
79                                             cursor,
80                                             user,
81                                             ids,
82                                             context
83                                         )
84         return res
85
86     def create(self, cursor, user, vals, context=None):
87         "Create report and register it"
88         res = super(ReportXML, self).create(cursor, user, vals, context)
89         if vals.get('report_type','') == 'webkit':
90             # I really look forward to virtual functions :S
91             register_report(
92                         vals['report_name'],
93                         vals['model'],
94                         vals.get('report_rml', False)
95                         )
96         return res
97
98     def write(self, cr, uid, ids, vals, context=None):
99         "Edit report and manage it registration"
100         if isinstance(ids, (int, long)):
101             ids = [ids,]
102         for rep in self.browse(cr, uid, ids, context=context):
103             if rep.report_type != 'webkit':
104                 continue
105             if vals.get('report_name', False) and \
106                 vals['report_name'] != rep.report_name:
107                 report_name = vals['report_name']
108             else:
109                 report_name = rep.report_name
110
111             register_report(
112                         report_name,
113                         vals.get('model', rep.model),
114                         vals.get('report_rml', rep.report_rml)
115                         )
116         res = super(ReportXML, self).write(cr, uid, ids, vals, context)
117         return res
118
119     _name = 'ir.actions.report.xml'
120     _inherit = 'ir.actions.report.xml'
121     _columns = {
122         'webkit_header':  fields.property(
123                                             'ir.header_webkit',
124                                             type='many2one',
125                                             relation='ir.header_webkit',
126                                             string='Webkit Header',
127                                             help="The header linked to the report",
128                                             view_load=True,
129                                             required=True
130                                         ),
131         'webkit_debug' : fields.boolean('Webkit debug', help="Enable the webkit engine debugger"),
132         'report_webkit_data': fields.text('Webkit Template', help="This template will be used if the main report file is not found"),
133         'precise_mode':fields.boolean('Precise Mode', help='This mode allow more precise element \
134                                                             position as each object is printed on a separate HTML.\
135                                                             but memory and disk usage is wider')
136     }
137
138 ReportXML()
139
140 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: