2022-04-06 12:09:56 +02:00
|
|
|
# Copyright 2018-22 ForgeFlow S.L.
|
2018-11-27 09:35:55 +01:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
2019-10-24 13:47:32 +02:00
|
|
|
from odoo import SUPERUSER_ID, _, api, fields, models
|
2018-11-27 12:22:49 +01:00
|
|
|
from odoo.exceptions import ValidationError
|
2018-11-27 09:35:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailActivity(models.Model):
|
|
|
|
_inherit = "mail.activity"
|
|
|
|
|
2018-11-27 12:22:49 +01:00
|
|
|
def _get_default_team_id(self, user_id=None):
|
|
|
|
if not user_id:
|
|
|
|
user_id = self.env.uid
|
2019-10-24 13:47:32 +02:00
|
|
|
res_model = self.env.context.get("default_res_model")
|
2022-01-17 12:45:08 +01:00
|
|
|
model = self.sudo().env["ir.model"].search([("model", "=", res_model)], limit=1)
|
2019-10-24 13:47:32 +02:00
|
|
|
domain = [("member_ids", "in", [user_id])]
|
2018-11-27 09:35:55 +01:00
|
|
|
if res_model:
|
2019-10-24 13:47:32 +02:00
|
|
|
domain.extend(
|
|
|
|
["|", ("res_model_ids", "=", False), ("res_model_ids", "in", model.ids)]
|
|
|
|
)
|
|
|
|
return self.env["mail.activity.team"].search(domain, limit=1)
|
2018-11-27 09:35:55 +01:00
|
|
|
|
2022-05-26 17:57:52 +02:00
|
|
|
user_id = fields.Many2one(required=False)
|
|
|
|
|
2018-11-27 12:22:49 +01:00
|
|
|
team_id = fields.Many2one(
|
2019-10-24 13:47:32 +02:00
|
|
|
comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id()
|
2018-11-27 12:22:49 +01:00
|
|
|
)
|
2018-11-27 09:35:55 +01:00
|
|
|
|
2019-10-24 13:47:32 +02:00
|
|
|
@api.onchange("user_id")
|
2018-11-27 12:22:49 +01:00
|
|
|
def _onchange_user_id(self):
|
2019-10-24 13:47:32 +02:00
|
|
|
res = {"domain": {"team_id": []}}
|
2018-11-27 12:22:49 +01:00
|
|
|
if not self.user_id:
|
|
|
|
return res
|
2019-10-24 13:47:32 +02:00
|
|
|
res["domain"]["team_id"] = [
|
|
|
|
"|",
|
|
|
|
("res_model_ids", "=", False),
|
|
|
|
("res_model_ids", "in", self.res_model_id.ids),
|
|
|
|
]
|
2018-11-27 12:22:49 +01:00
|
|
|
if self.team_id and self.user_id in self.team_id.member_ids:
|
|
|
|
return res
|
|
|
|
self.team_id = self.with_context(
|
2022-05-26 21:14:04 +02:00
|
|
|
default_res_model=self.res_model_id.model
|
2019-10-24 13:47:32 +02:00
|
|
|
)._get_default_team_id(user_id=self.user_id.id)
|
2018-11-27 09:35:55 +01:00
|
|
|
return res
|
2018-11-27 12:22:49 +01:00
|
|
|
|
2019-10-24 13:47:32 +02:00
|
|
|
@api.onchange("team_id")
|
2018-11-27 12:22:49 +01:00
|
|
|
def _onchange_team_id(self):
|
2019-10-24 13:47:32 +02:00
|
|
|
res = {"domain": {"user_id": []}}
|
2018-11-27 12:22:49 +01:00
|
|
|
if not self.team_id:
|
|
|
|
return res
|
2019-10-24 13:47:32 +02:00
|
|
|
res["domain"]["user_id"] = [("id", "in", self.team_id.member_ids.ids)]
|
2018-12-21 16:05:49 +01:00
|
|
|
if 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:
|
2019-10-24 13:47:32 +02:00
|
|
|
self.user_id = self.env["res.users"]
|
2018-11-27 12:22:49 +01:00
|
|
|
return res
|
|
|
|
|
2019-10-24 13:47:32 +02:00
|
|
|
@api.constrains("team_id", "user_id")
|
2018-11-27 12:22:49 +01:00
|
|
|
def _check_team_and_user(self):
|
2022-01-13 14:03:42 +01:00
|
|
|
for activity in self:
|
|
|
|
# SUPERUSER is used to put mail.activity on some objects
|
|
|
|
# like sale.order coming from stock.picking
|
|
|
|
# (for example with exception type activity, with no backorder).
|
|
|
|
# SUPERUSER is inactive and then even if you add it
|
|
|
|
# to member_ids it's not taken account
|
|
|
|
# To not be blocked we must add it to constraint condition.
|
|
|
|
# We must consider also users that could be archived but come from
|
|
|
|
# an automatic scheduled activity
|
|
|
|
if (
|
|
|
|
activity.user_id.id != SUPERUSER_ID
|
|
|
|
and activity.team_id
|
|
|
|
and activity.user_id
|
|
|
|
and activity.user_id
|
|
|
|
not in activity.team_id.with_context(active_test=False).member_ids
|
|
|
|
):
|
|
|
|
raise ValidationError(
|
2022-01-17 12:45:08 +01:00
|
|
|
_(
|
|
|
|
"The assigned user %(user_name)s is "
|
|
|
|
"not member of the team %(team_name)s.",
|
|
|
|
user_name=activity.user_id.name,
|
|
|
|
team_name=activity.team_id.name,
|
|
|
|
)
|
2022-01-13 14:03:42 +01:00
|
|
|
)
|
2022-05-26 17:57:52 +02:00
|
|
|
|
|
|
|
@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]
|