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.

54 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, models
  5. class Base(models.AbstractModel):
  6. _inherit = "base"
  7. @api.model
  8. def load(self, fields, data):
  9. """Try to identify rows by other pseudo-unique keys.
  10. It searches for rows that have no XMLID specified, and gives them
  11. one if any :attr:`~.field_ids` combination is found. With a valid
  12. XMLID in place, Odoo will understand that it must *update* the
  13. record instead of *creating* a new one.
  14. """
  15. # We only need to patch this call if there are usable rules for it
  16. if self.env["base_import.match"]._usable_rules(self._name, fields):
  17. newdata = list()
  18. # Data conversion to ORM format
  19. import_fields = 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. row["id"] = (match._BaseModel__export_xml_id()
  44. if match else row.get("id", u""))
  45. # Store the modified row, in the same order as fields
  46. newdata.append(tuple(row[f] for f in clean_fields))
  47. # We will import the patched data to get updates on matches
  48. data = newdata
  49. # Normal method handles the rest of the job
  50. return super(Base, self).load(fields, data)