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.

75 lines
1.8 KiB

11 years ago
11 years ago
  1. """
  2. Wrapper for OpenERP's cryptic write conventions for x2many fields.
  3. Example usage:
  4. import m2m
  5. browse_rec.write({'many_ids: m2m.clear())
  6. browse_rec.write({'many_ids: m2m.link(99))
  7. browse_rec.write({'many_ids: m2m.add({'name': 'Monty'}))
  8. browse_rec.write({'many_ids: m2m.replace([98, 99]))
  9. Since returned values are lists, the can be joined using the plus operator:
  10. browse_rec.write({'many_ids: m2m.clear() + m2m.link(99))
  11. (Source: https://github.com/dreispt/openerp-write2many)
  12. """
  13. def create(values):
  14. """ Create a referenced record """
  15. assert isinstance(values, dict)
  16. return [(0, 0, values)]
  17. def add(values):
  18. """ Intuitive alias for create() """
  19. return create(values)
  20. def write(id, values):
  21. """ Write on referenced record """
  22. assert isinstance(id, int)
  23. assert isinstance(values, dict)
  24. return [(1, id, values)]
  25. def remove(id):
  26. """ Unlink and delete referenced record """
  27. assert isinstance(id, int)
  28. return [(2, id)]
  29. def unlink(id):
  30. """ Unlink but do not delete the referenced record """
  31. assert isinstance(id, int)
  32. return [(3, id)]
  33. def link(id):
  34. """ Link but do not delete the referenced record """
  35. assert isinstance(id, int)
  36. return [(4, id)]
  37. def clear():
  38. """ Unlink all referenced records (doesn't delete them) """
  39. return [(5, 0)]
  40. def replace(ids):
  41. """ Unlink all current records and replace them with a new list """
  42. assert isinstance(ids, list)
  43. return [(6, 0, ids)]
  44. if __name__ == "__main__":
  45. # Tests:
  46. assert create({'name': 'Monty'}) == [(0, 0, {'name': 'Monty'})]
  47. assert write(99, {'name': 'Monty'}) == [(1, 99, {'name': 'Monty'})]
  48. assert remove(99) == [(2, 99)]
  49. assert unlink(99) == [(3, 99)]
  50. assert clear() == [(5, 0)]
  51. assert replace([97, 98, 99]) == [(6, 0, [97, 98, 99])]
  52. print("Done!")