social/mail_activity_team/models/mail_activity.py

76 lines
2.9 KiB
Python
Raw Normal View History

2018-11-27 09:35:55 +01:00
# Copyright 2018 Eficent Business and IT Consulting Services, S.L.
# 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.env["ir.model"].search([("model", "=", res_model)], limit=1)
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
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(
2019-10-24 13:47:32 +02:00
default_res_model=self.res_model_id.id
)._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)]
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):
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
2019-10-24 13:47:32 +02:00
if (
activity.user_id.id != SUPERUSER_ID
and activity.team_id
and activity.user_id
and activity.user_id not in self.team_id.member_ids
):
2018-11-27 12:22:49 +01:00
raise ValidationError(
2019-10-24 13:47:32 +02:00
_("The assigned user is not member of " "the team.")
)