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.

67 lines
2.5 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. user_id = fields.Many2one(required=False)
  19. team_id = fields.Many2one(
  20. comodel_name='mail.activity.team',
  21. default=lambda s: s._get_default_team_id(),
  22. )
  23. @api.onchange('user_id')
  24. def _onchange_user_id(self):
  25. res = {'domain': {'team_id': []}}
  26. if not self.user_id:
  27. return res
  28. res['domain']['team_id'] = [
  29. '|',
  30. ('res_model_ids', '=', False),
  31. ('res_model_ids', 'in', self.res_model_id.ids)]
  32. if self.team_id and self.user_id in self.team_id.member_ids:
  33. return res
  34. self.team_id = self.with_context(
  35. default_res_model=self.res_model_id.id).\
  36. _get_default_team_id(user_id=self.user_id.id)
  37. return res
  38. @api.onchange('team_id')
  39. def _onchange_team_id(self):
  40. res = {'domain': {'user_id': []}}
  41. if not self.team_id:
  42. return res
  43. res['domain']['user_id'] = [('id', 'in', self.team_id.member_ids.ids)]
  44. if self.user_id not in self.team_id.member_ids:
  45. if self.team_id.user_id:
  46. self.user_id = self.team_id.user_id
  47. elif len(self.team_id.member_ids) == 1:
  48. self.user_id = self.team_id.member_ids
  49. else:
  50. self.user_id = self.env['res.users']
  51. return res
  52. @api.multi
  53. @api.constrains('team_id', 'user_id')
  54. def _check_team_and_user(self):
  55. for activity in self:
  56. if activity.team_id and activity.user_id and \
  57. activity.user_id not in self.team_id.member_ids:
  58. raise ValidationError(
  59. _('The assigned user is not member of the team.'))