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.

63 lines
1.6 KiB

  1. # © 2017 Akretion (http://www.akretion.com)
  2. # Sébastien BEAU <sebastien.beau@akretion.com>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from collections import OrderedDict
  5. from odoo import models
  6. def update_dict(data, fields):
  7. """Contruct a tree of fields.
  8. Example:
  9. {
  10. "name": True,
  11. "resource": True,
  12. }
  13. Order of keys is important.
  14. """
  15. field = fields[0]
  16. if len(fields) == 1:
  17. if field == ".id":
  18. field = "id"
  19. data[field] = True
  20. else:
  21. if field not in data:
  22. data[field] = OrderedDict()
  23. update_dict(data[field], fields[1:])
  24. def convert_dict(dict_parser):
  25. """Convert dict returned by update_dict to list consistent w/ Odoo API.
  26. The list is composed of strings (field names or aliases) or tuples.
  27. """
  28. parser = []
  29. for field, value in dict_parser.items():
  30. if value is True:
  31. parser.append(field)
  32. else:
  33. parser.append((field, convert_dict(value)))
  34. return parser
  35. class IrExport(models.Model):
  36. _inherit = "ir.exports"
  37. def get_json_parser(self):
  38. """Creates a parser from ir.exports record and return it.
  39. The final parser can be used to "jsonify" records of ir.export's model.
  40. """
  41. self.ensure_one()
  42. dict_parser = OrderedDict()
  43. for line in self.export_fields:
  44. names = line.name.split("/")
  45. if line.alias:
  46. names = line.alias.split("/")
  47. update_dict(dict_parser, names)
  48. return convert_dict(dict_parser)