From 5edac89b4fb96d211717b53ad708d023a372786d Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Thu, 26 May 2022 17:57:52 +0200 Subject: [PATCH 1/8] [IMP] mail_activity_team: Define team on activity types --- mail_activity_team/__manifest__.py | 1 + mail_activity_team/models/__init__.py | 1 + mail_activity_team/models/mail_activity.py | 11 +++++ .../models/mail_activity_mixin.py | 37 ++++++++++---- .../models/mail_activity_type.py | 11 +++++ .../tests/test_mail_activity_team.py | 49 +++++++++++++++++++ .../views/mail_activity_type.xml | 22 +++++++++ 7 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 mail_activity_team/models/mail_activity_type.py create mode 100644 mail_activity_team/views/mail_activity_type.xml diff --git a/mail_activity_team/__manifest__.py b/mail_activity_team/__manifest__.py index 348d267..5b00b74 100644 --- a/mail_activity_team/__manifest__.py +++ b/mail_activity_team/__manifest__.py @@ -15,6 +15,7 @@ "data": [ "security/ir.model.access.csv", "security/mail_activity_team_security.xml", + "views/mail_activity_type.xml", "views/mail_activity_team_views.xml", "views/mail_activity_views.xml", "views/res_users_views.xml", diff --git a/mail_activity_team/models/__init__.py b/mail_activity_team/models/__init__.py index a3143d5..655b0dd 100644 --- a/mail_activity_team/models/__init__.py +++ b/mail_activity_team/models/__init__.py @@ -2,3 +2,4 @@ from . import mail_activity_team from . import mail_activity from . import mail_activity_mixin from . import res_users +from . import mail_activity_type diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 4b740dd..863e90c 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -19,6 +19,8 @@ class MailActivity(models.Model): ) return self.env["mail.activity.team"].search(domain, limit=1) + user_id = fields.Many2one(required=False) + team_id = fields.Many2one( comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id() ) @@ -81,3 +83,12 @@ class MailActivity(models.Model): team_name=activity.team_id.name, ) ) + + @api.onchange("activity_type_id") + def _onchange_activity_type_id(self): + super(MailActivity, self)._onchange_activity_type_id() + if self.activity_type_id.default_team_id: + self.team_id = self.activity_type_id.default_team_id + members = self.activity_type_id.default_team_id.member_ids + if self.user_id not in members and members: + self.user_id = members[:1] diff --git a/mail_activity_team/models/mail_activity_mixin.py b/mail_activity_team/models/mail_activity_mixin.py index d9ea56e..fdbcbd0 100644 --- a/mail_activity_team/models/mail_activity_mixin.py +++ b/mail_activity_team/models/mail_activity_mixin.py @@ -46,16 +46,35 @@ class MailActivityMixin(models.AbstractModel): user-team missmatch. We can hook onto `act_values` dict as it's passed to the create activity method. """ - user_id = act_values.get("user_id") - if user_id: - team = ( - self.env["mail.activity"] - .with_context( - default_res_model=self._name, + if "team_id" not in act_values: + if act_type_xmlid: + activity_type = self.sudo().env.ref(act_type_xmlid) + else: + activity_type = ( + self.env["mail.activity.type"] + .sudo() + .browse(act_values["activity_type_id"]) ) - ._get_default_team_id(user_id=user_id) - ) - act_values.update({"team_id": team.id}) + if activity_type.default_team_id: + act_values.update({"team_id": activity_type.default_team_id.id}) + if ( + not act_values.get("user_id") + and activity_type.default_team_id.member_ids + ): + act_values.update( + {"user_id": activity_type.default_team_id.member_ids[:1].id} + ) + else: + user_id = act_values.get("user_id") + if user_id: + team = ( + self.env["mail.activity"] + .with_context(default_res_model=self._name,) + ._get_default_team_id(user_id=user_id) + ) + # Even if it comes empty, we don't want to mismatch the user's team + if team: + act_values.update({"team_id": team.id}) return super().activity_schedule( act_type_xmlid=act_type_xmlid, date_deadline=date_deadline, diff --git a/mail_activity_team/models/mail_activity_type.py b/mail_activity_team/models/mail_activity_type.py new file mode 100644 index 0000000..f6babce --- /dev/null +++ b/mail_activity_team/models/mail_activity_type.py @@ -0,0 +1,11 @@ +# Copyright 2022 CreuBlanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class MailActivityType(models.Model): + + _inherit = "mail.activity.type" + + default_team_id = fields.Many2one(comodel_name="mail.activity.team") diff --git a/mail_activity_team/tests/test_mail_activity_team.py b/mail_activity_team/tests/test_mail_activity_team.py index 0f1793f..4c69550 100644 --- a/mail_activity_team/tests/test_mail_activity_team.py +++ b/mail_activity_team/tests/test_mail_activity_team.py @@ -148,6 +148,7 @@ class TestMailActivityTeam(TransactionCase): self.assertEqual( self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." ) + self.act2.team_id = False self.act2._onchange_team_id() self.assertEqual(self.act2.user_id, self.employee) self.act2.team_id = self.team2 @@ -179,3 +180,51 @@ class TestMailActivityTeam(TransactionCase): activity_type_id=self.env.ref("mail.mail_activity_data_call").id, ) self.assertEqual(activity.team_id, self.team2) + + def test_schedule_activity_default_team(self): + """Correctly assign teams to auto scheduled activities. Those won't + trigger onchanges and could raise constraints and team missmatches""" + partner_record = self.employee.partner_id.with_user(self.employee.id) + self.env.ref("mail.mail_activity_data_call").default_team_id = self.team2 + activity = partner_record.activity_schedule( + act_type_xmlid="mail.mail_activity_data_call", user_id=self.employee2.id, + ) + self.assertEqual(activity.team_id, self.team2) + self.assertEqual(activity.user_id, self.employee2) + + def test_schedule_activity_default_team_no_user(self): + """Correctly assign teams to auto scheduled activities. Those won't + trigger onchanges and could raise constraints and team missmatches""" + partner_record = self.employee.partner_id.with_user(self.employee.id) + self.activity2.default_team_id = self.team2 + self.team2.member_ids = self.employee2 + activity = partner_record.activity_schedule(activity_type_id=self.activity2.id,) + self.assertEqual(activity.team_id, self.team2) + self.assertEqual(activity.user_id, self.employee2) + + def test_activity_count(self): + res = ( + self.env["res.users"] + .with_user(self.employee.id) + .with_context({"team_activities": True}) + .systray_get_activities() + ) + self.assertEqual(res[0]["total_count"], 0) + + def test_activity_schedule_next(self): + self.activity1.write( + { + "default_team_id": self.team1.id, + "default_next_type_id": self.activity2.id, + "force_next": True, + } + ) + self.activity2.default_team_id = self.team2 + self.team2.member_ids = self.employee2 + partner_record = self.employee.partner_id.with_user(self.employee.id) + activity = partner_record.activity_schedule(activity_type_id=self.activity1.id) + activity.flush() + _messages, next_activities = activity._action_done() + self.assertTrue(next_activities) + self.assertEqual(next_activities.team_id, self.team2) + self.assertEqual(next_activities.user_id, self.employee2) diff --git a/mail_activity_team/views/mail_activity_type.xml b/mail_activity_team/views/mail_activity_type.xml new file mode 100644 index 0000000..325873a --- /dev/null +++ b/mail_activity_team/views/mail_activity_type.xml @@ -0,0 +1,22 @@ + + + + + + mail.activity.type.form (in mail_activity_team) + mail.activity.type + + + + + + + + + + + From c3c52d738486d2b4cce8fb64f6f4165603e044a3 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Thu, 26 May 2022 21:14:04 +0200 Subject: [PATCH 2/8] [IMP] mail_activity_team: Improve test using standard methods --- mail_activity_team/models/mail_activity.py | 2 +- mail_activity_team/models/res_users.py | 5 +- .../tests/test_mail_activity_team.py | 133 ++++++++++++++---- 3 files changed, 112 insertions(+), 28 deletions(-) diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 863e90c..274b9e4 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -38,7 +38,7 @@ class MailActivity(models.Model): if self.team_id and self.user_id in self.team_id.member_ids: return res self.team_id = self.with_context( - default_res_model=self.res_model_id.id + default_res_model=self.res_model_id.model )._get_default_team_id(user_id=self.user_id.id) return res diff --git a/mail_activity_team/models/res_users.py b/mail_activity_team/models/res_users.py index dabdb3b..1ab0437 100644 --- a/mail_activity_team/models/res_users.py +++ b/mail_activity_team/models/res_users.py @@ -64,6 +64,9 @@ class ResUsers(models.Model): user_activities[activity["model"]][ "%s_count" % activity["states"] ] += activity["count"] - if activity["states"] in ("today", "overdue"): + if ( + activity["states"] in ("today", "overdue") + and activity["user_id"] != user + ): user_activities[activity["model"]]["total_count"] += activity["count"] return list(user_activities.values()) diff --git a/mail_activity_team/tests/test_mail_activity_team.py b/mail_activity_team/tests/test_mail_activity_team.py index 4c69550..5d94c5e 100644 --- a/mail_activity_team/tests/test_mail_activity_team.py +++ b/mail_activity_team/tests/test_mail_activity_team.py @@ -1,13 +1,14 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo.exceptions import ValidationError -from odoo.tests.common import TransactionCase +from odoo.tests.common import Form, SavepointCase -class TestMailActivityTeam(TransactionCase): - def setUp(self): - super(TestMailActivityTeam, self).setUp() - +class TestMailActivityTeam(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + self = cls self.env["mail.activity.team"].search([]).unlink() self.employee = self.env["res.users"].create( @@ -125,6 +126,18 @@ class TestMailActivityTeam(TransactionCase): } ) + def test_activity_members(self): + self.team1.member_ids |= self.employee2 + self.partner_client.refresh() + self.assertIn(self.employee2, self.partner_client.activity_team_user_ids) + self.assertIn(self.employee, self.partner_client.activity_team_user_ids) + self.assertEqual( + self.partner_client, + self.env["res.partner"].search( + [("activity_team_user_ids", "=", self.employee.id)] + ), + ) + def test_team_and_user_onchange(self): with self.assertRaises(ValidationError): self.team1.member_ids = [(3, self.employee.id)] @@ -144,32 +157,83 @@ class TestMailActivityTeam(TransactionCase): self.team2._onchange_user_id() self.assertTrue(self.employee3 in self.team2.member_ids) - def test_activity_onchanges(self): + def test_activity_onchanges_keep_user(self): self.assertEqual( self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." ) - self.act2.team_id = False - self.act2._onchange_team_id() - self.assertEqual(self.act2.user_id, self.employee) - self.act2.team_id = self.team2 - self.act2._onchange_team_id() - self.assertEqual(self.act2.user_id, self.employee) - self.act2.user_id = self.employee2 - self.act2._onchange_user_id() - self.assertEqual(self.act2.team_id, self.team2) + with Form(self.act2) as form: + form.team_id = self.env["mail.activity.team"] + self.assertEqual(form.user_id, self.employee) + + def test_activity_onchanges_user_no_member_team(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + with Form(self.act2) as form: + form.user_id = self.employee2 + self.assertEqual(form.team_id, self.team2) + + def test_activity_onchanges_user_no_team(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + with Form(self.act2) as form: + form.team_id = self.env["mail.activity.team"] + form.user_id = self.employee2 + self.assertEqual(form.team_id, self.team2) + + def test_activity_onchanges_team_no_member(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + self.team2.user_id = False + self.team2.member_ids = False + with Form(self.act2) as form: + form.team_id = self.team2 + self.assertFalse(form.user_id) + + def test_activity_onchanges_team_different_member(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + self.team2.user_id = self.employee2 + self.team2.member_ids = self.employee2 + with Form(self.act2) as form: + form.team_id = self.team2 + self.assertEqual(form.user_id, self.employee2) + + def test_activity_onchanges_team_different_member_no_leader(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + self.team2.user_id = False + self.team2.member_ids = self.employee2 + with Form(self.act2) as form: + form.team_id = self.team2 + self.assertEqual(form.user_id, self.employee2) + + def test_activity_onchanges_activity_type_set_team(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + self.activity1.default_team_id = self.team2 + self.assertEqual(self.act2.activity_type_id, self.activity2) + with Form(self.act2) as form: + form.activity_type_id = self.activity1 + self.assertEqual(form.team_id, self.team2) + + def test_activity_onchanges_activity_type_no_team(self): + self.assertEqual( + self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." + ) + self.assertEqual(self.act2.activity_type_id, self.activity2) + with Form(self.act2) as form: + form.activity_type_id = self.activity1 + self.assertEqual(form.team_id, self.team1) + + def test_activity_constrain(self): with self.assertRaises(ValidationError): self.act2.write({"user_id": self.employee2.id, "team_id": self.team1.id}) - self.team1.user_id = False - self.act2.user_id = False - self.act2._onchange_user_id() - self.team2.member_ids = [(4, self.employee3.id)] - self.act2.team_id = self.team1 - self.act2.team_id = False - self.act2.user_id = self.employee3 - self.act2._onchange_user_id() - self.act2.team_id = self.team2 - self.team2.member_ids = [(3, self.act2.user_id.id)] - self.act2._onchange_team_id() def test_schedule_activity(self): """Correctly assign teams to auto scheduled activities. Those won't @@ -210,6 +274,23 @@ class TestMailActivityTeam(TransactionCase): .systray_get_activities() ) self.assertEqual(res[0]["total_count"], 0) + self.assertEqual(res[0]["today_count"], 1) + partner_record = self.employee.partner_id.with_user(self.employee.id) + self.activity2.default_team_id = self.team2 + activity = partner_record.activity_schedule( + activity_type_id=self.activity2.id, user_id=self.employee2.id + ) + activity.flush() + res = ( + self.env["res.users"] + .with_user(self.employee.id) + .with_context({"team_activities": True}) + .systray_get_activities() + ) + self.assertEqual(res[0]["total_count"], 1) + self.assertEqual(res[0]["today_count"], 2) + res = self.env["res.users"].with_user(self.employee.id).systray_get_activities() + self.assertEqual(res[0]["total_count"], 2) def test_activity_schedule_next(self): self.activity1.write( From 589f0f7f38c8a7d49fe23fb11ba28b2f0b1a043e Mon Sep 17 00:00:00 2001 From: Olga Marco Date: Mon, 13 Jun 2022 10:41:41 +0200 Subject: [PATCH 3/8] [IMP] mail_activity_team: Force team on schedule If you schedule an activity and you don't force the team as False, errors might araise. For example, on holidays. --- mail_activity_team/models/mail_activity_mixin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mail_activity_team/models/mail_activity_mixin.py b/mail_activity_team/models/mail_activity_mixin.py index fdbcbd0..ded859a 100644 --- a/mail_activity_team/models/mail_activity_mixin.py +++ b/mail_activity_team/models/mail_activity_mixin.py @@ -72,9 +72,7 @@ class MailActivityMixin(models.AbstractModel): .with_context(default_res_model=self._name,) ._get_default_team_id(user_id=user_id) ) - # Even if it comes empty, we don't want to mismatch the user's team - if team: - act_values.update({"team_id": team.id}) + act_values.update({"team_id": team.id}) return super().activity_schedule( act_type_xmlid=act_type_xmlid, date_deadline=date_deadline, From bd214206b6e58e2aa1210c3de50b4c89b855bdab Mon Sep 17 00:00:00 2001 From: Olga Marco Date: Tue, 14 Jun 2022 13:58:34 +0200 Subject: [PATCH 4/8] [IMP] mail_activity_team --- mail_activity_team/models/mail_activity.py | 5 +++-- .../models/mail_activity_mixin.py | 4 +++- .../tests/test_mail_activity_team.py | 18 ++++++++++-------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 274b9e4..179dafe 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -38,7 +38,7 @@ class MailActivity(models.Model): if self.team_id and self.user_id in self.team_id.member_ids: return res self.team_id = self.with_context( - default_res_model=self.res_model_id.model + default_res_model=self.sudo().res_model_id.model )._get_default_team_id(user_id=self.user_id.id) return res @@ -86,9 +86,10 @@ class MailActivity(models.Model): @api.onchange("activity_type_id") def _onchange_activity_type_id(self): - super(MailActivity, self)._onchange_activity_type_id() + res = super(MailActivity, self)._onchange_activity_type_id() if self.activity_type_id.default_team_id: self.team_id = self.activity_type_id.default_team_id members = self.activity_type_id.default_team_id.member_ids if self.user_id not in members and members: self.user_id = members[:1] + return res diff --git a/mail_activity_team/models/mail_activity_mixin.py b/mail_activity_team/models/mail_activity_mixin.py index ded859a..46e0fb4 100644 --- a/mail_activity_team/models/mail_activity_mixin.py +++ b/mail_activity_team/models/mail_activity_mixin.py @@ -69,7 +69,9 @@ class MailActivityMixin(models.AbstractModel): if user_id: team = ( self.env["mail.activity"] - .with_context(default_res_model=self._name,) + .with_context( + default_res_model=self._name, + ) ._get_default_team_id(user_id=user_id) ) act_values.update({"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 5d94c5e..c0c0bc5 100644 --- a/mail_activity_team/tests/test_mail_activity_team.py +++ b/mail_activity_team/tests/test_mail_activity_team.py @@ -1,10 +1,10 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo.exceptions import ValidationError -from odoo.tests.common import Form, SavepointCase +from odoo.tests.common import Form, TransactionCase -class TestMailActivityTeam(SavepointCase): +class TestMailActivityTeam(TransactionCase): @classmethod def setUpClass(cls): super().setUpClass() @@ -251,7 +251,8 @@ class TestMailActivityTeam(SavepointCase): partner_record = self.employee.partner_id.with_user(self.employee.id) self.env.ref("mail.mail_activity_data_call").default_team_id = self.team2 activity = partner_record.activity_schedule( - act_type_xmlid="mail.mail_activity_data_call", user_id=self.employee2.id, + act_type_xmlid="mail.mail_activity_data_call", + user_id=self.employee2.id, ) self.assertEqual(activity.team_id, self.team2) self.assertEqual(activity.user_id, self.employee2) @@ -262,7 +263,9 @@ class TestMailActivityTeam(SavepointCase): partner_record = self.employee.partner_id.with_user(self.employee.id) self.activity2.default_team_id = self.team2 self.team2.member_ids = self.employee2 - activity = partner_record.activity_schedule(activity_type_id=self.activity2.id,) + activity = partner_record.activity_schedule( + activity_type_id=self.activity2.id, + ) self.assertEqual(activity.team_id, self.team2) self.assertEqual(activity.user_id, self.employee2) @@ -270,7 +273,7 @@ class TestMailActivityTeam(SavepointCase): res = ( self.env["res.users"] .with_user(self.employee.id) - .with_context({"team_activities": True}) + .with_context(**{"team_activities": True}) .systray_get_activities() ) self.assertEqual(res[0]["total_count"], 0) @@ -284,7 +287,7 @@ class TestMailActivityTeam(SavepointCase): res = ( self.env["res.users"] .with_user(self.employee.id) - .with_context({"team_activities": True}) + .with_context(**{"team_activities": True}) .systray_get_activities() ) self.assertEqual(res[0]["total_count"], 1) @@ -296,8 +299,7 @@ class TestMailActivityTeam(SavepointCase): self.activity1.write( { "default_team_id": self.team1.id, - "default_next_type_id": self.activity2.id, - "force_next": True, + "triggered_next_type_id": self.activity2.id, } ) self.activity2.default_team_id = self.team2 From cdc606b5669229e2cc91981e30fb4ebee7e9e26e Mon Sep 17 00:00:00 2001 From: Kevin Luna Date: Fri, 18 Nov 2022 13:37:02 +0100 Subject: [PATCH 5/8] [IMP] mail_activity_type: Remove deprecated usage of onchange Replaced for web_domain_field module --- mail_activity_team/models/mail_activity.py | 34 +++++++++---------- .../tests/test_mail_activity_team.py | 6 ++++ .../views/mail_activity_views.xml | 31 +++++++++++++++-- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 179dafe..df9add5 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -1,5 +1,7 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import json + from odoo import SUPERUSER_ID, _, api, fields, models from odoo.exceptions import ValidationError @@ -20,42 +22,40 @@ class MailActivity(models.Model): return self.env["mail.activity.team"].search(domain, limit=1) user_id = fields.Many2one(required=False) + team_user_id = fields.Many2one(related="user_id", readonly=False) team_id = fields.Many2one( comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id() ) + user_id_domain = fields.Char(compute="_compute_user_id_domain") + + @api.depends("team_id") + def _compute_user_id_domain(self): + for record in self: + domain = [] + if record.team_id: + domain.append(("id", "in", record.team_id.member_ids.ids)) + record.user_id_domain = json.dumps(domain) @api.onchange("user_id") def _onchange_user_id(self): - res = {"domain": {"team_id": []}} - if not self.user_id: - return res - res["domain"]["team_id"] = [ - "|", - ("res_model_ids", "=", False), - ("res_model_ids", "in", self.res_model_id.ids), - ] - if self.team_id and self.user_id in self.team_id.member_ids: - return res + if not self.user_id or ( + self.team_id and self.user_id in self.team_id.member_ids + ): + return self.team_id = self.with_context( default_res_model=self.sudo().res_model_id.model )._get_default_team_id(user_id=self.user_id.id) - return res @api.onchange("team_id") def _onchange_team_id(self): - res = {"domain": {"user_id": []}} - if not self.team_id: - return res - res["domain"]["user_id"] = [("id", "in", self.team_id.member_ids.ids)] - if self.user_id not in self.team_id.member_ids: + if self.team_id and 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.constrains("team_id", "user_id") def _check_team_and_user(self): diff --git a/mail_activity_team/tests/test_mail_activity_team.py b/mail_activity_team/tests/test_mail_activity_team.py index c0c0bc5..161b0a4 100644 --- a/mail_activity_team/tests/test_mail_activity_team.py +++ b/mail_activity_team/tests/test_mail_activity_team.py @@ -1,5 +1,7 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import json + from odoo.exceptions import ValidationError from odoo.tests.common import Form, TransactionCase @@ -200,6 +202,10 @@ class TestMailActivityTeam(TransactionCase): self.team2.member_ids = self.employee2 with Form(self.act2) as form: form.team_id = self.team2 + self.assertIn( + form.user_id, + self.env["res.users"].search(json.loads(form.user_id_domain)), + ) self.assertEqual(form.user_id, self.employee2) def test_activity_onchanges_team_different_member_no_leader(self): diff --git a/mail_activity_team/views/mail_activity_views.xml b/mail_activity_team/views/mail_activity_views.xml index 2c0be0c..9b341bd 100644 --- a/mail_activity_team/views/mail_activity_views.xml +++ b/mail_activity_team/views/mail_activity_views.xml @@ -6,8 +6,22 @@ mail.activity + + {'invisible': [('team_id', '!=', False)]} + - + + @@ -31,8 +45,21 @@ ref="mail_activity_board.mail_activity_view_form_board" /> + + {'invisible': [('team_id', '=', False)]} + - + + From 607faa085764c3e1fa30307cd07ecdef04522f9e Mon Sep 17 00:00:00 2001 From: david Date: Mon, 18 Apr 2022 11:52:29 +0200 Subject: [PATCH 6/8] [FIX] mail_activity_team: activity count The user couldn't see the proper count of his own activities. It was allways showing the team activities counter. TT35885 --- mail_activity_team/models/mail_activity.py | 10 ------ mail_activity_team/models/res_users.py | 2 +- .../static/src/js/systray.esm.js | 35 +++++++++++-------- .../tests/test_mail_activity_team.py | 5 --- .../views/mail_activity_views.xml | 2 +- 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index df9add5..7eb0a20 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -1,6 +1,5 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import json from odoo import SUPERUSER_ID, _, api, fields, models from odoo.exceptions import ValidationError @@ -27,15 +26,6 @@ class MailActivity(models.Model): team_id = fields.Many2one( comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id() ) - user_id_domain = fields.Char(compute="_compute_user_id_domain") - - @api.depends("team_id") - def _compute_user_id_domain(self): - for record in self: - domain = [] - if record.team_id: - domain.append(("id", "in", record.team_id.member_ids.ids)) - record.user_id_domain = json.dumps(domain) @api.onchange("user_id") def _onchange_user_id(self): diff --git a/mail_activity_team/models/res_users.py b/mail_activity_team/models/res_users.py index 1ab0437..c43e006 100644 --- a/mail_activity_team/models/res_users.py +++ b/mail_activity_team/models/res_users.py @@ -14,7 +14,7 @@ class ResUsers(models.Model): @api.model def systray_get_activities(self): - if not self._context.get("team_activities", False): + if not self.env.context.get("team_activities"): return super().systray_get_activities() query = """SELECT m.id, count(*), act.res_model as model, CASE diff --git a/mail_activity_team/static/src/js/systray.esm.js b/mail_activity_team/static/src/js/systray.esm.js index ea10bf8..5d9f39c 100644 --- a/mail_activity_team/static/src/js/systray.esm.js +++ b/mail_activity_team/static/src/js/systray.esm.js @@ -11,10 +11,11 @@ ActivityMenu.include({ this.$filter_buttons = this.$(".o_filter_button"); this.$my_activities = this.$filter_buttons.first(); this.filter = "my"; - this.user_context = session.user_context; - this.user_context = _.extend({}, session.user_context, { - team_activities: false, - }); + this._update_team_activities_context(); + }, + + _update_team_activities_context: function () { + session.user_context.team_activities = this.filter === "team"; }, _updateCounter: function () { @@ -23,18 +24,13 @@ ActivityMenu.include({ }, _onClickFilterButton: function (event) { - var self = this; event.stopPropagation(); - self.$filter_buttons.removeClass("active"); + this.$filter_buttons.removeClass("active"); var $target = $(event.currentTarget); $target.addClass("active"); - self.filter = $target.data("filter"); - - self.user_context = _.extend({}, session.user_context, { - team_activities: self.filter === "team", - }); - - self._updateActivityPreview(); + this.filter = $target.data("filter"); + this._update_team_activities_context(); + this._updateActivityPreview(); }, _onActivityFilterClick: function (event) { if (this.filter === "my") { @@ -63,12 +59,21 @@ ActivityMenu.include({ [false, "form"], ], search_view_id: [false], - domain: [["activity_team_user_ids", "in", session.uid]], + domain: [["activity_team_user_ids", "in", [session.uid]]], context: context, }); } }, + _open_boards_activities_domain: function () { + if (this.filter === "team") { + return {additional_context: {search_default_my_team_activities: 1}}; + } + return this._super.apply(this, arguments); + }, _getActivityData: function () { + if (this.filter !== "team") { + return this._super.apply(this, arguments); + } var self = this; return self @@ -89,6 +94,8 @@ ActivityMenu.include({ ); self.$(".o_notification_counter").text(self.activityCounter); self.$el.toggleClass("o_no_notification", !self.activityCounter); + // Unset context after we gather the info to avoid side effects + session.user_context.team_activities = false; }); }, }); diff --git a/mail_activity_team/tests/test_mail_activity_team.py b/mail_activity_team/tests/test_mail_activity_team.py index 161b0a4..8ea26a8 100644 --- a/mail_activity_team/tests/test_mail_activity_team.py +++ b/mail_activity_team/tests/test_mail_activity_team.py @@ -1,6 +1,5 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import json from odoo.exceptions import ValidationError from odoo.tests.common import Form, TransactionCase @@ -202,10 +201,6 @@ class TestMailActivityTeam(TransactionCase): self.team2.member_ids = self.employee2 with Form(self.act2) as form: form.team_id = self.team2 - self.assertIn( - form.user_id, - self.env["res.users"].search(json.loads(form.user_id_domain)), - ) self.assertEqual(form.user_id, self.employee2) def test_activity_onchanges_team_different_member_no_leader(self): diff --git a/mail_activity_team/views/mail_activity_views.xml b/mail_activity_team/views/mail_activity_views.xml index 9b341bd..893ef85 100644 --- a/mail_activity_team/views/mail_activity_views.xml +++ b/mail_activity_team/views/mail_activity_views.xml @@ -52,7 +52,7 @@ From e4f701d36feade10e04eaf4c9babea47ee58c1f1 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Sun, 20 Nov 2022 19:47:10 +0100 Subject: [PATCH 7/8] [FIX] mail_activity_team: restore to my activities when unselecting --- mail_activity_team/static/src/js/systray.esm.js | 9 +++++++++ mail_activity_team/static/src/xml/systray.xml | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mail_activity_team/static/src/js/systray.esm.js b/mail_activity_team/static/src/js/systray.esm.js index 5d9f39c..3b6fe0b 100644 --- a/mail_activity_team/static/src/js/systray.esm.js +++ b/mail_activity_team/static/src/js/systray.esm.js @@ -98,4 +98,13 @@ ActivityMenu.include({ session.user_context.team_activities = false; }); }, + _onActivityMenuHide: function () { + this.filter = "my"; + this._update_team_activities_context(); + this._getActivityData(); + this.$filter_buttons.removeClass("active"); + var $target = $(".my_activities"); + $target.addClass("active"); + return this._super.apply(this, arguments); + }, }); diff --git a/mail_activity_team/static/src/xml/systray.xml b/mail_activity_team/static/src/xml/systray.xml index dadf02c..9af724f 100644 --- a/mail_activity_team/static/src/xml/systray.xml +++ b/mail_activity_team/static/src/xml/systray.xml @@ -7,13 +7,13 @@
From 375d727ca5396c12c8d9ffa80014f3d8d5b7382c Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Tue, 20 Dec 2022 14:16:08 +0100 Subject: [PATCH 8/8] [FIX] mail_activity_team: Adapt changes to 15.0 --- mail_activity_team/static/src/js/systray.esm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mail_activity_team/static/src/js/systray.esm.js b/mail_activity_team/static/src/js/systray.esm.js index 3b6fe0b..a356e85 100644 --- a/mail_activity_team/static/src/js/systray.esm.js +++ b/mail_activity_team/static/src/js/systray.esm.js @@ -81,7 +81,7 @@ ActivityMenu.include({ model: "res.users", method: "systray_get_activities", args: [], - kwargs: {context: self.user_context}, + kwargs: {context: session.user_context}, }) .then(function (data) { self._activities = data;