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 api, 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. @api.multi
  38. def get_json_parser(self):
  39. """Creates a parser from ir.exports record and return it.
  40. The final parser can be used to "jsonify" records of ir.export's model.
  41. """
  42. self.ensure_one()
  43. dict_parser = OrderedDict()
  44. for line in self.export_fields:
  45. names = line.name.split('/')
  46. if line.alias:
  47. names = line.alias.split('/')
  48. update_dict(dict_parser, names)
  49. return convert_dict(dict_parser)