From 8cc7d3c874c050696540938a5429843602a663e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Gil=20Sorribes?= Date: Fri, 21 Dec 2018 16:05:49 +0100 Subject: [PATCH] [IMP] Set team responsible as default user when activity is created --- mail_activity_team/models/mail_activity.py | 15 +++++++-------- mail_activity_team/models/mail_activity_team.py | 11 +++++++++-- .../tests/test_mail_activity_team.py | 2 +- .../views/mail_activity_team_views.xml | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 46dd6de2..1fccf746 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -46,14 +46,13 @@ class MailActivity(models.Model): if not self.team_id: return res res['domain']['user_id'] = [('id', 'in', self.team_id.member_ids.ids)] - if self.user_id and self.user_id in self.team_id.member_ids: - return res - if self.team_id.user_id: - self.user_id = self.team_id.user_id - elif self.env.user in self.team_id.member_ids.ids: - self.user_id = self.env.user - else: - self.user_id = self.env['res.users'] + if self.user_id not in self.team_id.member_ids: + if self.team_id.user_id: + self.user_id = self.team_id.user_id + elif len(self.team_id.member_ids) == 1: + self.user_id = self.team_id.member_ids + else: + self.user_id = self.env['res.users'] return res @api.multi diff --git a/mail_activity_team/models/mail_activity_team.py b/mail_activity_team/models/mail_activity_team.py index f97c185b..1e44e0d7 100644 --- a/mail_activity_team/models/mail_activity_team.py +++ b/mail_activity_team/models/mail_activity_team.py @@ -46,7 +46,6 @@ class MailActivityTeam(models.Model): user_id = fields.Many2one( comodel_name='res.users', string='Team Leader', - domain="[('id', 'in', member_ids)]", ) count_missing_activities = fields.Integer( string="Missing Activities", @@ -59,6 +58,13 @@ class MailActivityTeam(models.Model): if self.user_id and self.user_id not in self.member_ids: self.user_id = False + @api.onchange('user_id') + def _onchange_user_id(self): + if self.user_id and self.user_id not in self.member_ids: + members_ids = self.member_ids.ids + members_ids.append(self.user_id.id) + self.member_ids = [(4, member) for member in members_ids] + def assign_team_to_unassigned_activities(self): activity_model = self.env['mail.activity'] for team in self: @@ -68,4 +74,5 @@ class MailActivityTeam(models.Model): if team.res_model_ids: domain.append(('res_model_id', 'in', team.res_model_ids.ids)) missing_activities = activity_model.search(domain) - missing_activities.write({'team_id': team.id}) + for missing_activity in missing_activities: + missing_activity.write({'team_id': team.id}) diff --git a/mail_activity_team/tests/test_mail_activity_team.py b/mail_activity_team/tests/test_mail_activity_team.py index 3f7928cb..413e4122 100644 --- a/mail_activity_team/tests/test_mail_activity_team.py +++ b/mail_activity_team/tests/test_mail_activity_team.py @@ -100,7 +100,7 @@ class TestMailActivityTeam(TransactionCase): self.assertEqual(self.act2.team_id, self.team2) self.act2.team_id = self.team1 self.act2._onchange_team_id() - self.assertFalse(self.act2.user_id) + self.assertEqual(self.act2.user_id, self.team1.member_ids) with self.assertRaises(ValidationError): self.act2.write({ 'user_id': self.employee2.id, diff --git a/mail_activity_team/views/mail_activity_team_views.xml b/mail_activity_team/views/mail_activity_team_views.xml index 72b49a1d..6ecba09a 100644 --- a/mail_activity_team/views/mail_activity_team_views.xml +++ b/mail_activity_team/views/mail_activity_team_views.xml @@ -27,7 +27,7 @@ - +