Browse Source
[10.0][MIG][base_import_match] Migration and update
[10.0][MIG][base_import_match] Migration and update
Includes: - Normal migration steps. - Usage of brand new `_inherit = "base"` in Odoo 10, which implies removing a lot of monkey-patching code. - Log a warning when multiple matches are found.14.0
Jairo Llopis
8 years ago
committed by
Jesus Ramoneda
6 changed files with 96 additions and 165 deletions
-
4base_import_match/README.rst
-
2base_import_match/__manifest__.py
-
1base_import_match/models/__init__.py
-
54base_import_match/models/base.py
-
198base_import_match/models/base_import.py
-
2base_import_match/tests/test_import.py
@ -0,0 +1,54 @@ |
|||
# -*- coding: utf-8 -*- |
|||
# Copyright 2017 Jairo Llopis <jairo.llopis@tecnativa.com> |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
from odoo import api, models |
|||
|
|||
|
|||
class Base(models.AbstractModel): |
|||
_inherit = "base" |
|||
|
|||
@api.model |
|||
def load(self, fields, data): |
|||
"""Try to identify rows by other pseudo-unique keys. |
|||
|
|||
It searches for rows that have no XMLID specified, and gives them |
|||
one if any :attr:`~.field_ids` combination is found. With a valid |
|||
XMLID in place, Odoo will understand that it must *update* the |
|||
record instead of *creating* a new one. |
|||
""" |
|||
# We only need to patch this call if there are usable rules for it |
|||
if self.env["base_import.match"]._usable_rules(self._name, fields): |
|||
newdata = list() |
|||
# Data conversion to ORM format |
|||
import_fields = map(models.fix_import_export_id_paths, fields) |
|||
converted_data = self._convert_records( |
|||
self._extract_records(import_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 |
|||
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 |
|||
row["id"] = (match._BaseModel__export_xml_id() |
|||
if match else row.get("id", u"")) |
|||
# Store the modified row, in the same order as fields |
|||
newdata.append(tuple(row[f] for f in clean_fields)) |
|||
# We will import the patched data to get updates on matches |
|||
data = newdata |
|||
# Normal method handles the rest of the job |
|||
return super(Base, self).load(fields, data) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue