Skip to content

Commit

Permalink
[FIX] fix issue when creating binding from the main record
Browse files Browse the repository at this point in the history
Indeed the implementation of write on one2many (see odoo/fields.py:2235)
call the method create/write in mode norecompute
This mean that the field is_urls_sync_required is not computed after
the call to super of write/create of the binding but later during the
create/write of the main record.

To solve the issue we sync the url after calling the method
"_recompute_done".
This also simplify the code \o/
  • Loading branch information
sebastienbeau committed Apr 4, 2020
1 parent b81b921 commit 3f4407e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
18 changes: 5 additions & 13 deletions base_url/models/abstract_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,11 @@ def _sync_urls(self):
record.set_url(record.url_key)
return records

@api.model
def create(self, value):
res = super(AbstractUrl, self).create(value)
synced = res._sync_urls()
super(AbstractUrl, synced).write({"is_urls_sync_required": False})
return res

@api.multi
def write(self, value):
res = super(AbstractUrl, self).write(value)
synced = self._sync_urls()
super(AbstractUrl, synced).write({"is_urls_sync_required": False})
return res
def _recompute_done(self, field):
super(AbstractUrl, self)._recompute_done(field)
if field.name == "is_urls_sync_required":
synced = self.exists()._sync_urls()
synced.write({"is_urls_sync_required": False})

@api.multi
def unlink(self):
Expand Down
18 changes: 17 additions & 1 deletion base_url/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ class UrlBackendFake(models.Model, TestMixin):
name = fields.Char(required=True)


class ResPartner(models.Model, TestMixin):
_name = "res.partner"
_inherit = "res.partner"
_test_teardown_no_delete = True
_test_purge_fields = ["binding_ids"]

binding_ids = fields.One2many("res.partner.addressable.fake", "record_id")


class ResPartnerAddressableFake(models.Model, TestMixin):
_name = "res.partner.addressable.fake"
_inherit = "abstract.url"
_inherits = {"res.partner": "record_id"}
_description = "Fake partner addressable"

backend_id = fields.Many2one(comodel_name="url.backend.fake")
special_code = fields.Char()

@api.multi
@api.depends("lang_id", "record_id.name")
@api.depends("lang_id", "special_code", "record_id.name")
def _compute_automatic_url_key(self):
self._generic_compute_automatic_url_key()

def _get_url_keywords(self):
res = super(ResPartnerAddressableFake, self)._get_url_keywords()
if self.special_code:
res += [self.special_code]
return res
1 change: 1 addition & 0 deletions base_url/tests/models_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def _test_teardown_model(cls, env):
model = env[cls._name]
if fname in model:
model._pop_field(fname)
model._proper_fields.remove(fname)

if not getattr(cls, "_test_teardown_no_delete", False):
del env.registry.models[cls._name]
Expand Down
32 changes: 31 additions & 1 deletion base_url/tests/test_abstract_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from odoo.exceptions import ValidationError
from odoo.tests import SavepointCase

from .models import ResPartnerAddressableFake, UrlBackendFake
from .models import ResPartner, ResPartnerAddressableFake, UrlBackendFake


class TestAbstractUrl(SavepointCase):
Expand All @@ -14,6 +14,7 @@ def setUpClass(cls):
super(TestAbstractUrl, cls).setUpClass()
UrlBackendFake._test_setup_model(cls.env)
ResPartnerAddressableFake._test_setup_model(cls.env)
ResPartner._test_setup_model(cls.env)
cls.lang = cls.env.ref("base.lang_en")
cls.UrlUrl = cls.env["url.url"]
cls.ResPartnerAddressable = cls.env["res.partner.addressable.fake"]
Expand All @@ -27,6 +28,7 @@ def setUpClass(cls):
def tearDownClass(cls):
ResPartnerAddressableFake._test_teardown_model(cls.env)
UrlBackendFake._test_teardown_model(cls.env)
ResPartner._test_teardown_model(cls.env)
super(TestAbstractUrl, cls).tearDownClass()

def _get_default_partner_value(self):
Expand Down Expand Up @@ -125,3 +127,31 @@ def test_onchange_do_nothing(self):
{}, ["automatic_url_key"], {}
)
self.assertEqual(result, {"value": {}})

def test_create_from_main(self):
partner = self.env["res.partner"].create({"name": self.name})
partner.write(
{
"binding_ids": [
(
0,
0,
{
"lang_id": self.lang.id,
"url_builder": "auto",
"backend_id": self.url_backend.id,
},
)
]
}
)
self._check_url_key(partner.binding_ids, "partner-name")

def test_write_from_main(self):
my_partner = self._create_auto()
my_partner.record_id.write(
{"binding_ids": [(1, my_partner.id, {"special_code": "foo"})]}
)
self.assertEqual(2, len(my_partner.url_url_ids))
url_keys = set(my_partner.mapped("url_url_ids.url_key"))
self.assertSetEqual(url_keys, {"partner-name-foo", "partner-name"})

0 comments on commit 3f4407e

Please sign in to comment.