[FIX] report : translation of report using website editor

In the website editor, the translations are loaded using
the route 'get_view_translations', which returns the translations
of the templates loaded by the website (t-call calls)

The thing is, report templates use the 'translate_doc' method
to actually load the report, translated in the partner language,
and the templates loaded by this method are not seen by the website,
therefore, when calling 'get_view_translations', those report
templates were just ignored, thus their translations are not loaded.

This rev. injects the templates loaded with translate_doc
when rendering the report into the method 'customize_template_get'
(which is used by 'get_view_translations' to retrieve the loaded templates).

The translations of the reports are therefore now loaded corretly when
hitting the "translate" button in the website editor for reports.

Besides, this rev. has as (good) side-effect to add the template,
in the template selection input when editing using the HTML editor.

opw-620713
This commit is contained in:
Denis Ledoux 2015-02-18 16:20:26 +01:00
parent 184ed718a3
commit 0167acbb52
5 changed files with 109 additions and 22 deletions

View File

@ -89,6 +89,31 @@ class Report(osv.Model):
#--------------------------------------------------------------------------
# Extension of ir_ui_view.render with arguments frequently used in reports
#--------------------------------------------------------------------------
def translate_doc(self, cr, uid, doc_id, model, lang_field, template, values, context=None):
"""Helper used when a report should be translated into a specific lang.
<t t-foreach="doc_ids" t-as="doc_id">
<t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', account.report_invoice_document')"/>
</t>
:param doc_id: id of the record to translate
:param model: model of the record to translate
:param lang_field': field of the record containing the lang
:param template: name of the template to translate into the lang_field
"""
ctx = context.copy()
doc = self.pool[model].browse(cr, uid, doc_id, context=ctx)
qcontext = values.copy()
# Do not force-translate if we chose to display the report in a specific lang
if ctx.get('translatable') is True:
qcontext['o'] = doc
else:
# Reach the lang we want to translate the doc into
ctx['lang'] = eval('doc.%s' % lang_field, {'doc': doc})
qcontext['o'] = self.pool[model].browse(cr, uid, doc_id, context=ctx)
return self.pool['ir.ui.view'].render(cr, uid, template, qcontext, context=ctx)
def render(self, cr, uid, ids, template, values=None, context=None):
"""Allow to render a QWeb template python-side. This function returns the 'ir.ui.view'
render but embellish it with some variables/methods used in reports.
@ -107,28 +132,7 @@ class Report(osv.Model):
view_obj = self.pool['ir.ui.view']
def translate_doc(doc_id, model, lang_field, template):
"""Helper used when a report should be translated into a specific lang.
<t t-foreach="doc_ids" t-as="doc_id">
<t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', account.report_invoice_document')"/>
</t>
:param doc_id: id of the record to translate
:param model: model of the record to translate
:param lang_field': field of the record containing the lang
:param template: name of the template to translate into the lang_field
"""
ctx = context.copy()
doc = self.pool[model].browse(cr, uid, doc_id, context=ctx)
qcontext = values.copy()
# Do not force-translate if we chose to display the report in a specific lang
if ctx.get('translatable') is True:
qcontext['o'] = doc
else:
# Reach the lang we want to translate the doc into
ctx['lang'] = eval('doc.%s' % lang_field, {'doc': doc})
qcontext['o'] = self.pool[model].browse(cr, uid, doc_id, context=ctx)
return view_obj.render(cr, uid, template, qcontext, context=ctx)
return self.translate_doc(cr, uid, doc_id, model, lang_field, template, values, context=context)
user = self.pool['res.users'].browse(cr, uid, uid)
website = None

View File

@ -0,0 +1,2 @@
import controllers
import report

View File

@ -0,0 +1 @@
import main

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2014-Today OpenERP SA (<http://www.openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.addons.website.controllers.main import Website
from openerp.http import request, route
class Website(Website):
@route()
def customize_template_get(self, xml_id, full=False):
res = super(Website, self).customize_template_get(xml_id, full=full)
if full and request.session.get('report_view_ids'):
res += request.session['report_view_ids']
return res

View File

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2014-Today OpenERP SA (<http://www.openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.addons.web.http import request
from openerp.osv import osv
class Report(osv.Model):
_inherit = 'report'
def translate_doc(self, cr, uid, doc_id, model, lang_field, template, values, context=None):
if request and hasattr(request, 'website'):
if request.website is not None:
v = request.website.get_template(template)
request.session['report_view_ids'].append({
'name': v.name,
'id': v.id,
'xml_id': v.xml_id,
'inherit_id': v.inherit_id.id,
'header': False,
'active': v.active,
})
return super(Report, self).translate_doc(cr, uid, doc_id, model, lang_field, template, values, context=context)
def render(self, cr, uid, ids, template, values=None, context=None):
if request and hasattr(request, 'website'):
if request.website is not None:
request.session['report_view_ids'] = []
return super(Report, self).render(cr, uid, ids, template, values=values, context=context)