Browse Source

[11.0][MIG] onchange_helper + add tests

pull/1461/head
Andrea 7 years ago
committed by sbejaoui
parent
commit
1c0b7b9971
  1. 10
      onchange_helper/README.rst
  2. 2
      onchange_helper/__init__.py
  3. 9
      onchange_helper/__manifest__.py
  4. 4
      onchange_helper/models/__init__.py
  5. 17
      onchange_helper/models/base.py
  6. 3
      onchange_helper/tests/__init__.py
  7. 28
      onchange_helper/tests/test_onchange_helper.py

10
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 :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) 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'])` `vals = self.env['sale.order'].play_onchanges(vals, ['partner_id'])`
@ -38,13 +38,14 @@ Credits
Images Images
------ ------
* Odoo Community Association: `Icon <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
* Odoo Community Association: `Icon <https://odoo-community.org/logo.png>`_.
Contributors Contributors
------------ ------------
* Guewen Baconnier <guewen.baconnier@camptocamp.com> * Guewen Baconnier <guewen.baconnier@camptocamp.com>
* Florian da Costa <florian.dacosta@akretion.com> * Florian da Costa <florian.dacosta@akretion.com>
* Andrea Stirpe <a.stirpe@onestein.nl>
Maintainer Maintainer
---------- ----------
@ -60,4 +61,3 @@ mission is to support the collaborative development of Odoo features and
promote its widespread use. promote its widespread use.
To contribute to this module, please visit https://odoo-community.org. To contribute to this module, please visit https://odoo-community.org.

2
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 from . import models

9
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', {'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', 'summary': 'Technical module that ease execution of onchange in Python code',
'author': 'Akretion,Camptocamp,Odoo Community Association (OCA)', 'author': 'Akretion,Camptocamp,Odoo Community Association (OCA)',
'website': 'http://www.akretion.com',
'website': 'https://github.com/OCA/server-tools',
'license': 'AGPL-3', 'license': 'AGPL-3',
'category': 'Generic Modules', 'category': 'Generic Modules',
'depends': ['base'], 'depends': ['base'],

4
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

17
onchange_helper/models/ir_rule.py → 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 from odoo import api, models
@ -9,7 +8,7 @@ from odoo import api, models
def get_new_values(model, record, on_change_result): def get_new_values(model, record, on_change_result):
vals = on_change_result.get('value', {}) vals = on_change_result.get('value', {})
new_values = {} new_values = {}
for fieldname, value in vals.iteritems():
for fieldname, value in vals.items():
if fieldname not in record: if fieldname not in record:
column = model._fields[fieldname] column = model._fields[fieldname]
if value and column.type == 'many2one': 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)) new_values.update(get_new_values(self, values, onchange_values))
all_values.update(new_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} if f in values or f in new_values}
return res return res
class IrRule(models.Model):
_inherit = 'ir.rule'
class Base(models.AbstractModel):
_inherit = 'base'
def _setup_complete(self): def _setup_complete(self):
if not hasattr(models.BaseModel, 'play_onchanges'): if not hasattr(models.BaseModel, 'play_onchanges'):
setattr(models.BaseModel, 'play_onchanges', play_onchanges) setattr(models.BaseModel, 'play_onchanges', play_onchanges)
return super(IrRule, self)._setup_complete()
return super(Base, self)._setup_complete()

3
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

28
onchange_helper/tests/test_onchange_helper.py

@ -0,0 +1,28 @@
# Copyright 2017 Onestein (<http://www.onestein.eu>)
# 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)
Loading…
Cancel
Save