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.

60 lines
2.0 KiB

  1. # Copyright 2015 Antiun Ingenieria S.L. - Javier Iniesta
  2. # Copyright 2016 Tecnativa S.L. - Vicent Cubells
  3. # Copyright 2016 Tecnativa S.L. - Pedro M. Baeza
  4. # Copyright 2018 Eficent Business and IT Consulting Services, S.L.
  5. # Copyright 2019 Tecnativa - Cristina Martin R.
  6. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  7. from odoo import _, api, exceptions, fields, models
  8. class ResPartnerIndustry(models.Model):
  9. _inherit = "res.partner.industry"
  10. _order = "parent_path"
  11. _parent_order = "name"
  12. _parent_store = True
  13. name = fields.Char(required=True)
  14. parent_id = fields.Many2one(
  15. comodel_name="res.partner.industry", ondelete="restrict"
  16. )
  17. child_ids = fields.One2many(
  18. comodel_name="res.partner.industry", inverse_name="parent_id", string="Children"
  19. )
  20. parent_path = fields.Char(index=True)
  21. def name_get(self):
  22. def get_names(cat):
  23. """Return the list [cat.name, cat.parent_id.name, ...]"""
  24. res = []
  25. while cat:
  26. res.insert(0, cat.name)
  27. cat = cat.parent_id
  28. return res
  29. return [(cat.id, " / ".join(get_names(cat))) for cat in self]
  30. @api.constrains("parent_id")
  31. def _check_parent_id(self):
  32. if not self._check_recursion():
  33. raise exceptions.ValidationError(
  34. _("Error! You cannot create recursive industries.")
  35. )
  36. @api.constrains("name", "parent_id")
  37. def _check_uniq_name(self):
  38. if (
  39. self.search_count(
  40. [("name", "=", self.name), ("parent_id", "=", self.parent_id.id)]
  41. )
  42. > 1
  43. ):
  44. raise exceptions.ValidationError(
  45. _("Error! Industry with same name and parent already exists.")
  46. )
  47. def copy(self, default=None):
  48. default = default or {}
  49. if "name" not in default or default["name"] == self.name:
  50. default["name"] = self.name + " 2"
  51. return super(ResPartnerIndustry, self).copy(default=default)