[IMP] mail_activity_team: Allow to force a team on server actions

pull/1129/head
Ivàn Todorovich 2023-01-30 16:18:10 -03:00 committed by Raf Ven
parent 69192251b8
commit e2c7bd7c29
6 changed files with 124 additions and 74 deletions

View File

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

View File

@ -1,3 +1,4 @@
from . import ir_actions_server
from . import mail_activity_team
from . import mail_activity
from . import mail_activity_mixin

View File

@ -0,0 +1,20 @@
# Copyright 2023 Camptocamp SA (https://www.camptocamp.com).
# @author Iván Todorovich <ivan.todorovich@camptocamp.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models
class IrActionsServer(models.Model):
_inherit = "ir.actions.server"
activity_team_id = fields.Many2one(
"mail.activity.team",
string="Activity Team",
)
def _run_action_next_activity(self, eval_context=None):
# OVERRIDE to force the activity team on scheduled actions
if self.activity_user_type == "specific" and self.activity_team_id:
self = self.with_context(force_activity_team=self.activity_team_id)
return super()._run_action_next_activity(eval_context=eval_context)

View File

@ -46,6 +46,8 @@ class MailActivityMixin(models.AbstractModel):
user-team missmatch. We can hook onto `act_values` dict as it's passed
to the create activity method.
"""
if self.env.context.get("force_activity_team"):
act_values["team_id"] = self.env.context["force_activity_team"].id
if "team_id" not in act_values:
if act_type_xmlid:
activity_type = self.sudo().env.ref(act_type_xmlid)

View File

@ -9,12 +9,13 @@ class TestMailActivityTeam(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
self = cls
self.env["mail.activity.team"].search([]).unlink()
self.employee = self.env["res.users"].create(
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
# Start from a clean slate
cls.env["mail.activity.team"].search([]).unlink()
# Create Users
cls.employee = cls.env["res.users"].create(
{
"company_id": self.env.ref("base.main_company").id,
"company_id": cls.env.ref("base.main_company").id,
"name": "Employee",
"login": "csu",
"email": "crmuser@yourcompany.com",
@ -23,109 +24,93 @@ class TestMailActivityTeam(TransactionCase):
6,
0,
[
self.env.ref("base.group_user").id,
self.env.ref("base.group_partner_manager").id,
cls.env.ref("base.group_user").id,
cls.env.ref("base.group_partner_manager").id,
],
)
],
}
)
self.employee2 = self.env["res.users"].create(
cls.employee2 = cls.env["res.users"].create(
{
"company_id": self.env.ref("base.main_company").id,
"company_id": cls.env.ref("base.main_company").id,
"name": "Employee 2",
"login": "csu2",
"email": "crmuser2@yourcompany.com",
"groups_id": [(6, 0, [self.env.ref("base.group_user").id])],
"groups_id": [(6, 0, [cls.env.ref("base.group_user").id])],
}
)
self.partner_ir_model = self.env["ir.model"]._get("res.partner")
activity_type_model = self.env["mail.activity.type"]
self.activity1 = activity_type_model.create(
cls.employee3 = cls.env["res.users"].create(
{
"company_id": cls.env.ref("base.main_company").id,
"name": "Employee 3",
"login": "csu3",
"email": "crmuser3@yourcompany.com",
"groups_id": [(6, 0, [cls.env.ref("base.group_user").id])],
}
)
# Create Activity Types
cls.activity1 = cls.env["mail.activity.type"].create(
{
"name": "Initial Contact",
"delay_count": 5,
"delay_unit": "days",
"summary": "ACT 1 : Presentation, barbecue, ... ",
"res_model": self.partner_ir_model.model,
"res_model": "res.partner",
}
)
self.activity2 = activity_type_model.create(
cls.activity2 = cls.env["mail.activity.type"].create(
{
"name": "Call for Demo",
"delay_count": 6,
"delay_unit": "days",
"summary": "ACT 2 : I want to show you my ERP !",
"res_model": self.partner_ir_model.model,
"res_model": "res.partner",
}
)
self.partner_client = self.env.ref("base.res_partner_1")
self.act1 = (
self.env["mail.activity"]
.with_user(self.employee)
# Create Teams and Activities
cls.partner_client = cls.env.ref("base.res_partner_1")
cls.partner_ir_model = cls.env["ir.model"]._get("res.partner")
cls.act1 = (
cls.env["mail.activity"]
.with_user(cls.employee)
.create(
{
"activity_type_id": self.activity1.id,
"activity_type_id": cls.activity1.id,
"note": "Partner activity 1.",
"res_id": self.partner_client.id,
"res_model_id": self.partner_ir_model.id,
"user_id": self.employee.id,
"res_id": cls.partner_client.id,
"res_model_id": cls.partner_ir_model.id,
"user_id": cls.employee.id,
}
)
)
self.team1 = (
self.env["mail.activity.team"]
.sudo()
.create(
{
"name": "Team 1",
"res_model_ids": [(6, 0, [self.partner_ir_model.id])],
"member_ids": [(6, 0, [self.employee.id])],
}
)
)
self.team2 = (
self.env["mail.activity.team"]
.sudo()
.create(
{
"name": "Team 2",
"res_model_ids": [(6, 0, [self.partner_ir_model.id])],
"member_ids": [(6, 0, [self.employee.id, self.employee2.id])],
}
)
)
self.act2 = (
self.env["mail.activity"]
.with_user(self.employee)
.create(
{
"activity_type_id": self.activity2.id,
"note": "Partner activity 2.",
"res_id": self.partner_client.id,
"res_model_id": self.partner_ir_model.id,
"user_id": self.employee.id,
}
)
)
self.employee3 = self.env["res.users"].create(
cls.team1 = cls.env["mail.activity.team"].create(
{
"company_id": self.env.ref("base.main_company").id,
"name": "Employee 3",
"login": "csu3",
"email": "crmuser3@yourcompany.com",
"groups_id": [(6, 0, [self.env.ref("base.group_user").id])],
"name": "Team 1",
"res_model_ids": [(6, 0, [cls.partner_ir_model.id])],
"member_ids": [(6, 0, [cls.employee.id])],
}
)
cls.team2 = cls.env["mail.activity.team"].create(
{
"name": "Team 2",
"res_model_ids": [(6, 0, [cls.partner_ir_model.id])],
"member_ids": [(6, 0, [cls.employee.id, cls.employee2.id])],
}
)
cls.act2 = (
cls.env["mail.activity"]
.with_user(cls.employee)
.create(
{
"activity_type_id": cls.activity2.id,
"note": "Partner activity 2.",
"res_id": cls.partner_client.id,
"res_model_id": cls.partner_ir_model.id,
"user_id": cls.employee.id,
}
)
)
def test_activity_members(self):
self.team1.member_ids |= self.employee2
@ -312,3 +297,22 @@ class TestMailActivityTeam(TransactionCase):
self.assertTrue(next_activities)
self.assertEqual(next_activities.team_id, self.team2)
self.assertEqual(next_activities.user_id, self.employee2)
def test_schedule_activity_from_server_action(self):
partner = self.env["res.partner"].create({"name": "Test Partner"})
action = self.env["ir.actions.server"].create(
{
"name": "Test Server Action",
"model_id": self.partner_ir_model.id,
"state": "next_activity",
"activity_type_id": self.activity1.id,
"activity_user_type": "specific",
"activity_user_id": self.employee.id,
"activity_team_id": self.team1.id,
}
)
action.with_context(active_model=partner._name, active_ids=partner.ids).run()
self.assertEqual(partner.activity_ids[-1].team_id, self.team1)
action.activity_team_id = self.team2
action.with_context(active_model=partner._name, active_ids=partner.ids).run()
self.assertEqual(partner.activity_ids[-1].team_id, self.team2)

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2023 Camptocamp SA (https://www.camptocamp.com).
@author Iván Todorovich <ivan.todorovich@camptocamp.com>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="view_server_action_form_template" model="ir.ui.view">
<field name="model">ir.actions.server</field>
<field name="inherit_id" ref="mail.view_server_action_form_template" />
<field name="arch" type="xml">
<field name="activity_user_id" position="after">
<field
name="activity_team_id"
attrs="{'invisible': [('activity_user_type', '!=', 'specific')]}"
/>
</field>
</field>
</record>
</odoo>