From 5edac89b4fb96d211717b53ad708d023a372786d Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Thu, 26 May 2022 17:57:52 +0200 Subject: [PATCH] [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 + + + + + + + + + + +