social/mail_activity_team/models/mail_activity_team.py

70 lines
2.6 KiB
Python
Raw Normal View History

# Copyright 2018 Eficent Business and IT Consulting Services, S.L.
# Copyright 2021 Sodexis
2018-11-27 09:35:55 +01:00
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
2018-11-27 12:22:49 +01:00
from odoo import api, fields, models
2018-11-27 09:35:55 +01:00
class MailActivityTeam(models.Model):
_name = "mail.activity.team"
2019-10-24 13:47:32 +02:00
_description = "Mail Activity Team"
2018-11-27 09:35:55 +01:00
2019-10-24 13:47:32 +02:00
@api.depends("res_model_ids", "member_ids")
2018-11-27 12:22:49 +01:00
def _compute_missing_activities(self):
2019-10-24 13:47:32 +02:00
activity_model = self.env["mail.activity"]
2018-11-27 12:22:49 +01:00
for team in self:
2019-10-24 13:47:32 +02:00
domain = [("team_id", "=", False)]
2018-11-27 12:22:49 +01:00
if team.member_ids:
2019-10-24 13:47:32 +02:00
domain.append(("user_id", "in", team.member_ids.ids))
2018-11-27 12:22:49 +01:00
if team.res_model_ids:
2019-10-24 13:47:32 +02:00
domain.append(("res_model_id", "in", team.res_model_ids.ids))
team.count_missing_activities = activity_model.search(domain, count=True)
2018-11-27 12:22:49 +01:00
2019-10-24 13:47:32 +02:00
name = fields.Char(string="Name", required=True, translate=True)
active = fields.Boolean(string="Active", default=True)
2018-11-27 12:22:49 +01:00
res_model_ids = fields.Many2many(
2019-10-24 13:47:32 +02:00
comodel_name="ir.model",
string="Used models",
2018-11-27 12:22:49 +01:00
domain=lambda self: [
2019-10-24 13:47:32 +02:00
(
"model",
"in",
[
k
for k in self.env.registry
if issubclass(
type(self.env[k]), type(self.env["mail.activity.mixin"])
)
and self.env[k]._auto
],
)
2018-11-27 12:22:49 +01:00
],
)
member_ids = fields.Many2many(
2019-10-24 13:47:32 +02:00
comodel_name="res.users",
relation="mail_activity_team_users_rel",
2018-11-27 12:22:49 +01:00
string="Team Members",
)
2019-10-24 13:47:32 +02:00
user_id = fields.Many2one(comodel_name="res.users", string="Team Leader")
2018-11-27 12:22:49 +01:00
count_missing_activities = fields.Integer(
2019-10-24 13:47:32 +02:00
string="Missing Activities", compute="_compute_missing_activities", default=0
2018-11-27 12:22:49 +01:00
)
2019-10-24 13:47:32 +02:00
@api.onchange("user_id")
def _onchange_user_id(self):
if self.user_id and self.user_id not in self.member_ids:
members_ids = self.member_ids.ids
members_ids.append(self.user_id.id)
self.member_ids = [(4, member) for member in members_ids]
2018-11-27 12:22:49 +01:00
def assign_team_to_unassigned_activities(self):
2019-10-24 13:47:32 +02:00
activity_model = self.env["mail.activity"]
2018-11-27 12:22:49 +01:00
for team in self:
2019-10-24 13:47:32 +02:00
domain = [("team_id", "=", False)]
2018-11-27 12:22:49 +01:00
if team.member_ids:
2019-10-24 13:47:32 +02:00
domain.append(("user_id", "in", team.member_ids.ids))
2018-11-27 12:22:49 +01:00
if team.res_model_ids:
2019-10-24 13:47:32 +02:00
domain.append(("res_model_id", "in", team.res_model_ids.ids))
2018-11-27 12:22:49 +01:00
missing_activities = activity_model.search(domain)
for missing_activity in missing_activities:
2019-10-24 13:47:32 +02:00
missing_activity.write({"team_id": team.id})