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.

44 lines
1.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 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 openerp import api, models
  6. from openerp.exceptions import UserError
  7. @api.multi
  8. def jsonify(self, parser):
  9. """ Convert the record according to the parser given
  10. Example of parser:
  11. parser = [
  12. 'name',
  13. 'number',
  14. 'create_date',
  15. ('partner_id', ['id', 'display_name', 'ref'])
  16. ('line_id', ['id', ('product_id', ['name']), 'price_unit'])
  17. ]
  18. In order to be consitent with the odoo api the jsonify method always
  19. return a list of object even if there is only one element in input
  20. """
  21. result = []
  22. for rec in self:
  23. res = {}
  24. for field in parser:
  25. if isinstance(field, tuple):
  26. field_name, subparser = field
  27. field_type = rec._fields[field_name].type
  28. if field_type in ('one2many', 'many2many'):
  29. res[field_name] = rec[field_name].jsonify(subparser)
  30. elif field_type == 'many2one':
  31. res[field_name] = rec[field_name].jsonify(subparser)[0]
  32. else:
  33. raise UserError('Wrong parser configuration')
  34. else:
  35. res[field] = rec[field]
  36. result.append(res)
  37. return result
  38. models.Model.jsonify = jsonify