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.

62 lines
1.6 KiB

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