Skip to content

Commit

Permalink
[FIX] base_import_match: importing related o2m records
Browse files Browse the repository at this point in the history
The original code did improperly handle importing
related one2many records where at best all their
attribute values would get overwritten by last
of them. This was caused by mapping them only by
their first part.
  • Loading branch information
Rad0van authored and sergiocorato committed Jul 17, 2024
1 parent 4200890 commit 188a44e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
3 changes: 2 additions & 1 deletion base_import_match/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Base Import Match
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:40f62ca1ed4ddafbe04e1dba6fafb257e2262df0dc6dc19eda7aa8c466d9c1b0
!! source digest: sha256:a814a82527acac6bf3fec5490bf37a35060a58c552bed21fec066ed77b7bed01
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -140,6 +140,7 @@ Contributors
* Jairo Llopis
* Vicent Cubells
* Ernesto Tejeda
* Radovan Skolnik <[email protected]>

Maintainers
~~~~~~~~~~~
Expand Down
47 changes: 25 additions & 22 deletions base_import_match/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,33 @@ def load(self, fields, data):
# Mock Odoo to believe the user is importing the ID field
if "id" not in fields:
fields.append("id")
import_fields.append(["id"])
# Needed to match with converted data field names
clean_fields = [f[0] for f in import_fields]
for dbid, xmlid, record, info in converted_data:
row = dict(zip(clean_fields, data[info["record"]]))
match = self
if xmlid:
# Skip rows with ID, they do not need all this
row["id"] = xmlid
newdata.append(tuple(row[f] for f in clean_fields))
continue
elif dbid:
# Find the xmlid for this dbid
match = self.browse(dbid)
else:
# Store records that match a combination
match = self.env["base_import.match"]._match_find(self, record, row)
# Give a valid XMLID to this row if a match was found
# To generate externals IDS.
match.export_data(fields)
ext_id = match.get_external_id()
row["id"] = ext_id[match.id] if match else row.get("id", "")
# Store the modified row, in the same order as fields
newdata.append(tuple(row[f] for f in clean_fields))
# In case of one2many on empty lines one record may contain several rows
for row_index in range(info["rows"]["from"], info["rows"]["to"] + 1):
row = dict(zip(fields, data[row_index]))
match = self
if xmlid:
# Skip rows with ID, they do not need all this
row["id"] = xmlid
continue
elif dbid:
# Find the xmlid for this dbid
match = self.browse(dbid)
elif row_index == info["rows"]["from"]:
# Store records that match a combination
# But only for first row of record,
# because the rest contain one2many fields
match = self.env["base_import.match"]._match_find(
self, record, row
)
# Give a valid XMLID to this row if a match was found
# To generate externals IDS.
match.export_data(fields)
ext_id = match.get_external_id()
row["id"] = ext_id[match.id] if match else row.get("id", "")
# Store the modified row, in the same order as fields
newdata.append(tuple(row[f] for f in fields))
# We will import the patched data to get updates on matches
data = newdata
# Normal method handles the rest of the job
Expand Down
1 change: 1 addition & 0 deletions base_import_match/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* Jairo Llopis
* Vicent Cubells
* Ernesto Tejeda
* Radovan Skolnik <[email protected]>
3 changes: 2 additions & 1 deletion base_import_match/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Base Import Match</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:40f62ca1ed4ddafbe04e1dba6fafb257e2262df0dc6dc19eda7aa8c466d9c1b0
!! source digest: sha256:a814a82527acac6bf3fec5490bf37a35060a58c552bed21fec066ed77b7bed01
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-backend/tree/14.0/base_import_match"><img alt="OCA/server-backend" src="https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-backend-14-0/server-backend-14-0-base_import_match"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-backend&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>By default, when importing data (like CSV import) with the <tt class="docutils literal">base_import</tt>
Expand Down Expand Up @@ -495,6 +495,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
* Jairo Llopis
* Vicent Cubells
* Ernesto Tejeda</li>
<li>Radovan Skolnik &lt;<a class="reference external" href="mailto:radovan&#64;skolnik.info">radovan&#64;skolnik.info</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
email,function,child_ids/name,child_ids/color,child_ids/email
[email protected],Bug Fixer,Bart Steward,666,[email protected]
,,Lisa Steward,777,[email protected]
,,Maggie Steward,555,[email protected]
41 changes: 41 additions & 0 deletions base_import_match/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,44 @@ def test_res_users_login(self):
record = self._base_import_record("res.users", "res_users_login")
record.do(["login", "name"], [], OPTIONS)
self.assertEqual(self.env.ref("base.user_demo").name, "Demo User Changed")

def test_res_partner_email_one2many(self):
"""Change function based on email and import one2many record."""
record = self._base_import_record("res.partner", "res_partner_email_one2many")
record.do(
[
"email",
"function",
"child_ids/name",
"child_ids/color",
"child_ids/email",
],
[],
OPTIONS,
)
self.assertEqual(
self.env.ref("base.res_partner_address_4").function, "Bug Fixer"
)
self.assertTrue(
self.env.ref("base.res_partner_address_4").child_ids,
)
self.assertEqual(
len(self.env.ref("base.res_partner_address_4").child_ids),
3,
)
self.assertEqual(
set(self.env.ref("base.res_partner_address_4").mapped("child_ids.name")),
{"Bart Steward", "Lisa Steward", "Maggie Steward"},
)
self.assertEqual(
set(self.env.ref("base.res_partner_address_4").mapped("child_ids.email")),
{
"[email protected]",
"[email protected]",
"[email protected]",
},
)
self.assertEqual(
set(self.env.ref("base.res_partner_address_4").mapped("child_ids.color")),
{666, 777, 555},
)

0 comments on commit 188a44e

Please sign in to comment.