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

# -*- coding: utf-8 -*-
from odoo import models, fields, api
TYPES = [
("adm", "Administration"),
("sup", "Supervision"),
("ag", "General Assembly"),
]
class ResPartnerRelationInsctance(models.Model):
_name = "res.partner.relation.instance"
_description = "Partners relation instance"
_order = "inst_type, short, name, id"
name = fields.Char(
string="Name",
translate=True,
required=True,
)
short = fields.Char(
string="Short name",
translate=True,
required=True,
)
inst_type = fields.Selection(
selection=TYPES,
string="Instance type",
required=True,
)
parent_id = fields.Many2one(
comodel_name="res.partner.relation.instance",
string="Superior instance",
)
parent_path = fields.Char(
unaccent=False,
)
parents_and_self = fields.Many2many(
comodel_name="res.partner.relation.instance",
compute="_compute_parents_and_self",
)
child_ids = fields.One2many(
comodel_name="res.partner.relation.instance",
inverse_name="parent_id",
string="Composing instances",
)
def name_get(self):
res = []
for instance in self:
res.append(
(instance.id, " / ".join(instance.parents_and_self.mapped("name")))
)
return res
def _compute_parents_and_self(self):
for instance in self:
if instance.parent_path:
instance.parents_and_self = self.env[self._model].browse(
[int(p) for p in instance.parent_path.split("/")[:-1]]
)
else:
instance.parents_and_self = instance
@api.constrains("parent_id")
def check_parent_id(self):
if not self._check_recursion():
raise ValueError(_("Error ! You cannot create recursive instances."))