Skip to content

Commit 7779f36

Browse files
committed
[IMP] mail_activity_team: Enhance activity notification logic and improve team user assignment
1 parent 20b7191 commit 7779f36

File tree

6 files changed

+375
-7
lines changed

6 files changed

+375
-7
lines changed

mail_activity_team/models/mail_activity.py

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Copyright 2018-22 ForgeFlow S.L.
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
33

4-
from odoo import SUPERUSER_ID, _, api, fields, models
4+
from odoo import SUPERUSER_ID, api, fields, models
55
from odoo.exceptions import ValidationError
6+
from odoo.tools.misc import get_lang
67

78

89
class MailActivity(models.Model):
@@ -47,7 +48,26 @@ def create(self, vals_list):
4748
# so if we don't have a user_team_id we don't want user_id too
4849
if "user_id" in vals and not vals.get("team_user_id", False):
4950
del vals["user_id"]
50-
return super().create(vals_list)
51+
52+
activities = super().create(vals_list)
53+
54+
# when creating activities for other: send a notification to assigned user;
55+
if not self.env.context.get("mail_activity_quick_update"):
56+
activities.action_notify_team()
57+
return activities
58+
59+
def write(self, values):
60+
new_team_activities = self.env["mail.activity"]
61+
if values.get("team_id"):
62+
new_team_activities = self.filtered(
63+
lambda activity: activity.user_id.id != values.get("team_id")
64+
)
65+
res = super().write(values)
66+
# notify new responsibles
67+
if "team_id" in values:
68+
if not self.env.context.get("mail_activity_quick_update", False):
69+
new_team_activities.action_notify_team()
70+
return res
5171

5272
@api.onchange("user_id")
5373
def _onchange_user_id(self):
@@ -88,7 +108,7 @@ def _check_team_and_user(self):
88108
not in activity.team_id.with_context(active_test=False).member_ids
89109
):
90110
raise ValidationError(
91-
_(
111+
self.env._(
92112
"The assigned user %(user_name)s is "
93113
"not member of the team %(team_name)s.",
94114
user_name=activity.user_id.name,
@@ -105,3 +125,77 @@ def _onchange_activity_type_id(self):
105125
if self.user_id not in members and members:
106126
self.user_id = members[:1]
107127
return res
128+
129+
def action_notify_team(self):
130+
# Check if any activity has a team with notify_members enabled
131+
classified = self._classify_by_model()
132+
for model, activity_data in classified.items():
133+
records_sudo = self.env[model].sudo().browse(activity_data["record_ids"])
134+
activity_data["record_ids"] = (
135+
records_sudo.exists().ids
136+
) # in case record was cascade-deleted in DB, skipping unlink override
137+
for activity in self:
138+
if activity.res_id not in classified[activity.res_model]["record_ids"]:
139+
continue
140+
if activity.team_id and activity.team_id.notify_members:
141+
# Get the record and model description
142+
model_description = (
143+
activity.env["ir.model"]._get(activity.res_model).display_name
144+
)
145+
record = activity.env[activity.res_model].browse(activity.res_id)
146+
# Notify each team member except the assigned user and the current user
147+
for member in activity.team_id.member_ids.filtered(
148+
lambda m, assigned_user_id=activity.user_id: self.env.uid
149+
not in m.user_ids.ids
150+
and (
151+
not assigned_user_id
152+
or (assigned_user_id and assigned_user_id not in m.user_ids)
153+
)
154+
):
155+
if member.lang:
156+
# Send notification in member's language
157+
activity_ctx = activity.with_context(lang=member.lang)
158+
else:
159+
activity_ctx = activity
160+
body = activity_ctx.env["ir.qweb"]._render(
161+
"mail.message_activity_assigned",
162+
{
163+
"activity": activity_ctx,
164+
"model_description": model_description,
165+
"is_html_empty": lambda x: not x or x == "<p><br></p>",
166+
},
167+
minimal_qcontext=True,
168+
)
169+
record.message_notify(
170+
partner_ids=member.sudo().partner_id.ids,
171+
body=body,
172+
record_name=activity_ctx.res_name,
173+
model_description=model_description,
174+
email_layout_xmlid="mail.mail_notification_layout",
175+
subject=self.env._(
176+
"%(activity_name)s: %(summary)s (Team Activity)",
177+
activity_name=activity_ctx.res_name,
178+
summary=activity_ctx.summary
179+
or activity_ctx.activity_type_id.name,
180+
),
181+
subtitles=[
182+
self.env._(
183+
"Activity: %s", activity_ctx.activity_type_id.name
184+
),
185+
self.env._("Team: %s", activity_ctx.team_id.name),
186+
self.env._(
187+
"Deadline: %s",
188+
activity_ctx.date_deadline.strftime(
189+
get_lang(activity_ctx.env).date_format
190+
)
191+
if hasattr(activity_ctx.date_deadline, "strftime")
192+
else str(activity_ctx.date_deadline),
193+
),
194+
],
195+
)
196+
197+
def action_notify(self):
198+
"""Override to notify team members when notify_members is enabled."""
199+
result = super().action_notify()
200+
self.action_notify_team()
201+
return result

mail_activity_team/models/mail_activity_team.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def _compute_missing_activities(self):
4545
string="Team Members",
4646
)
4747
user_id = fields.Many2one(comodel_name="res.users", string="Team Leader")
48+
notify_members = fields.Boolean(
49+
default=False,
50+
help="When enabled, all team members will be notified "
51+
"when an activity is assigned to this team.",
52+
)
4853
count_missing_activities = fields.Integer(
4954
string="Missing Activities", compute="_compute_missing_activities", default=0
5055
)

0 commit comments

Comments
 (0)