Skip to content

Commit

Permalink
[MIG] l10n_fr_department to v18
Browse files Browse the repository at this point in the history
Improve name of some variables
Change unicity constraint from unique(code) to unique(country_id, code) on res.country.department
  • Loading branch information
alexis-via committed Nov 26, 2024
1 parent 6478830 commit f095a72
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 56 deletions.
21 changes: 0 additions & 21 deletions l10n_fr_department/migrations/16.0.1.0.0/pre-migration.py

This file was deleted.

28 changes: 5 additions & 23 deletions l10n_fr_department/model/res_country_department.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ class ResCountryDepartment(models.Model):
_description = "Department"
_name = "res.country.department"
_order = "country_id, code"
_rec_names_search = ["name", "code"]

state_id = fields.Many2one(
"res.country.state",
string="State",
required=True,
help="State related to the current department",
)
country_id = fields.Many2one(
"res.country",
related="state_id.country_id",
string="Country",
store=True,
help="Country of the related state",
)
name = fields.Char(string="Department Name", size=128, required=True)
code = fields.Char(
Expand All @@ -35,28 +32,13 @@ class ResCountryDepartment(models.Model):

_sql_constraints = [
(
"code_uniq",
"unique (code)",
"You cannot have two departments with the same code!",
"code_country_uniq",
"unique (code, country_id)",
"You cannot have two departments with the same code in the same country!",
)
]

@api.depends("name", "code")
def _compute_display_name(self):
for rec in self:
dname = f"{rec.name} ({rec.code})"
rec.display_name = dname

@api.model
def _name_search(self, name, domain=None, operator="ilike", limit=None, order=None):
if domain is None:
domain = []
if name and operator == "ilike":
ids = list(
self._search([("code", "=", name)] + domain, limit=limit, order=order)
)
if ids:
return ids
return super()._name_search(
name, domain=domain, operator=operator, limit=limit, order=order
)
rec.display_name = f"{rec.name} ({rec.code})"
10 changes: 5 additions & 5 deletions l10n_fr_department/model/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class ResPartner(models.Model):
# If a department code changes, it will have to be manually recomputed
def _compute_country_department(self):
def _get_zipcode(partner) -> str:
if partner.country_id not in fr_countries:
if partner.country_id not in fr_dom_countries:
return ""
partner_zip = partner.zip
if not partner_zip:
return ""
partner_zip = partner_zip.strip().replace(" ", "").rjust(5, "0")
return partner_zip if len(partner_zip) == 5 else ""

fr_countries_codes = ("FR", "GP", "MQ", "GF", "RE", "YT")
fr_countries_domain = [("code", "in", fr_countries_codes)]
fr_countries = self.env["res.country"].search(fr_countries_domain)
fr_dom_countries_codes = ("FR", "GP", "MQ", "GF", "RE", "YT")
fr_dom_countries_domain = [("code", "in", fr_dom_countries_codes)]
fr_dom_countries = self.env["res.country"].search(fr_dom_countries_domain)

# Group partners by zip code
partners_by_zipcode = dict(groupby(self, key=_get_zipcode))
Expand All @@ -48,7 +48,7 @@ def _get_zipcode(partner) -> str:
if department_codes:
departments_domain = [
("code", "in", tuple(department_codes)),
("country_id", "in", fr_countries.ids),
("country_id", "in", fr_dom_countries.ids),
]
departments = department_obj.search(departments_domain)

Expand Down
4 changes: 2 additions & 2 deletions l10n_fr_department/post_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def set_department_on_partner(env):
So, when it computes the field on module installation, the
departments are not available in the DB, so the country_department_id field
on res.partner stays null. This post_install script fixes this."""
fr_countries = env["res.country"].search(
fr_dom_countries = env["res.country"].search(
[("code", "in", ("FR", "GP", "MQ", "GF", "RE", "YT"))]
)
partners = (
env["res.partner"]
.with_context(active_test=False)
.search([("country_id", "in", fr_countries.ids)])
.search([("country_id", "in", fr_dom_countries.ids)])
)
partners._compute_country_department()
8 changes: 4 additions & 4 deletions l10n_fr_department/view/res_country_department.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
<field name="name">res.country.department.tree</field>
<field name="model">res.country.department</field>
<field name="arch" type="xml">
<tree>
<field name="name" />
<list>
<field name="name" decoration-bf="1" />
<field name="code" />
<field name="state_id" />
<field name="country_id" />
</tree>
</list>
</field>
</record>
<record id="view_country_department_form" model="ir.ui.view">
Expand All @@ -61,7 +61,7 @@
<record id="action_view_country_department_tree" model="ir.actions.act_window">
<field name="name">Departments</field>
<field name="res_model">res.country.department</field>
<field name="view_mode">tree,form</field>
<field name="view_mode">list,form</field>
</record>
<menuitem
id="menu_res_country_department"
Expand Down
2 changes: 1 addition & 1 deletion l10n_fr_department/view/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<!-- The layout of the address block in the partner form view is too fragile to add
the field country_department_id inside it. And, as country_department_id is a computed
field, it can be displayed elsewhere -->
<xpath expr="//div[hasclass('o_address_format')]/.." position="inside">
<xpath expr="//div[hasclass('o_address_format')]" position="after">
<field
name="country_department_id"
invisible="not country_department_id"
Expand Down

0 comments on commit f095a72

Please sign in to comment.