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.

65 lines
2.4 KiB

  1. # Copyright 2018 Eficent Business and IT Consulting Services, S.L.
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, models, fields, _
  4. from odoo.exceptions import ValidationError
  5. class MailActivity(models.Model):
  6. _inherit = "mail.activity"
  7. def _get_default_team_id(self, user_id=None):
  8. if not user_id:
  9. user_id = self.env.uid
  10. res_model = self.env.context.get('default_res_model')
  11. model = self.env['ir.model'].search(
  12. [('model', '=', res_model)], limit=1)
  13. domain = [('member_ids', 'in', [user_id])]
  14. if res_model:
  15. domain.extend(['|', ('res_model_ids', '=', False),
  16. ('res_model_ids', 'in', model.ids)])
  17. return self.env['mail.activity.team'].search(domain, limit=1)
  18. team_id = fields.Many2one(
  19. comodel_name='mail.activity.team',
  20. default=lambda s: s._get_default_team_id(),
  21. )
  22. @api.onchange('user_id')
  23. def _onchange_user_id(self):
  24. res = {'domain': {'team_id': []}}
  25. if not self.user_id:
  26. return res
  27. res['domain']['team_id'] = [
  28. '|',
  29. ('res_model_ids', '=', False),
  30. ('res_model_ids', 'in', self.res_model_id.ids)]
  31. if self.team_id and self.user_id in self.team_id.member_ids:
  32. return res
  33. self.team_id = self.with_context(
  34. default_res_model=self.res_model_id.id).\
  35. _get_default_team_id(user_id=self.user_id.id)
  36. return res
  37. @api.onchange('team_id')
  38. def _onchange_team_id(self):
  39. res = {'domain': {'user_id': []}}
  40. if not self.team_id:
  41. return res
  42. res['domain']['user_id'] = [('id', 'in', self.team_id.member_ids.ids)]
  43. if self.user_id not in self.team_id.member_ids:
  44. if self.team_id.user_id:
  45. self.user_id = self.team_id.user_id
  46. elif len(self.team_id.member_ids) == 1:
  47. self.user_id = self.team_id.member_ids
  48. else:
  49. self.user_id = self.env['res.users']
  50. return res
  51. @api.multi
  52. @api.constrains('team_id', 'user_id')
  53. def _check_team_and_user(self):
  54. for activity in self:
  55. if activity.team_id and activity.user_id and \
  56. activity.user_id not in self.team_id.member_ids:
  57. raise ValidationError(
  58. _('The assigned user is not member of the team.'))