Skip to content

Commit

Permalink
[MIG] l10n_fr_department and l10n_fr_department_oversea to v16
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-via committed Dec 30, 2022
1 parent fa0be25 commit d8c8637
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 39 deletions.
2 changes: 1 addition & 1 deletion l10n_fr_department/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"name": "French Departments (Départements)",
"summary": "Populate Database with French Departments (Départements)",
"version": "15.0.1.2.0",
"version": "16.0.1.0.0",
"category": "French Localization",
"author": "GRAP, "
"Akretion, "
Expand Down
21 changes: 21 additions & 0 deletions l10n_fr_department/migrations/16.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2022 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# 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",
),
],
)
15 changes: 9 additions & 6 deletions l10n_fr_department/model/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
class ResPartner(models.Model):
_inherit = "res.partner"

department_id = fields.Many2one(
# Warning: The OCA module 'partner_contact_department'
# from https://github.com/OCA/partner-contact
# adds a field 'department_id' on res.partner
# So we chose a more specific field name: country_department_id
country_department_id = fields.Many2one(
"res.country.department",
compute="_compute_department",
compute="_compute_country_department",
string="Department",
store=True,
)

@api.depends("zip", "country_id", "country_id.code")
# If a department code changes, it will have to be manually recomputed
def _compute_department(self):
def _compute_country_department(self):
def _get_zipcode(partner) -> str:
if partner.country_id not in fr_countries:
return ""
Expand Down Expand Up @@ -54,7 +58,7 @@ def _get_zipcode(partner) -> str:
# computed before loading ``res.country.department`` records via
# .xml file)
if not departments:
self.department_id = False
self.country_department_id = False
# Else: group departments by zip code, assign them to the grouped
# partners according to their common zip code
else:
Expand All @@ -65,7 +69,7 @@ def _get_zipcode(partner) -> str:
dep_code = zip2dep(zipcode)
if dep_code in departments_by_code:
department = departments_by_code[dep_code][0]
self.browse().concat(*partner_list).department_id = department
self.browse().concat(*partner_list).country_department_id = department

@api.model
@ormcache("zipcode")
Expand All @@ -80,7 +84,6 @@ def _fr_zipcode_to_department_code(self, zipcode):
"06260": "04",
"48250": "07",
"43450": "15",
"36260": "18",
"33220": "24",
"05700": "26",
"73670": "38",
Expand Down
6 changes: 3 additions & 3 deletions l10n_fr_department/post_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
def set_department_on_partner(cr, registry):
"""This post_install script is required because, when the module
is installed, Odoo creates the column in the DB and compute the field
and THEN it loads the file data/res_country_department_data.yml...
and THEN it loads the file data/res_country_department.xml...
So, when it computes the field on module installation, the
departments are not available in the DB, so the department_id field
departments are not available in the DB, so the country_department_id field
on res.partner stays null. This post_install script fixes this."""
env = api.Environment(cr, SUPERUSER_ID, {})
fr_countries = env["res.country"].search(
Expand All @@ -21,5 +21,5 @@ def set_department_on_partner(cr, registry):
.with_context(active_test=False)
.search([("country_id", "in", fr_countries.ids)])
)
partners._compute_department()
partners._compute_country_department()
return
5 changes: 2 additions & 3 deletions l10n_fr_department/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ french departments, but only the french departments of mainland.
If you need the overseas departments, please install the module
*l10n_fr_department_oversea*.

It also adds a computed many2one *department_id* field on the
*res_partner* object (this field is not displayed in the partner form
view by default).
It also adds a computed many2one *country_department_id* field on the
*res_partner* object.
Binary file modified l10n_fr_department/static/src/img/screenshots/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions l10n_fr_department/tests/test_department.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_fr_department(self):
}
)
self.assertEqual(
partner1.department_id,
partner1.country_department_id,
self.env.ref("l10n_fr_department.res_country_department_rhone"),
)
partner2 = rpo.create(
Expand All @@ -31,7 +31,7 @@ def test_fr_department(self):
}
)
self.assertEqual(
partner2.department_id,
partner2.country_department_id,
self.env.ref("l10n_fr_department.res_country_department_vaucluse"),
)

Expand All @@ -48,17 +48,17 @@ def test_corse(self):
"country_id": self.env.ref("base.fr").id,
}
)
self.assertEqual(partner.department_id, corse2A)
self.assertEqual(partner.country_department_id, corse2A)

partner.write({"zip": "20200", "city": "Bastia"})
self.assertEqual(partner.department_id, corse2B)
self.assertEqual(partner.country_department_id, corse2B)

partner.write({"zip": "20190", "city": "Zigliara"})
self.assertEqual(partner.department_id, corse2A)
self.assertEqual(partner.country_department_id, corse2A)
partner.write({"zip": "20620", "city": "Biguglia"})
self.assertEqual(partner.department_id, corse2B)
self.assertEqual(partner.country_department_id, corse2B)
partner.write({"zip": "20999", "city": "Unknown"})
self.assertFalse(partner.department_id)
self.assertFalse(partner.country_department_id)

def test_special(self):
partner1 = self.env["res.partner"].create(
Expand All @@ -70,7 +70,7 @@ def test_special(self):
}
)
self.assertEqual(
partner1.department_id,
partner1.country_department_id,
self.env.ref(
"l10n_fr_department.res_country_department_alpesdehauteprovence"
),
Expand Down
2 changes: 1 addition & 1 deletion l10n_fr_department/view/res_country_department.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2013-2020 GRAP (http://www.grap.coop)
Copyright 2013-2022 GRAP (http://www.grap.coop)
@author Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
Expand Down
15 changes: 8 additions & 7 deletions l10n_fr_department/view/res_partner.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
© 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
Copyright 2016-2022 Akretion France (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>
Expand All @@ -11,9 +12,9 @@
<field name="arch" type="xml">
<filter name="group_country" position="before">
<filter
name="department_groupby"
name="country_department_groupby"
string="Department"
context="{'group_by': 'department_id'}"
context="{'group_by': 'country_department_id'}"
/>
</filter>
</field>
Expand All @@ -25,19 +26,19 @@
<field name="arch" type="xml">
<field name="country_id" position="before">
<field
name="department_id"
name="country_department_id"
class="o_address_country"
attrs="{'invisible': [('department_id', '=', False)]}"
attrs="{'invisible': [('country_department_id', '=', False)]}"
/>
</field>
<xpath
expr="//field[@name='child_ids']/form//field[@name='country_id']"
position="before"
>
<field
name="department_id"
name="country_department_id"
class="o_address_country"
attrs="{'invisible': [('department_id', '=', False)]}"
attrs="{'invisible': [('country_department_id', '=', False)]}"
/>
</xpath>
</field>
Expand Down
1 change: 1 addition & 0 deletions l10n_fr_department_oversea/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .pre_init_hook import create_fr_oversea_state_xmlid
from .post_install import set_oversea_department_on_partner
3 changes: 2 additions & 1 deletion l10n_fr_department_oversea/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "French Overseas Departments (DOM)",
"summary": "Populate Database with overseas French "
"Departments (Départements d'outre-mer)",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"category": "French Localization",
"author": "Akretion,Odoo Community Association (OCA)",
"maintainers": ["alexis-via"],
Expand All @@ -17,6 +17,7 @@
"data/res_country_state.xml",
"data/res_country_department.xml",
],
"pre_init_hook": "create_fr_oversea_state_xmlid",
"post_init_hook": "set_oversea_department_on_partner",
"installable": True,
}
2 changes: 1 addition & 1 deletion l10n_fr_department_oversea/data/res_country_department.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016-2020 Akretion France (http://www.akretion.com/)
Copyright 2016-2022 Akretion France (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
Expand Down
2 changes: 1 addition & 1 deletion l10n_fr_department_oversea/data/res_country_state.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2016-2020 Akretion France (http://www.akretion.com/)
Copyright 2016-2022 Akretion France (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
Expand Down
13 changes: 9 additions & 4 deletions l10n_fr_department_oversea/post_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
def set_oversea_department_on_partner(cr, registry):
"""This post_install script is required because, when the module
is installed, Odoo creates the column in the DB and compute the field
and THEN it loads the file data/res_country_department_data.yml...
and THEN it loads the file data/res_country_department.xml...
So, when it computes the field on module installation, the
departments are not available in the DB, so the department_id field
departments are not available in the DB, so the country_department_id field
on res.partner stays null. This post_install script fixes this."""
env = api.Environment(cr, SUPERUSER_ID, {})
fr_countries = env["res.country"].search(
Expand All @@ -19,7 +19,12 @@ def set_oversea_department_on_partner(cr, registry):
partners = (
env["res.partner"]
.with_context(active_test=False)
.search([("country_id", "in", fr_countries.ids), ("department_id", "=", False)])
.search(
[
("country_id", "in", fr_countries.ids),
("country_department_id", "=", False),
]
)
)
partners._compute_department()
partners._compute_country_department()
return
20 changes: 20 additions & 0 deletions l10n_fr_department_oversea/pre_init_hook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2017-2022 Akretion France
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.addons.l10n_fr_state.pre_init_hook import generic_create_state_xmlid

# This code is designed to avoid a problem in the following scenario:
# On a new database, the administrator executes these steps in this order:
# 1) installs base_address_extended_geonames_import and run the geonames import wizard
# for one of the French DOMs
# -> it creates the corresponding French regions (without xmlid)
# 2) installs l10n_fr_department_oversea
# -> it tries to create the res.country.state, but it fails due to the unicity
# constraint unique(country_id, code) of res.country.state.


def create_fr_oversea_state_xmlid(cr):
generic_create_state_xmlid(
cr, "l10n_fr_department_oversea", "data/res_country_state.xml"
)
6 changes: 3 additions & 3 deletions l10n_fr_department_oversea/tests/test_department_oversea.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016-2018 Akretion France
# Copyright 2016-2022 Akretion France
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.tests.common import TransactionCase
Expand All @@ -18,7 +18,7 @@ def test_fr_department_oversea(self):
}
)
self.assertEqual(
partner1.department_id,
partner1.country_department_id,
self.env.ref("l10n_fr_department_oversea.res_country_department_reunion"),
)
# I also want it to work if you select France as country
Expand All @@ -33,6 +33,6 @@ def test_fr_department_oversea(self):
}
)
self.assertEqual(
partner2.department_id,
partner2.country_department_id,
self.env.ref("l10n_fr_department_oversea.res_country_department_reunion"),
)
1 change: 1 addition & 0 deletions setup/l10n_fr_department/odoo/addons/l10n_fr_department
6 changes: 6 additions & 0 deletions setup/l10n_fr_department/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
6 changes: 6 additions & 0 deletions setup/l10n_fr_department_oversea/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit d8c8637

Please sign in to comment.