odoo/addons/report_webkit/ir_report.py

140 lines
5.5 KiB
Python

# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2010 Camptocamp SA (http://www.camptocamp.com)
# All Right Reserved
#
# Author : Nicolas Bessi (Camptocamp)
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from osv import osv, fields
import netsvc
from webkit_report import WebKitParser
from report.report_sxw import rml_parse
import base64
import tools
from tools.translate import _
import os
def register_report(name, model, tmpl_path, parser):
"Register the report into the services"
name = 'report.%s' % name
if netsvc.Service._services.get(name, False):
del netsvc.Service._services[name]
WebKitParser(name, model, tmpl_path, parser=parser)
class ReportXML(osv.osv):
def __init__(self, pool, cr):
super(ReportXML, self).__init__(pool, cr)
def register_all(self,cursor):
value = super(ReportXML, self).register_all(cursor)
cursor.execute("SELECT * FROM ir_act_report_xml")
records = cursor.dictfetchall()
for record in records:
if record['report_type'] == 'webkit':
parser=rml_parse
register_report(record['report_name'], record['model'], record['report_rml'], parser)
return value
def unlink(self, cursor, user, ids, context=None):
"""Delete report and unregister it"""
records = self.read(cursor, user, ids)
trans_obj = self.pool.get('ir.translation')
trans_ids = trans_obj.search(
cursor,
user,
[('type', '=', 'report'), ('res_id', 'in', ids)]
)
trans_obj.unlink(cursor, user, trans_ids)
# Warning: we cannot unregister the services at the moment
# because they are shared across databases. Calling a deleted
# report will fail so it's ok.
records = None
res = super(ReportXML, self).unlink(
cursor,
user,
ids,
context
)
return res
def create(self, cursor, user, vals, context=None):
"Create report and register it"
res = super(ReportXML, self).create(cursor, user, vals, context)
parser=rml_parse
register_report(
vals['report_name'],
vals['model'],
vals.get('report_rml', False),
parser
)
return res
def write(self, cursor, user, ids, vals, context=None):
"Edit report and manage it registration"
parser=rml_parse
record = self.read(cursor, user, ids)
if isinstance(record, list) :
record = record[0]
if vals.get('report_name', False) and \
vals['report_name']!=record['report_name']:
delete_report_service(record['report_name'])
report_name = vals['report_name']
else:
report_name = record['report_name']
register_report(
report_name,
vals.get('model', record['model']),
vals.get('report_rml', record['report_rml']),
parser
)
res = super(ReportXML, self).write(cursor, user, ids, vals, context)
return res
_name = 'ir.actions.report.xml'
_inherit = 'ir.actions.report.xml'
_columns = {
'webkit_header': fields.property(
'ir.header_webkit',
type='many2one',
relation='ir.header_webkit',
string='WebKit Header',
help="The header linked to the report",
method=True,
view_load=True,
required=True
),
'webkit_debug' : fields.boolean('Webkit debug', help="Enable the webkit engine debugger"),
'report_webkit_data': fields.text('Webkit Template', help="This template will be used if the main report file is not found"),
}
ReportXML()