[IMP] mail_activity_team: Define team on activity types

This commit is contained in:
Enric Tobella 2022-05-26 17:57:52 +02:00
parent 0c71daadde
commit 5edac89b4f
7 changed files with 123 additions and 9 deletions

View File

@ -15,6 +15,7 @@
"data": [ "data": [
"security/ir.model.access.csv", "security/ir.model.access.csv",
"security/mail_activity_team_security.xml", "security/mail_activity_team_security.xml",
"views/mail_activity_type.xml",
"views/mail_activity_team_views.xml", "views/mail_activity_team_views.xml",
"views/mail_activity_views.xml", "views/mail_activity_views.xml",
"views/res_users_views.xml", "views/res_users_views.xml",

View File

@ -2,3 +2,4 @@ from . import mail_activity_team
from . import mail_activity from . import mail_activity
from . import mail_activity_mixin from . import mail_activity_mixin
from . import res_users from . import res_users
from . import mail_activity_type

View File

@ -19,6 +19,8 @@ class MailActivity(models.Model):
) )
return self.env["mail.activity.team"].search(domain, limit=1) return self.env["mail.activity.team"].search(domain, limit=1)
user_id = fields.Many2one(required=False)
team_id = fields.Many2one( team_id = fields.Many2one(
comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id() 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, 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]

View File

@ -46,15 +46,34 @@ class MailActivityMixin(models.AbstractModel):
user-team missmatch. We can hook onto `act_values` dict as it's passed user-team missmatch. We can hook onto `act_values` dict as it's passed
to the create activity method. to the create activity method.
""" """
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"])
)
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") user_id = act_values.get("user_id")
if user_id: if user_id:
team = ( team = (
self.env["mail.activity"] self.env["mail.activity"]
.with_context( .with_context(default_res_model=self._name,)
default_res_model=self._name,
)
._get_default_team_id(user_id=user_id) ._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( return super().activity_schedule(
act_type_xmlid=act_type_xmlid, act_type_xmlid=act_type_xmlid,

View File

@ -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")

View File

@ -148,6 +148,7 @@ class TestMailActivityTeam(TransactionCase):
self.assertEqual( self.assertEqual(
self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1." self.act2.team_id, self.team1, "Error: Activity 2 should have Team 1."
) )
self.act2.team_id = False
self.act2._onchange_team_id() self.act2._onchange_team_id()
self.assertEqual(self.act2.user_id, self.employee) self.assertEqual(self.act2.user_id, self.employee)
self.act2.team_id = self.team2 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, activity_type_id=self.env.ref("mail.mail_activity_data_call").id,
) )
self.assertEqual(activity.team_id, self.team2) 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)

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2022 CreuBlanca
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo>
<record model="ir.ui.view" id="mail_activity_type_form_view">
<field name="name">mail.activity.type.form (in mail_activity_team)</field>
<field name="model">mail.activity.type</field>
<field name="inherit_id" ref="mail.mail_activity_type_view_form" />
<field name="arch" type="xml">
<field name="default_user_id" position="after">
<field
name="default_team_id"
options="{'no_create': True, 'no_edit': True}"
/>
</field>
</field>
</record>
</odoo>