Odoo modules about association management
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.

68 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. TYPES = [
  4. ("adm", "Administration"),
  5. ("sup", "Supervision"),
  6. ("ag", "General Assembly"),
  7. ]
  8. class ResPartnerRelationInsctance(models.Model):
  9. _name = "res.partner.relation.instance"
  10. _description = "Partners relation instance"
  11. _order = "inst_type, short, name, id"
  12. name = fields.Char(
  13. string="Name",
  14. translate=True,
  15. required=True,
  16. )
  17. short = fields.Char(
  18. string="Short name",
  19. translate=True,
  20. required=True,
  21. )
  22. inst_type = fields.Selection(
  23. selection=TYPES,
  24. string="Instance type",
  25. required=True,
  26. )
  27. parent_id = fields.Many2one(
  28. comodel_name="res.partner.relation.instance",
  29. string="Superior instance",
  30. )
  31. parent_path = fields.Char(
  32. unaccent=False,
  33. )
  34. parents_and_self = fields.Many2many(
  35. comodel_name="res.partner.relation.instance",
  36. compute="_compute_parents_and_self",
  37. )
  38. child_ids = fields.One2many(
  39. comodel_name="res.partner.relation.instance",
  40. inverse_name="parent_id",
  41. string="Composing instances",
  42. )
  43. def name_get(self):
  44. res = []
  45. for instance in self:
  46. res.append(
  47. (instance.id, " / ".join(instance.parents_and_self.mapped("name")))
  48. )
  49. return res
  50. def _compute_parents_and_self(self):
  51. for instance in self:
  52. if instance.parent_path:
  53. instance.parents_and_self = self.env[self._model].browse(
  54. [int(p) for p in instance.parent_path.split("/")[:-1]]
  55. )
  56. else:
  57. instance.parents_and_self = instance
  58. @api.constrains("parent_id")
  59. def check_parent_id(self):
  60. if not self._check_recursion():
  61. raise ValueError(_("Error ! You cannot create recursive instances."))