Browse Source

Merge PR #1669 into 10.0

Signed-off-by sebastienbeau
10.0
OCA-git-bot 5 years ago
parent
commit
bd13502d21
  1. 9
      onchange_helper/models/models.py
  2. 20
      test_onchange_helper/tests/test_onchange_helper.py

9
onchange_helper/models/models.py

@ -128,6 +128,7 @@ class Base(models.AbstractModel):
This method reimplement the onchange method to be able to work on the This method reimplement the onchange method to be able to work on the
current recordset if provided. current recordset if provided.
""" """
updated_values = values.copy()
env = self.env env = self.env
if self: if self:
self.ensure_one() self.ensure_one()
@ -197,6 +198,14 @@ class Base(models.AbstractModel):
dirty |= set(dirties) dirty |= set(dirties)
todo.extend(dirties) todo.extend(dirties)
# preserve values to update since these are the one selected
# by the user.
for f in dirties:
field = self._fields[f]
if (f in updated_values and
field.type not in ("one2many", "many2many")):
record[f] = values[f]
# prepare the result to return a dictionary with the new values for # prepare the result to return a dictionary with the new values for
# the dirty fields # the dirty fields
result = {} result = {}

20
test_onchange_helper/tests/test_onchange_helper.py

@ -13,6 +13,7 @@ class TestOnchangeHelper(common.TransactionCase):
self.Category = self.env["test_onchange_helper.category"] self.Category = self.env["test_onchange_helper.category"]
self.Message = self.env["test_onchange_helper.message"] self.Message = self.env["test_onchange_helper.message"]
self.Discussion = self.env["test_onchange_helper.discussion"] self.Discussion = self.env["test_onchange_helper.discussion"]
self.maxDiff = None
@contextmanager @contextmanager
def assertNoOrmWrite(self, model): def assertNoOrmWrite(self, model):
@ -38,7 +39,7 @@ class TestOnchangeHelper(common.TransactionCase):
def test_play_onchanges_many2one_new_record(self): def test_play_onchanges_many2one_new_record(self):
root = self.Category.create({"name": "root"}) root = self.Category.create({"name": "root"})
values = {"name": "test", "parent": root.id, "root_categ": False}
values = {"name": "test", "parent": root.id}
self.env.invalidate_all() self.env.invalidate_all()
with self.assertNoOrmWrite(self.Category): with self.assertNoOrmWrite(self.Category):
@ -46,20 +47,10 @@ class TestOnchangeHelper(common.TransactionCase):
self.assertIn("root_categ", result) self.assertIn("root_categ", result)
self.assertEqual(result["root_categ"], root.id) self.assertEqual(result["root_categ"], root.id)
values.update(result)
values["parent"] = False
self.env.invalidate_all()
with self.assertNoOrmWrite(self.Category):
result = self.Category.play_onchanges(values, "parent")
# since the root_categ is already False into values the field is not
# changed by the onchange
self.assertNotIn("root_categ", result)
def test_play_onchanges_many2one_existing_record(self): def test_play_onchanges_many2one_existing_record(self):
root = self.Category.create({"name": "root"}) root = self.Category.create({"name": "root"})
values = {"name": "test", "parent": root.id, "root_categ": False}
values = {"name": "test", "parent": root.id}
self.env.invalidate_all() self.env.invalidate_all()
with self.assertNoOrmWrite(self.Category): with self.assertNoOrmWrite(self.Category):
@ -275,15 +266,13 @@ class TestOnchangeHelper(common.TransactionCase):
values = { values = {
"name": discussion.name, "name": discussion.name,
"moderator": demo.id, "moderator": demo.id,
"categories": [(4, cat.id) for cat in discussion.categories],
"messages": [(4, msg.id) for msg in discussion.messages],
"participants": [(4, usr.id) for usr in discussion.participants],
} }
self.env.invalidate_all() self.env.invalidate_all()
with self.assertNoOrmWrite(discussion): with self.assertNoOrmWrite(discussion):
result = discussion.play_onchanges(values, "moderator") result = discussion.play_onchanges(values, "moderator")
self.assertIn("participants", result) self.assertIn("participants", result)
self.assertTrue(discussion.participants)
self.assertItemsEqual( self.assertItemsEqual(
result["participants"], result["participants"],
[(5,)] + [(4, user.id) for user in discussion.participants + demo], [(5,)] + [(4, user.id) for user in discussion.participants + demo],
@ -304,7 +293,6 @@ class TestOnchangeHelper(common.TransactionCase):
"categories": [(4, cat.id) for cat in discussion.categories], "categories": [(4, cat.id) for cat in discussion.categories],
"messages": messages, "messages": messages,
"participants": [(4, usr.id) for usr in discussion.participants], "participants": [(4, usr.id) for usr in discussion.participants],
"message_concat": False,
} }
with self.assertNoOrmWrite(discussion): with self.assertNoOrmWrite(discussion):
result = discussion.play_onchanges(values, "messages") result = discussion.play_onchanges(values, "messages")

Loading…
Cancel
Save