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.

61 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. """
  8. Contruct a tree of fields.
  9. ie: {
  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. """
  26. Converts the dict returned by update_dict to a list consistent with the
  27. Odoo API. The list is composed of strings (field names or aliases) or
  28. tuples.
  29. """
  30. parser = []
  31. for field, value in dict_parser.items():
  32. if value is True:
  33. parser.append(field)
  34. else:
  35. parser.append((field, convert_dict(value)))
  36. return parser
  37. class IrExport(models.Model):
  38. _inherit = 'ir.exports'
  39. @api.multi
  40. def get_json_parser(self):
  41. """
  42. Creates a parser from a ir_exports record and returns it. This parser
  43. can then be used to "jsonify" records of the ir_export's model.
  44. """
  45. self.ensure_one()
  46. dict_parser = OrderedDict()
  47. for line in self.export_fields:
  48. names = line.name.split('/')
  49. if line.alias:
  50. names = line.alias.split('/')
  51. update_dict(dict_parser, names)
  52. return convert_dict(dict_parser)