[IMP] report_context: black, isort, prettier

pull/783/head
Jaime Arroyo 2020-09-30 12:06:33 +02:00 committed by AlexPForgeFlow
parent 7155d0c982
commit a5057c020b
7 changed files with 77 additions and 72 deletions

View File

@ -2,18 +2,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Report Context',
'summary': """
"name": "Report Context",
"summary": """
Adding context to reports""",
'version': '12.0.1.0.0',
'license': 'AGPL-3',
'author': 'Creu Blanca,Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/reporting-engine',
'depends': [
'web',
],
'data': [
'views/ir_actions_report.xml',
'data/config_parameter.xml',
],
"version": "12.0.1.0.0",
"license": "AGPL-3",
"author": "Creu Blanca,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/reporting-engine",
"depends": ["web"],
"data": ["views/ir_actions_report.xml", "data/config_parameter.xml"],
}

View File

@ -1,18 +1,20 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.addons.web.controllers import main as report
from odoo.http import route, request
import json
from odoo.http import request, route
from odoo.addons.web.controllers import main as report
class ReportController(report.ReportController):
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
report = request.env['ir.actions.report']._get_report_from_name(
reportname)
original_context = json.loads(data.get('context', '{}'))
data['context'] = json.dumps(report.with_context(
original_context
)._get_context())
report = request.env["ir.actions.report"]._get_report_from_name(reportname)
original_context = json.loads(data.get("context", "{}"))
data["context"] = json.dumps(
report.with_context(original_context)._get_context()
)
return super().report_routes(
reportname, docids=docids, converter=converter, **data)
reportname, docids=docids, converter=converter, **data
)

View File

@ -1,8 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data noupdate="1">
<record id="report_default_context"
model="ir.config_parameter" forcecreate="True">
<record
id="report_default_context"
model="ir.config_parameter"
forcecreate="True"
>
<field name="key">report.default.context</field>
<field name="value">{}</field>
</record>

View File

@ -1,36 +1,42 @@
# Copyright 2019 Creu Blanca
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tools.safe_eval import safe_eval
from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval
class IrActionsReport(models.Model):
_inherit = 'ir.actions.report'
_inherit = "ir.actions.report"
context = fields.Char(
string='Context Value', default={},
string="Context Value",
default={},
required=True,
help="Context dictionary as Python expression, empty by default "
"(Default: {})")
"(Default: {})",
)
def _get_context(self):
self.ensure_one()
context = self.env['ir.config_parameter'].sudo().get_param(
'report.default.context', '{}')
context = (
self.env["ir.config_parameter"]
.sudo()
.get_param("report.default.context", "{}")
)
# We must transform it to a dictionary
context = safe_eval(context or '{}')
report_context = safe_eval(self.context or '{}')
context = safe_eval(context or "{}")
report_context = safe_eval(self.context or "{}")
context.update(report_context)
context.update(self.env.context)
return context
@api.multi
def render(self, res_ids, data=None):
return super(IrActionsReport, self.with_context(
self._get_context())).render(res_ids, data=data)
return super(IrActionsReport, self.with_context(self._get_context())).render(
res_ids, data=data
)
@api.noguess
def report_action(self, docids, data=None, config=True):
return super(IrActionsReport, self.with_context(
self._get_context()
)).report_action(docids, data=data, config=config)
return super(
IrActionsReport, self.with_context(self._get_context())
).report_action(docids, data=data, config=config)

View File

@ -15,4 +15,3 @@ It can also be added on the developer side using::
<record id="model_name.report_id" model="ir.actions.report">
<field name="context">YOUR CONTEXT HERE</field>
</record>

View File

@ -4,44 +4,47 @@ from odoo.tests.common import TransactionCase
class TestReportContext(TransactionCase):
def test_report_01(self):
company = self.browse_ref('base.main_company')
report = self.browse_ref('web.action_report_internalpreview')
self.env['ir.config_parameter'].sudo().set_param(
'report.default.context', '{"test_parameter": 1}')
report.write({'context': '{"test_parameter": 2}'})
company = self.browse_ref("base.main_company")
report = self.browse_ref("web.action_report_internalpreview")
self.env["ir.config_parameter"].sudo().set_param(
"report.default.context", '{"test_parameter": 1}'
)
report.write({"context": '{"test_parameter": 2}'})
action = report.with_context(test_parameter=3).report_action(company)
self.assertEqual(3, action['context']['test_parameter'])
self.assertEqual(3, action["context"]["test_parameter"])
def test_report_02(self):
company = self.browse_ref('base.main_company')
report = self.browse_ref('web.action_report_internalpreview')
self.env['ir.config_parameter'].sudo().set_param(
'report.default.context', '{"test_parameter": 1}')
report.write({'context': '{"test_parameter": 2}'})
company = self.browse_ref("base.main_company")
report = self.browse_ref("web.action_report_internalpreview")
self.env["ir.config_parameter"].sudo().set_param(
"report.default.context", '{"test_parameter": 1}'
)
report.write({"context": '{"test_parameter": 2}'})
action = report.report_action(company)
self.assertEqual(2, action['context']['test_parameter'])
self.assertEqual(2, action["context"]["test_parameter"])
def test_report_03(self):
company = self.browse_ref('base.main_company')
report = self.browse_ref('web.action_report_internalpreview')
self.env['ir.config_parameter'].sudo().set_param(
'report.default.context', '{"test_parameter": 1}')
company = self.browse_ref("base.main_company")
report = self.browse_ref("web.action_report_internalpreview")
self.env["ir.config_parameter"].sudo().set_param(
"report.default.context", '{"test_parameter": 1}'
)
action = report.with_context(test_parameter=3).report_action(company)
self.assertEqual(3, action['context']['test_parameter'])
self.assertEqual(3, action["context"]["test_parameter"])
def test_report_04(self):
company = self.browse_ref('base.main_company')
report = self.browse_ref('web.action_report_internalpreview')
report.write({'context': '{"test_parameter": 2}'})
company = self.browse_ref("base.main_company")
report = self.browse_ref("web.action_report_internalpreview")
report.write({"context": '{"test_parameter": 2}'})
action = report.report_action(company)
self.assertEqual(2, action['context']['test_parameter'])
self.assertEqual(2, action["context"]["test_parameter"])
def test_report_05(self):
company = self.browse_ref('base.main_company')
report = self.browse_ref('web.action_report_internalpreview')
self.env['ir.config_parameter'].sudo().set_param(
'report.default.context', '{"test_parameter": 1}')
company = self.browse_ref("base.main_company")
report = self.browse_ref("web.action_report_internalpreview")
self.env["ir.config_parameter"].sudo().set_param(
"report.default.context", '{"test_parameter": 1}'
)
action = report.report_action(company)
self.assertEqual(1, action['context']['test_parameter'])
self.assertEqual(1, action["context"]["test_parameter"])

View File

@ -1,18 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2019 Creu Blanca
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.ui.view" id="act_report_xml_view">
<field name="name">ir.actions.report.context.form</field>
<field name="model">ir.actions.report</field>
<field name="inherit_id" ref="base.act_report_xml_view"/>
<field name="inherit_id" ref="base.act_report_xml_view" />
<field name="arch" type="xml">
<field name="print_report_name" position="after">
<field name="context"/>
<field name="context" />
</field>
</field>
</record>
</odoo>