From e490c27352b61070011fd09967d646fb305c9c01 Mon Sep 17 00:00:00 2001 From: Andrea Date: Sun, 14 Jan 2018 12:24:54 +0100 Subject: [PATCH] [11.0][MIG] onchange_helper + add tests --- onchange_helper/README.rst | 10 +++---- onchange_helper/__init__.py | 2 +- onchange_helper/__manifest__.py | 9 +++--- onchange_helper/models/__init__.py | 4 +-- .../models/{ir_rule.py => base.py} | 17 ++++++----- onchange_helper/tests/__init__.py | 3 ++ onchange_helper/tests/test_onchange_helper.py | 28 +++++++++++++++++++ 7 files changed, 51 insertions(+), 22 deletions(-) rename onchange_helper/models/{ir_rule.py => base.py} (77%) create mode 100644 onchange_helper/tests/__init__.py create mode 100644 onchange_helper/tests/test_onchange_helper.py diff --git a/onchange_helper/README.rst b/onchange_helper/README.rst index 48cca771e..622fbe8dd 100644 --- a/onchange_helper/README.rst +++ b/onchange_helper/README.rst @@ -1,5 +1,5 @@ -.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg - :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html +.. image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: https://www.gnu.org/licenses/agpl :alt: License: AGPL-3 =============== @@ -18,7 +18,7 @@ To use this module, you need to: Example if you want to create a sale order and you want to get the values relative to partner_id field (as if you fill the field from UI) - `vals = {'partner_id: 1'}` + `vals = {'partner_id': 1}` `vals = self.env['sale.order'].play_onchanges(vals, ['partner_id'])` @@ -38,13 +38,14 @@ Credits Images ------ -* Odoo Community Association: `Icon `_. +* Odoo Community Association: `Icon `_. Contributors ------------ * Guewen Baconnier * Florian da Costa +* Andrea Stirpe Maintainer ---------- @@ -60,4 +61,3 @@ mission is to support the collaborative development of Odoo features and promote its widespread use. To contribute to this module, please visit https://odoo-community.org. - diff --git a/onchange_helper/__init__.py b/onchange_helper/__init__.py index cde864bae..31660d6a9 100644 --- a/onchange_helper/__init__.py +++ b/onchange_helper/__init__.py @@ -1,3 +1,3 @@ -# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from . import models diff --git a/onchange_helper/__manifest__.py b/onchange_helper/__manifest__.py index 0d1b9b6a1..82f03bcc1 100644 --- a/onchange_helper/__manifest__.py +++ b/onchange_helper/__manifest__.py @@ -1,12 +1,11 @@ -# -*- coding: utf-8 -*- -# © 2016-2017 Akretion (http://www.akretion.com) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# Copyright 2016-2017 Akretion (http://www.akretion.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). {'name': 'Onchange Helper', - 'version': '10.0.1.0.0', + 'version': '11.0.1.0.0', 'summary': 'Technical module that ease execution of onchange in Python code', 'author': 'Akretion,Camptocamp,Odoo Community Association (OCA)', - 'website': 'http://www.akretion.com', + 'website': 'https://github.com/OCA/server-tools', 'license': 'AGPL-3', 'category': 'Generic Modules', 'depends': ['base'], diff --git a/onchange_helper/models/__init__.py b/onchange_helper/models/__init__.py index 0f0f860f3..08f5a5618 100644 --- a/onchange_helper/models/__init__.py +++ b/onchange_helper/models/__init__.py @@ -1,3 +1,3 @@ -# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from . import ir_rule +from . import base diff --git a/onchange_helper/models/ir_rule.py b/onchange_helper/models/base.py similarity index 77% rename from onchange_helper/models/ir_rule.py rename to onchange_helper/models/base.py index b4f67af16..fdc0457a3 100644 --- a/onchange_helper/models/ir_rule.py +++ b/onchange_helper/models/base.py @@ -1,7 +1,6 @@ -# -*- coding: utf-8 -*- -# © 2016-2017 Akretion (http://www.akretion.com) -# © 2016-2017 Camptocamp (http://www.camptocamp.com/) -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +# Copyright 2016-2017 Akretion (http://www.akretion.com) +# Copyright 2016-2017 Camptocamp (http://www.camptocamp.com/) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from odoo import api, models @@ -9,7 +8,7 @@ from odoo import api, models def get_new_values(model, record, on_change_result): vals = on_change_result.get('value', {}) new_values = {} - for fieldname, value in vals.iteritems(): + for fieldname, value in vals.items(): if fieldname not in record: column = model._fields[fieldname] if value and column.type == 'many2one': @@ -38,15 +37,15 @@ def play_onchanges(self, values, onchange_fields): new_values.update(get_new_values(self, values, onchange_values)) all_values.update(new_values) - res = {f: v for f, v in all_values.iteritems() + res = {f: v for f, v in all_values.items() if f in values or f in new_values} return res -class IrRule(models.Model): - _inherit = 'ir.rule' +class Base(models.AbstractModel): + _inherit = 'base' def _setup_complete(self): if not hasattr(models.BaseModel, 'play_onchanges'): setattr(models.BaseModel, 'play_onchanges', play_onchanges) - return super(IrRule, self)._setup_complete() + return super(Base, self)._setup_complete() diff --git a/onchange_helper/tests/__init__.py b/onchange_helper/tests/__init__.py new file mode 100644 index 000000000..806c1a0d6 --- /dev/null +++ b/onchange_helper/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_onchange_helper diff --git a/onchange_helper/tests/test_onchange_helper.py b/onchange_helper/tests/test_onchange_helper.py new file mode 100644 index 000000000..adcff2c1a --- /dev/null +++ b/onchange_helper/tests/test_onchange_helper.py @@ -0,0 +1,28 @@ +# Copyright 2017 Onestein () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestOnchangeHelper(TransactionCase): + + def test01_partner_parent(self): + main_partner = self.env.ref('base.main_partner') + input_vals = dict(partner_id=main_partner.id) + updated_vals = self.env['res.partner'].play_onchanges( + input_vals, + ['parent_id'] + ) + self.assertIn('commercial_partner_id', updated_vals) + self.assertIn('display_name', updated_vals) + self.assertIn('partner_id', updated_vals) + + def test02_partner_country(self): + partner_demo = self.env.ref('base.partner_demo') + input_vals = {'partner_id': partner_demo.id} + updated_vals = self.env['res.partner'].play_onchanges( + input_vals, + ['country_id'] + ) + self.assertIn('contact_address', updated_vals) + self.assertIn('partner_id', updated_vals)