diff --git a/mail_activity_team/__manifest__.py b/mail_activity_team/__manifest__.py index 348d267..5b00b74 100644 --- a/mail_activity_team/__manifest__.py +++ b/mail_activity_team/__manifest__.py @@ -15,6 +15,7 @@ "data": [ "security/ir.model.access.csv", "security/mail_activity_team_security.xml", + "views/mail_activity_type.xml", "views/mail_activity_team_views.xml", "views/mail_activity_views.xml", "views/res_users_views.xml", diff --git a/mail_activity_team/models/__init__.py b/mail_activity_team/models/__init__.py index a3143d5..655b0dd 100644 --- a/mail_activity_team/models/__init__.py +++ b/mail_activity_team/models/__init__.py @@ -2,3 +2,4 @@ from . import mail_activity_team from . import mail_activity from . import mail_activity_mixin from . import res_users +from . import mail_activity_type diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 4b740dd..7eb0a20 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -1,5 +1,6 @@ # Copyright 2018-22 ForgeFlow S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + from odoo import SUPERUSER_ID, _, api, fields, models from odoo.exceptions import ValidationError @@ -19,41 +20,32 @@ class MailActivity(models.Model): ) return self.env["mail.activity.team"].search(domain, limit=1) + user_id = fields.Many2one(required=False) + team_user_id = fields.Many2one(related="user_id", readonly=False) + team_id = fields.Many2one( comodel_name="mail.activity.team", default=lambda s: s._get_default_team_id() ) @api.onchange("user_id") def _onchange_user_id(self): - res = {"domain": {"team_id": []}} - if not self.user_id: - return res - res["domain"]["team_id"] = [ - "|", - ("res_model_ids", "=", False), - ("res_model_ids", "in", self.res_model_id.ids), - ] - if self.team_id and self.user_id in self.team_id.member_ids: - return res + if not self.user_id or ( + self.team_id and self.user_id in self.team_id.member_ids + ): + return self.team_id = self.with_context( - default_res_model=self.res_model_id.id + default_res_model=self.sudo().res_model_id.model )._get_default_team_id(user_id=self.user_id.id) - return res @api.onchange("team_id") def _onchange_team_id(self): - res = {"domain": {"user_id": []}} - if not self.team_id: - return res - 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 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: self.user_id = self.env["res.users"] - return res @api.constrains("team_id", "user_id") def _check_team_and_user(self): @@ -81,3 +73,13 @@ class MailActivity(models.Model): team_name=activity.team_id.name, ) ) + + @api.onchange("activity_type_id") + def _onchange_activity_type_id(self): + 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] + return res diff --git a/mail_activity_team/models/mail_activity_mixin.py b/mail_activity_team/models/mail_activity_mixin.py index d9ea56e..46e0fb4 100644 --- a/mail_activity_team/models/mail_activity_mixin.py +++ b/mail_activity_team/models/mail_activity_mixin.py @@ -46,16 +46,35 @@ class MailActivityMixin(models.AbstractModel): user-team missmatch. We can hook onto `act_values` dict as it's passed to the create activity method. """ - user_id = act_values.get("user_id") - if user_id: - team = ( - self.env["mail.activity"] - .with_context( - default_res_model=self._name, + if "team_id" not in act_values: + if act_type_xmlid: + activity_type = self.sudo().env.ref(act_type_xmlid) + else: + activity_type = ( + self.env["mail.activity.type"] + .sudo() + .browse(act_values["activity_type_id"]) ) - ._get_default_team_id(user_id=user_id) - ) - act_values.update({"team_id": team.id}) + if activity_type.default_team_id: + act_values.update({"team_id": activity_type.default_team_id.id}) + if ( + not act_values.get("user_id") + and activity_type.default_team_id.member_ids + ): + act_values.update( + {"user_id": activity_type.default_team_id.member_ids[:1].id} + ) + else: + user_id = act_values.get("user_id") + if user_id: + team = ( + self.env["mail.activity"] + .with_context( + default_res_model=self._name, + ) + ._get_default_team_id(user_id=user_id) + ) + act_values.update({"team_id": team.id}) return super().activity_schedule( act_type_xmlid=act_type_xmlid, date_deadline=date_deadline, diff --git a/mail_activity_team/models/mail_activity_type.py b/mail_activity_team/models/mail_activity_type.py new file mode 100644 index 0000000..f6babce --- /dev/null +++ b/mail_activity_team/models/mail_activity_type.py @@ -0,0 +1,11 @@ +# Copyright 2022 CreuBlanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class MailActivityType(models.Model): + + _inherit = "mail.activity.type" + + default_team_id = fields.Many2one(comodel_name="mail.activity.team") diff --git a/mail_activity_team/models/res_users.py b/mail_activity_team/models/res_users.py index dabdb3b..c43e006 100644 --- a/mail_activity_team/models/res_users.py +++ b/mail_activity_team/models/res_users.py @@ -14,7 +14,7 @@ class ResUsers(models.Model): @api.model def systray_get_activities(self): - if not self._context.get("team_activities", False): + if not self.env.context.get("team_activities"): return super().systray_get_activities() query = """SELECT m.id, count(*), act.res_model as model, CASE @@ -64,6 +64,9 @@ class ResUsers(models.Model): user_activities[activity["model"]][ "%s_count" % activity["states"] ] += activity["count"] - if activity["states"] in ("today", "overdue"): + if ( + activity["states"] in ("today", "overdue") + and activity["user_id"] != user + ): user_activities[activity["model"]]["total_count"] += activity["count"] return list(user_activities.values()) diff --git a/mail_activity_team/static/src/js/systray.esm.js b/mail_activity_team/static/src/js/systray.esm.js index ea10bf8..a356e85 100644 --- a/mail_activity_team/static/src/js/systray.esm.js +++ b/mail_activity_team/static/src/js/systray.esm.js @@ -11,10 +11,11 @@ ActivityMenu.include({ this.$filter_buttons = this.$(".o_filter_button"); this.$my_activities = this.$filter_buttons.first(); this.filter = "my"; - this.user_context = session.user_context; - this.user_context = _.extend({}, session.user_context, { - team_activities: false, - }); + this._update_team_activities_context(); + }, + + _update_team_activities_context: function () { + session.user_context.team_activities = this.filter === "team"; }, _updateCounter: function () { @@ -23,18 +24,13 @@ ActivityMenu.include({ }, _onClickFilterButton: function (event) { - var self = this; event.stopPropagation(); - self.$filter_buttons.removeClass("active"); + this.$filter_buttons.removeClass("active"); var $target = $(event.currentTarget); $target.addClass("active"); - self.filter = $target.data("filter"); - - self.user_context = _.extend({}, session.user_context, { - team_activities: self.filter === "team", - }); - - self._updateActivityPreview(); + this.filter = $target.data("filter"); + this._update_team_activities_context(); + this._updateActivityPreview(); }, _onActivityFilterClick: function (event) { if (this.filter === "my") { @@ -63,12 +59,21 @@ ActivityMenu.include({ [false, "form"], ], search_view_id: [false], - domain: [["activity_team_user_ids", "in", session.uid]], + domain: [["activity_team_user_ids", "in", [session.uid]]], context: context, }); } }, + _open_boards_activities_domain: function () { + if (this.filter === "team") { + return {additional_context: {search_default_my_team_activities: 1}}; + } + return this._super.apply(this, arguments); + }, _getActivityData: function () { + if (this.filter !== "team") { + return this._super.apply(this, arguments); + } var self = this; return self @@ -76,7 +81,7 @@ ActivityMenu.include({ model: "res.users", method: "systray_get_activities", args: [], - kwargs: {context: self.user_context}, + kwargs: {context: session.user_context}, }) .then(function (data) { self._activities = data; @@ -89,6 +94,17 @@ ActivityMenu.include({ ); self.$(".o_notification_counter").text(self.activityCounter); self.$el.toggleClass("o_no_notification", !self.activityCounter); + // Unset context after we gather the info to avoid side effects + session.user_context.team_activities = false; }); }, + _onActivityMenuHide: function () { + this.filter = "my"; + this._update_team_activities_context(); + this._getActivityData(); + this.$filter_buttons.removeClass("active"); + var $target = $(".my_activities"); + $target.addClass("active"); + return this._super.apply(this, arguments); + }, }); diff --git a/mail_activity_team/static/src/xml/systray.xml b/mail_activity_team/static/src/xml/systray.xml index dadf02c..9af724f 100644 --- a/mail_activity_team/static/src/xml/systray.xml +++ b/mail_activity_team/static/src/xml/systray.xml @@ -7,13 +7,13 @@