res_partner.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models
  4. from odoo.tools import email_normalize
  5. class ResPartner(models.Model):
  6. """ Inherits partner and adds Tasks information in the partner form """
  7. _inherit = 'res.partner'
  8. task_ids = fields.One2many('project.task', 'partner_id', string='Tasks')
  9. task_count = fields.Integer(compute='_compute_task_count', string='# Tasks')
  10. def _compute_task_count(self):
  11. # retrieve all children partners and prefetch 'parent_id' on them
  12. all_partners = self.with_context(active_test=False).search([('id', 'child_of', self.ids)])
  13. all_partners.read(['parent_id'])
  14. task_data = self.env['project.task']._read_group(
  15. domain=[('partner_id', 'in', all_partners.ids)],
  16. fields=['partner_id'], groupby=['partner_id']
  17. )
  18. self.task_count = 0
  19. for group in task_data:
  20. partner = self.browse(group['partner_id'][0])
  21. while partner:
  22. if partner in self:
  23. partner.task_count += group['partner_id_count']
  24. partner = partner.parent_id
  25. # Deprecated: remove me in MASTER
  26. def _create_portal_users(self):
  27. partners_without_user = self.filtered(lambda partner: not partner.user_ids)
  28. if not partners_without_user:
  29. return self.env['res.users']
  30. created_users = self.env['res.users']
  31. for partner in partners_without_user:
  32. created_users += self.env['res.users'].with_context(no_reset_password=True).sudo()._create_user_from_template({
  33. 'email': email_normalize(partner.email),
  34. 'login': email_normalize(partner.email),
  35. 'partner_id': partner.id,
  36. 'company_id': self.env.company.id,
  37. 'company_ids': [(6, 0, self.env.company.ids)],
  38. 'active': True,
  39. })
  40. return created_users