Skip to content

Commit

Permalink
[MIG] partner_country_lang: Migration to 17.0
Browse files Browse the repository at this point in the history
TT54939
  • Loading branch information
victoralmau committed Feb 5, 2025
1 parent cbb10dc commit 8697a6f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 41 deletions.
2 changes: 1 addition & 1 deletion partner_country_lang/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Partner language according country",
"version": "15.0.1.0.0",
"version": "17.0.1.0.0",
"category": "Customer Relationship Management",
"website": "https://github.com/OCA/partner-contact",
"author": "Tecnativa, Odoo Community Association (OCA)",
Expand Down
39 changes: 7 additions & 32 deletions partner_country_lang/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
# Copyright 2022 Tecnativa - Víctor Martínez
# Copyright 2022-2025 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models
from odoo import api, fields, models


class ResPartner(models.Model):
_inherit = "res.partner"

@api.onchange("country_id")
def _onchange_country_id(self):
result = super()._onchange_country_id()
if self.country_id.lang:
self.lang = self.country_id.lang
return result
lang = fields.Selection(compute="_compute_lang")

def _adjust_lang_by_country(self, vals):
"""Adjust vals dictionary for adding the language of the country
if no one is included in it, but country is.
"""
if vals.get("country_id") and "lang" not in vals:
country = self.env["res.country"].browse(vals["country_id"])
if country.lang:
vals["lang"] = country.lang

@api.model_create_multi
def create(self, vals_list):
"""Add lang if the partner is created with a country through code
and no language is specified.
"""
for vals in vals_list:
self._adjust_lang_by_country(vals)
return super().create(vals_list)

def write(self, vals):
"""Change language if a country is written through code and no
one is modified as well.
"""
self._adjust_lang_by_country(vals)
return super().write(vals)
@api.depends("country_id")
def _compute_lang(self):
for item in self:
item.lang = item.country_id.lang or item.lang
11 changes: 3 additions & 8 deletions partner_country_lang/tests/test_partner_country_lang.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2022 Tecnativa - Víctor Martínez
# Copyright 2022-2025 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests import Form, TransactionCase
from odoo.addons.base.tests.common import BaseCommon


class TestPartnerCountryLang(TransactionCase):
class TestPartnerCountryLang(BaseCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
Expand All @@ -14,11 +14,6 @@ def setUpClass(cls):
cls.country_fr.lang = cls.lang_fr.code
cls.partner = cls.env["res.partner"].create({"name": "Mr Odoo"})

def test_partner_onchange(self):
partner_form = Form(self.partner)
partner_form.country_id = self.country_fr
self.assertEqual(partner_form.lang, self.lang_fr.code)

def test_partner_create(self):
partner = self.env["res.partner"].create(
{"name": "Mrs Odoo", "country_id": self.country_fr.id}
Expand Down

0 comments on commit 8697a6f

Please sign in to comment.