Merge PR #922 into 15.0

Signed-off-by pedrobaeza
This commit is contained in:
OCA-git-bot 2022-12-20 21:04:53 +00:00
commit 40d2c6854a
11 changed files with 307 additions and 72 deletions

View File

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

View File

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

View File

@ -1,5 +1,6 @@
# Copyright 2018-22 ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import SUPERUSER_ID, _, api, fields, models
from odoo.exceptions import ValidationError
@ -19,41 +20,32 @@ 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()
)
@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.res_model_id.id
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):
@ -81,3 +73,13 @@ class MailActivity(models.Model):
team_name=activity.team_id.name,
)
)
@api.onchange("activity_type_id")
def _onchange_activity_type_id(self):
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

View File

@ -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)
)
act_values.update({"team_id": team.id})
return super().activity_schedule(
act_type_xmlid=act_type_xmlid,
date_deadline=date_deadline,

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

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

View File

@ -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
@ -76,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;
@ -89,6 +94,17 @@ 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;
});
},
_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);
},
});

View File

@ -7,13 +7,13 @@
<div>
<button
type="button"
class="btn btn-link o_filter_button active"
class="my_activities btn btn-link o_filter_button active"
data-filter='my'
role="tab"
> My Activities </button>
<button
type="button"
class="btn btn-link o_filter_button"
class="team_activities btn btn-link o_filter_button"
data-filter='team'
role="tab"
> Team Activities </button>

View File

@ -1,13 +1,15 @@
# 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, TransactionCase
class TestMailActivityTeam(TransactionCase):
def setUp(self):
super(TestMailActivityTeam, self).setUp()
@classmethod
def setUpClass(cls):
super().setUpClass()
self = cls
self.env["mail.activity.team"].search([]).unlink()
self.employee = self.env["res.users"].create(
@ -125,6 +127,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,31 +158,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._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
@ -179,3 +245,70 @@ 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)
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(
{
"default_team_id": self.team1.id,
"triggered_next_type_id": self.activity2.id,
}
)
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>

View File

@ -6,8 +6,22 @@
<field name="model">mail.activity</field>
<field name="inherit_id" ref="mail.mail_activity_view_form_popup" />
<field name="arch" type="xml">
<field name="user_id" position="attributes">
<attribute
name="attrs"
>{'invisible': [('team_id', '!=', False)]}</attribute>
</field>
<field name="user_id" position="after">
<field name="team_id" options="{'no_create': True, 'no_open': True}" />
<field
name="team_user_id"
attrs="{'invisible': [('team_id', '=', False)]}"
domain="[('activity_team_ids', '=', team_id)]"
/>
<field
name="team_id"
options="{'no_create': True, 'no_open': True}"
domain="['|', ('res_model_ids', '=', False), ('res_model_ids', '=', res_model_id)]"
/>
</field>
</field>
</record>
@ -31,8 +45,21 @@
ref="mail_activity_board.mail_activity_view_form_board"
/>
<field name="arch" type="xml">
<field name="user_id" position="attributes">
<attribute
name="attrs"
>{'invisible': [('team_id', '=', False)]}</attribute>
</field>
<field name="user_id" position="after">
<field name="team_id" />
<field
name="team_user_id"
attrs="{'invisible': [('team_id', '!=', False)]}"
domain="[('activity_team_ids', '=', team_id)]"
/>
<field
name="team_id"
domain="['|', ('res_model_ids', '=', False), ('res_model_ids', '=', res_model_id)]"
/>
</field>
</field>
</record>