From f095a72e12e5d72aa1a9c32deeb56134ec554329 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Tue, 26 Nov 2024 22:53:37 +0100 Subject: [PATCH] [MIG] l10n_fr_department to v18 Improve name of some variables Change unicity constraint from unique(code) to unique(country_id, code) on res.country.department --- .../migrations/16.0.1.0.0/pre-migration.py | 21 -------------- .../model/res_country_department.py | 28 ++++--------------- l10n_fr_department/model/res_partner.py | 10 +++---- l10n_fr_department/post_install.py | 4 +-- .../view/res_country_department.xml | 8 +++--- l10n_fr_department/view/res_partner.xml | 2 +- 6 files changed, 17 insertions(+), 56 deletions(-) delete mode 100644 l10n_fr_department/migrations/16.0.1.0.0/pre-migration.py diff --git a/l10n_fr_department/migrations/16.0.1.0.0/pre-migration.py b/l10n_fr_department/migrations/16.0.1.0.0/pre-migration.py deleted file mode 100644 index 2407c4ad5..000000000 --- a/l10n_fr_department/migrations/16.0.1.0.0/pre-migration.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2022 Akretion France (http://www.akretion.com/) -# @author: Alexis de Lattre -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from openupgradelib import openupgrade - - -@openupgrade.migrate() -def migrate(env, version): - if not openupgrade.column_exists(env.cr, "res_partner", "country_department_id"): - openupgrade.rename_fields( - env, - [ - ( - "res.partner", - "res_partner", - "department_id", - "country_department_id", - ), - ], - ) diff --git a/l10n_fr_department/model/res_country_department.py b/l10n_fr_department/model/res_country_department.py index 57431e90f..e01f56e87 100644 --- a/l10n_fr_department/model/res_country_department.py +++ b/l10n_fr_department/model/res_country_department.py @@ -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( @@ -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})" diff --git a/l10n_fr_department/model/res_partner.py b/l10n_fr_department/model/res_partner.py index a554ea4b2..086485c22 100644 --- a/l10n_fr_department/model/res_partner.py +++ b/l10n_fr_department/model/res_partner.py @@ -25,7 +25,7 @@ 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: @@ -33,9 +33,9 @@ def _get_zipcode(partner) -> str: 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)) @@ -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) diff --git a/l10n_fr_department/post_install.py b/l10n_fr_department/post_install.py index 7cc9c8c59..ca3bb1d4f 100644 --- a/l10n_fr_department/post_install.py +++ b/l10n_fr_department/post_install.py @@ -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() diff --git a/l10n_fr_department/view/res_country_department.xml b/l10n_fr_department/view/res_country_department.xml index d9bcdf64c..7266b5522 100644 --- a/l10n_fr_department/view/res_country_department.xml +++ b/l10n_fr_department/view/res_country_department.xml @@ -36,12 +36,12 @@ res.country.department.tree res.country.department - - + + - + @@ -61,7 +61,7 @@ Departments res.country.department - tree,form + list,form - +