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.

26 lines
955 B

  1. # Copyright (C) 2020 Open Source Integrators
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class ResPartner(models.Model):
  5. _inherit = "res.partner"
  6. @api.depends("animal_ids")
  7. def _compute_animal_count(self):
  8. for rec in self:
  9. rec.animal_count = len(rec.animal_ids)
  10. animal_ids = fields.One2many("animal", "partner_id", string="Animals")
  11. animal_count = fields.Integer(
  12. compute=_compute_animal_count, string="Number of Animals", store=True
  13. )
  14. def action_view_animals(self):
  15. action = self.env.ref("animal.action_animal").read()[0]
  16. if self.animal_count > 1:
  17. action["domain"] = [("id", "in", self.animal_ids.ids)]
  18. else:
  19. action["views"] = [(self.env.ref("animal.view_animal_form").id, "form")]
  20. action["res_id"] = self.animal_ids and self.animal_ids.ids[0] or False
  21. return action