social/mail_activity_team/models/mail_activity.py

86 lines
3.4 KiB
Python
Raw Normal View History

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")
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
user_id = fields.Many2one(required=False)
team_user_id = fields.Many2one(related="user_id", readonly=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):
if not self.user_id or (
self.team_id and self.user_id in self.team_id.member_ids
):
return
2018-11-27 12:22:49 +01:00
self.team_id = self.with_context(
2022-06-14 13:58:34 +02:00
default_res_model=self.sudo().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 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):
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:
2019-10-24 13:47:32 +02:00
self.user_id = self.env["res.users"]
2018-11-27 12:22:49 +01:00
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):
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(
_(
"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,
)
)
@api.onchange("activity_type_id")
def _onchange_activity_type_id(self):
2022-06-14 13:58:34 +02:00
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]
2022-06-14 13:58:34 +02:00
return res