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.

43 lines
1.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Antiun Ingenieria S.L. - Javier Iniesta
  3. # © 2016 Tecnativa S.L. - Vicent Cubells
  4. # © 2016 Tecnativa S.L. - Pedro M. Baeza
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from openerp import _, api, fields, exceptions, models
  7. class ResPartnerSector(models.Model):
  8. _name = 'res.partner.sector'
  9. _order = "parent_left"
  10. _parent_order = "name"
  11. _parent_store = True
  12. _description = "Sector"
  13. name = fields.Char(required=True, translate=True)
  14. parent_id = fields.Many2one(string='Parent',
  15. comodel_name='res.partner.sector',
  16. ondelete='restrict')
  17. child_ids = fields.One2many(comodel_name='res.partner.sector',
  18. inverse_name='parent_id',
  19. string="Children")
  20. parent_left = fields.Integer('Parent Left', select=True)
  21. parent_right = fields.Integer('Parent Right', select=True)
  22. @api.multi
  23. def name_get(self):
  24. def get_names(cat):
  25. """ Return the list [cat.name, cat.parent_id.name, ...] """
  26. res = []
  27. while cat:
  28. res.append(cat.name)
  29. cat = cat.parent_id
  30. return res
  31. return [(cat.id, " / ".join(reversed(get_names(cat)))) for cat in self]
  32. @api.constrains('parent_id')
  33. def _check_parent_id(self):
  34. if not self._check_recursion():
  35. raise exceptions.ValidationError(
  36. _('Error! You cannot create recursive sectors.'))