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.

66 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.team_id.user_id:
  44. self.user_id = self.team_id.user_id
  45. elif len(self.team_id.member_ids.ids) == 1:
  46. self.user_id = self.team_id.member_ids
  47. elif self.env.user.id in self.team_id.member_ids.ids:
  48. self.user_id = self.env.user
  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.'))