[11.0][MIG] onchange_helper + add tests
parent
4a381ad5bc
commit
fe77c12ac3
|
@ -1,5 +1,5 @@
|
||||||
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
|
.. image:: https://img.shields.io/badge/license-AGPL--3-blue.png
|
||||||
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
|
: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.
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# Copyright 2016-2017 Akretion (http://www.akretion.com)
|
||||||
# © 2016-2017 Akretion (http://www.akretion.com)
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
||||||
|
|
||||||
{'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'],
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# Copyright 2016-2017 Akretion (http://www.akretion.com)
|
||||||
# © 2016-2017 Akretion (http://www.akretion.com)
|
# Copyright 2016-2017 Camptocamp (http://www.camptocamp.com/)
|
||||||
# © 2016-2017 Camptocamp (http://www.camptocamp.com/)
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
||||||
|
|
||||||
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):
|
class Base(models.AbstractModel):
|
||||||
_inherit = 'ir.rule'
|
_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()
|
|
@ -0,0 +1,3 @@
|
||||||
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from . import test_onchange_helper
|
|
@ -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…
Reference in New Issue