Browse Source

Merge PR #1119 into 14.0

Signed-off-by pedrobaeza
14.0
OCA-git-bot 3 years ago
parent
commit
dfc35d3ab0
  1. 2
      base_location/models/res_city_zip.py
  2. 21
      base_location/models/res_company.py
  3. 65
      base_location/models/res_partner.py
  4. 4
      base_location/tests/test_base_location.py
  5. 2
      base_location/views/res_partner_view.xml

2
base_location/models/res_city_zip.py

@ -22,6 +22,8 @@ class ResCityZip(models.Model):
ondelete="cascade",
index=True,
)
state_id = fields.Many2one(related="city_id.state_id")
country_id = fields.Many2one(related="city_id.country_id")
display_name = fields.Char(
compute="_compute_new_display_name", store=True, index=True
)

21
base_location/models/res_company.py

@ -33,7 +33,6 @@ class ResCompany(models.Model):
inverse="_inverse_zip_id",
help="Use the city name or the zip code to search the location",
)
country_enforce_cities = fields.Boolean(
related="partner_id.country_id.enforce_cities"
)
@ -48,24 +47,20 @@ class ResCompany(models.Model):
return res
def _inverse_city_id(self):
for company in self:
company.with_context(
skip_check_zip=True
).partner_id.city_id = company.city_id
for company in self.with_context(skip_check_zip=True):
company.partner_id.city_id = company.city_id
def _inverse_zip_id(self):
for company in self:
company.with_context(skip_check_zip=True).partner_id.zip_id = company.zip_id
for company in self.with_context(skip_check_zip=True):
company.partner_id.zip_id = company.zip_id
def _inverse_state(self):
return super(
ResCompany, self.with_context(skip_check_zip=True)
)._inverse_state()
self = self.with_context(skip_check_zip=True)
return super(ResCompany, self)._inverse_state()
def _inverse_country(self):
return super(
ResCompany, self.with_context(skip_check_zip=True)
)._inverse_country()
self = self.with_context(skip_check_zip=True)
return super(ResCompany, self)._inverse_country()
@api.onchange("zip_id")
def _onchange_zip_id(self):

65
base_location/models/res_partner.py

@ -2,6 +2,8 @@
# Copyright 2018 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from lxml import etree
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
@ -16,9 +18,6 @@ class ResPartner(models.Model):
compute="_compute_zip_id",
readonly=False,
store=True,
domain="[('city_id', '=?', city_id), "
"('city_id.country_id', '=?', country_id), "
"('city_id.state_id', '=?', state_id)]",
)
city_id = fields.Many2one(
index=True, # add index for performance
@ -33,17 +32,24 @@ class ResPartner(models.Model):
)
state_id = fields.Many2one(compute="_compute_state_id", readonly=False, store=True)
@api.depends("state_id", "country_id")
@api.depends("state_id", "country_id", "city_id", "zip")
def _compute_zip_id(self):
"""Empty the zip auto-completion field if data mismatch when on UI."""
for record in self.filtered("zip_id"):
for field in ["state_id", "country_id"]:
fields_map = {
"zip": "name",
"city_id": "city_id",
"state_id": "state_id",
"country_id": "country_id",
}
for rec_field, zip_field in fields_map.items():
if (
record[field]
and record[field] != record._origin[field]
and record[field] != record.zip_id.city_id[field]
record[rec_field]
and record[rec_field] != record._origin[rec_field]
and record[rec_field] != record.zip_id[zip_field]
):
record.zip_id = False
break
@api.depends("zip_id")
def _compute_city_id(self):
@ -90,28 +96,53 @@ class ResPartner(models.Model):
if state and record.state_id != state:
record.state_id = record.zip_id.city_id.state_id
@api.constrains("zip_id", "country_id", "city_id", "state_id")
@api.constrains("zip_id", "country_id", "city_id", "state_id", "zip")
def _check_zip(self):
if self.env.context.get("skip_check_zip"):
return
for rec in self:
if not rec.zip_id:
continue
if rec.zip_id.city_id.state_id != rec.state_id:
if rec.zip_id.city_id.country_id != rec.country_id:
raise ValidationError(
_("The state of the partner %s differs from that in " "location %s")
_("The country of the partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
if rec.zip_id.city_id.country_id != rec.country_id:
if rec.zip_id.city_id.state_id != rec.state_id:
raise ValidationError(
_(
"The country of the partner %s differs from that in "
"location %s"
)
_("The state of the partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
if rec.zip_id.city_id != rec.city_id:
raise ValidationError(
_("The city of partner %s differs from that in " "location %s")
_("The city of partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
if rec.zip_id.name != rec.zip:
raise ValidationError(
_("The zip of the partner %s differs from that in location %s")
% (rec.name, rec.zip_id.name)
)
def _zip_id_domain(self):
return """
[
("city_id", "=?", city_id),
("city_id.country_id", "=?", country_id),
("city_id.state_id", "=?", state_id),
]
"""
@api.model
def _fields_view_get_address(self, arch):
# We want to use a domain that requires city_id to be on the view
# but we can't add it directly there, otherwise _fields_view_get_address
# in base_address_city won't do its magic, as it immediately returns
# if city_id is already in there. On the other hand, if city_id is not in the
# views, odoo won't let us use it in zip_id's domain.
# For this reason we need to set the domain here.
arch = super()._fields_view_get_address(arch)
doc = etree.fromstring(arch)
for node in doc.xpath("//field[@name='zip_id']"):
node.attrib["domain"] = self._zip_id_domain()
return etree.tostring(doc, encoding="unicode")

4
base_location/tests/test_base_location.py

@ -122,6 +122,10 @@ class TestBaseLocation(common.SavepointCase):
self.partner_obj.create(
{"name": "P1", "zip_id": self.barcelona.id, "city_id": False}
)
with self.assertRaises(ValidationError):
self.partner_obj.create(
{"name": "P1", "zip_id": self.barcelona.id, "zip": False}
)
def test_writing_company(self):
self.company.zip_id = self.barcelona

2
base_location/views/res_partner_view.xml

@ -6,7 +6,6 @@
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="city" position="before">
<field name="city_id" invisible="1" />
<field
name="zip_id"
options="{'create_name_field': 'city', 'no_open': True, 'no_create': True}"
@ -19,7 +18,6 @@
expr="//field[@name='child_ids']/form//field[@name='city']"
position="before"
>
<field name="city_id" invisible="1" />
<field
name="zip_id"
options="{'create_name_field': 'city', 'no_open': True, 'no_create': True}"

Loading…
Cancel
Save