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.

78 lines
2.7 KiB

6 years ago
  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, fields, models
  4. class MailActivityTeam(models.Model):
  5. _name = "mail.activity.team"
  6. _description = 'Mail Activity Team'
  7. @api.depends('res_model_ids', 'member_ids')
  8. def _compute_missing_activities(self):
  9. activity_model = self.env['mail.activity']
  10. for team in self:
  11. domain = [('team_id', '=', False)]
  12. if team.member_ids:
  13. domain.append(('user_id', 'in', team.member_ids.ids))
  14. if team.res_model_ids:
  15. domain.append(('res_model_id', 'in', team.res_model_ids.ids))
  16. team.count_missing_activities = activity_model.search(
  17. domain, count=True)
  18. name = fields.Char(
  19. string='Name',
  20. required=True,
  21. translate=True,
  22. )
  23. active = fields.Boolean(
  24. string='Active',
  25. default=True,
  26. )
  27. res_model_ids = fields.Many2many(
  28. comodel_name='ir.model',
  29. string='Used models',
  30. domain=lambda self: [
  31. ('model', 'in',
  32. [k for k in self.env.registry if issubclass(
  33. type(self.env[k]), type(self.env['mail.activity.mixin']))
  34. and self.env[k]._auto])
  35. ],
  36. )
  37. member_ids = fields.Many2many(
  38. comodel_name='res.users',
  39. relation='mail_activity_team_users_rel',
  40. string="Team Members",
  41. )
  42. user_id = fields.Many2one(
  43. comodel_name='res.users',
  44. string='Team Leader',
  45. )
  46. count_missing_activities = fields.Integer(
  47. string="Missing Activities",
  48. compute='_compute_missing_activities',
  49. default=0,
  50. )
  51. @api.onchange('member_ids')
  52. def _onchange_member_ids(self):
  53. if self.user_id and self.user_id not in self.member_ids:
  54. self.user_id = False
  55. @api.onchange('user_id')
  56. def _onchange_user_id(self):
  57. if self.user_id and self.user_id not in self.member_ids:
  58. members_ids = self.member_ids.ids
  59. members_ids.append(self.user_id.id)
  60. self.member_ids = [(4, member) for member in members_ids]
  61. def assign_team_to_unassigned_activities(self):
  62. activity_model = self.env['mail.activity']
  63. for team in self:
  64. domain = [('team_id', '=', False)]
  65. if team.member_ids:
  66. domain.append(('user_id', 'in', team.member_ids.ids))
  67. if team.res_model_ids:
  68. domain.append(('res_model_id', 'in', team.res_model_ids.ids))
  69. missing_activities = activity_model.search(domain)
  70. for missing_activity in missing_activities:
  71. missing_activity.write({'team_id': team.id})