You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.6 KiB

  1. # Copyright 2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, models
  4. class Base(models.AbstractModel):
  5. _inherit = "base"
  6. @api.model
  7. def load(self, fields, data):
  8. """Try to identify rows by other pseudo-unique keys.
  9. It searches for rows that have no XMLID specified, and gives them
  10. one if any :attr:`~.field_ids` combination is found. With a valid
  11. XMLID in place, Odoo will understand that it must *update* the
  12. record instead of *creating* a new one.
  13. """
  14. # We only need to patch this call if there are usable rules for it
  15. if self.env["base_import.match"]._usable_rules(self._name, fields):
  16. newdata = list()
  17. # Data conversion to ORM format
  18. import_fields = list(
  19. map(models.fix_import_export_id_paths, fields))
  20. converted_data = self._convert_records(
  21. self._extract_records(import_fields, data))
  22. # Mock Odoo to believe the user is importing the ID field
  23. if "id" not in fields:
  24. fields.append("id")
  25. import_fields.append(["id"])
  26. # Needed to match with converted data field names
  27. clean_fields = [f[0] for f in import_fields]
  28. for dbid, xmlid, record, info in converted_data:
  29. row = dict(zip(clean_fields, data[info["record"]]))
  30. match = self
  31. if xmlid:
  32. # Skip rows with ID, they do not need all this
  33. row["id"] = xmlid
  34. continue
  35. elif dbid:
  36. # Find the xmlid for this dbid
  37. match = self.browse(dbid)
  38. else:
  39. # Store records that match a combination
  40. match = self.env["base_import.match"]._match_find(
  41. self, record, row)
  42. # Give a valid XMLID to this row if a match was found
  43. # To generate externals IDS.
  44. match.export_data(fields)
  45. ext_id = match.get_external_id()
  46. row["id"] = (ext_id[match.id]
  47. if match else row.get("id", u""))
  48. # Store the modified row, in the same order as fields
  49. newdata.append(tuple(row[f] for f in clean_fields))
  50. # We will import the patched data to get updates on matches
  51. data = newdata
  52. # Normal method handles the rest of the job
  53. return super(Base, self).load(fields, data)