In the 'get_html' method, the report is using a custom model to render its html,
we should use its own 'render_html' method to generate the report. During the
module migration, we replaced
'report_model.render_html(cr, uid, ids, data=data, context=context)'
by
'report_model.render_html(data=data)'
The new API signature of 'get_html' is
@api.model
def get_html(self, docids, report_name, data=None):
That way, 'render_html' doesn't receive docids anymore when called. We should do instead
if report_model is not None:
return report_model.browse(docids).render_html(data=data)
As render_html is an api.multi method, this is all wrong too, because we'll send a
browse record with the custom report model to the render_html method, to use the
ids with 'self.ids' that are absolutely not corresponding to the report model but to
to the record model we want to print (an invoice for example).
In old API style we were sending a list of ids, but it's not correct to keep the same
behavior by sending a browse record with only the ids relevant. We have also to
change render_html method decorator to @api.model with a new signature
@api.model
def render_html(self, docids, data=None):
And replace the call to render_html by
if report_model is not None:
return report_model.render_html(docids, data=data)
In the case the report is printed from a wizard, this is sufficient because the ids
are taken from the active_ids in context, or in data.
In the case the report is printed from a simple action, we should replace the occurence
of 'self.ids' (which doesn't mean anything) to 'docids'
This fix doesn't solve the printing issues for the custom accounting reports, because
the method get_pdf has been reimplemented in the module account_reports.
50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, models
|
|
|
|
|
|
class AbstractReport(models.AbstractModel):
|
|
"""Model used to embed old style reports"""
|
|
_name = 'report.abstract_report'
|
|
_template = None
|
|
_wrapped_report_class = None
|
|
|
|
@api.model
|
|
def render_html(self, docids, data=None):
|
|
context = dict(self.env.context or {})
|
|
|
|
# If the key 'landscape' is present in data['form'], passing it into the context
|
|
if data and data.get('form', {}).get('landscape'):
|
|
context['landscape'] = True
|
|
|
|
if context and context.get('active_ids'):
|
|
# Browse the selected objects via their reference in context
|
|
model = context.get('active_model') or context.get('model')
|
|
objects_model = self.env[model]
|
|
objects = objects_model.with_context(context).browse(context['active_ids'])
|
|
else:
|
|
# If no context is set (for instance, during test execution), build one
|
|
model = self.env['report']._get_report_from_name(self._template).model
|
|
objects_model = self.env[model]
|
|
objects = objects_model.with_context(context).browse(docids)
|
|
context['active_model'] = model
|
|
context['active_ids'] = docids
|
|
|
|
# Generate the old style report
|
|
wrapped_report = self.with_context(context)._wrapped_report_class(self.env.cr, self.env.uid, '', context=self.env.context)
|
|
wrapped_report.set_context(objects, data, context['active_ids'])
|
|
|
|
# Rendering self._template with the wrapped report instance localcontext as
|
|
# rendering environment
|
|
docargs = dict(wrapped_report.localcontext)
|
|
if not docargs.get('lang'):
|
|
docargs.pop('lang', False)
|
|
docargs['docs'] = docargs.get('objects')
|
|
|
|
# Used in template translation (see translate_doc method from report model)
|
|
docargs['doc_ids'] = context['active_ids']
|
|
docargs['doc_model'] = model
|
|
|
|
return self.env['report'].with_context(context).render(self._template, docargs)
|