From 542dec7931250bbb9cc7ec5f9e1567bb9e5be858 Mon Sep 17 00:00:00 2001 From: mreficent Date: Tue, 27 Nov 2018 12:22:49 +0100 Subject: [PATCH] [IMP] Some improvements and fixes --- mail_activity_team/models/mail_activity.py | 70 ++++++++++++++----- .../models/mail_activity_team.py | 69 ++++++++++++++++-- mail_activity_team/models/res_users.py | 8 ++- .../security/ir.model.access.csv | 2 +- .../security/mail_activity_team_security.xml | 4 +- .../views/mail_activity_team_views.xml | 16 +++-- 6 files changed, 134 insertions(+), 35 deletions(-) diff --git a/mail_activity_team/models/mail_activity.py b/mail_activity_team/models/mail_activity.py index 51cf198..46dd6de 100644 --- a/mail_activity_team/models/mail_activity.py +++ b/mail_activity_team/models/mail_activity.py @@ -1,34 +1,66 @@ # Copyright 2018 Eficent Business and IT Consulting Services, S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import api, models, fields +from odoo import api, models, fields, _ +from odoo.exceptions import ValidationError class MailActivity(models.Model): _inherit = "mail.activity" - def _get_default_team_id(self): - res_model = self.env.context.get('default_res_model', False) - model = self.env['ir.model'].search([('model', '=', res_model)], - limit=1) - domain = [('member_ids', 'in', [self.env.uid])] + def _get_default_team_id(self, user_id=None): + if not user_id: + user_id = self.env.uid + 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])] if res_model: domain.extend(['|', ('res_model_ids', '=', False), ('res_model_ids', 'in', model.ids)]) return self.env['mail.activity.team'].search(domain, limit=1) - team_id = fields.Many2one('mail.activity.team', - default=lambda s: s._get_default_team_id(),) + team_id = fields.Many2one( + comodel_name='mail.activity.team', + default=lambda s: s._get_default_team_id(), + ) - @api.onchange('res_model_id', 'user_id') - def _onchange_model_user(self): + @api.onchange('user_id') + def _onchange_user_id(self): res = {'domain': {'team_id': []}} - if self.team_id: - if self.user_id not in self.team_id.member_ids: - self.team_id = False - if self.res_model_id: - res['domain']['team_id'] = [ - ('res_model_ids', 'in', self.res_model_id.ids)] - if self.user_id: - res['domain']['team_id'] = [ - ('member_ids', 'in', self.user_id.ids)] + 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 + self.team_id = self.with_context( + default_res_model=self.res_model_id.id).\ + _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 and self.user_id in self.team_id.member_ids: + return res + if self.team_id.user_id: + self.user_id = self.team_id.user_id + elif self.env.user in self.team_id.member_ids.ids: + self.user_id = self.env.user + else: + self.user_id = self.env['res.users'] + return res + + @api.multi + @api.constrains('team_id', 'user_id') + def _check_team_and_user(self): + for activity in self: + if activity.team_id and activity.user_id and \ + activity.user_id not in self.team_id.member_ids: + raise ValidationError( + _('The assigned user is not member of the team.')) diff --git a/mail_activity_team/models/mail_activity_team.py b/mail_activity_team/models/mail_activity_team.py index 81d0676..f97c185 100644 --- a/mail_activity_team/models/mail_activity_team.py +++ b/mail_activity_team/models/mail_activity_team.py @@ -1,14 +1,71 @@ # Copyright 2018 Eficent Business and IT Consulting Services, S.L. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import fields, models +from odoo import api, fields, models class MailActivityTeam(models.Model): _name = "mail.activity.team" _description = 'Mail Activity Team' - name = fields.Char(string='Name', required=True, translate=True) - active = fields.Boolean(string='Active', default=True) - res_model_ids = fields.Many2many('ir.model', string='Used models') - member_ids = fields.Many2many('res.users', 'mail_activity_team_users_rel', - string="Team Members") + @api.depends('res_model_ids', 'member_ids') + def _compute_missing_activities(self): + activity_model = self.env['mail.activity'] + for team in self: + domain = [('team_id', '=', False)] + if team.member_ids: + domain.append(('user_id', 'in', team.member_ids.ids)) + if team.res_model_ids: + domain.append(('res_model_id', 'in', team.res_model_ids.ids)) + team.count_missing_activities = activity_model.search( + domain, count=True) + + name = fields.Char( + string='Name', + required=True, + translate=True, + ) + active = fields.Boolean( + string='Active', + default=True, + ) + res_model_ids = fields.Many2many( + comodel_name='ir.model', + string='Used models', + domain=lambda self: [ + ('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]) + ], + ) + member_ids = fields.Many2many( + comodel_name='res.users', + relation='mail_activity_team_users_rel', + string="Team Members", + ) + user_id = fields.Many2one( + comodel_name='res.users', + string='Team Leader', + domain="[('id', 'in', member_ids)]", + ) + count_missing_activities = fields.Integer( + string="Missing Activities", + compute='_compute_missing_activities', + default=0, + ) + + @api.onchange('member_ids') + def _onchange_member_ids(self): + if self.user_id and self.user_id not in self.member_ids: + self.user_id = False + + def assign_team_to_unassigned_activities(self): + activity_model = self.env['mail.activity'] + for team in self: + domain = [('team_id', '=', False)] + if team.member_ids: + domain.append(('user_id', 'in', team.member_ids.ids)) + if team.res_model_ids: + domain.append(('res_model_id', 'in', team.res_model_ids.ids)) + missing_activities = activity_model.search(domain) + missing_activities.write({'team_id': team.id}) diff --git a/mail_activity_team/models/res_users.py b/mail_activity_team/models/res_users.py index b55ae84..e1f9cc3 100644 --- a/mail_activity_team/models/res_users.py +++ b/mail_activity_team/models/res_users.py @@ -6,6 +6,8 @@ from odoo import models, fields class ResUsers(models.Model): _inherit = "res.users" - activity_team_ids = fields.Many2many('mail.activity.team', - 'mail_activity_team_users_rel', - string="Activity Teams") + activity_team_ids = fields.Many2many( + comodel_name='mail.activity.team', + relation='mail_activity_team_users_rel', + string="Activity Teams", + ) diff --git a/mail_activity_team/security/ir.model.access.csv b/mail_activity_team/security/ir.model.access.csv index c5efccf..0a9918d 100644 --- a/mail_activity_team/security/ir.model.access.csv +++ b/mail_activity_team/security/ir.model.access.csv @@ -1,4 +1,4 @@ "id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink" access_mail_activity_team_all,mail.activity.team.all,model_mail_activity_team,,1,0,0,0 access_mail_activity_team_user,mail.activity.team.user,model_mail_activity_team,base.group_user,1,1,0,0 -access_mail_activity_team_system_user,mail.activity.team.system.user,model_mail_activity_team,base.group_system_user,1,1,1,1 +access_mail_activity_team_system_user,mail.activity.team.system.user,model_mail_activity_team,base.group_system,1,1,1,1 diff --git a/mail_activity_team/security/mail_activity_team_security.xml b/mail_activity_team/security/mail_activity_team_security.xml index 9ac6363..e19398a 100644 --- a/mail_activity_team/security/mail_activity_team_security.xml +++ b/mail_activity_team/security/mail_activity_team_security.xml @@ -4,10 +4,10 @@ mail.activity: user: my team - ['|', '&'('team_id', '=', False),('user_id', '=', user.id),('team_id', '=', user.team_ids.ids)] + ["|", ('team_id', 'in', user.activity_team_ids.ids), "&", ('team_id', '=', False), ('user_id', '=', user.id)] - + diff --git a/mail_activity_team/views/mail_activity_team_views.xml b/mail_activity_team/views/mail_activity_team_views.xml index 52203d2..72b49a1 100644 --- a/mail_activity_team/views/mail_activity_team_views.xml +++ b/mail_activity_team/views/mail_activity_team_views.xml @@ -4,26 +4,34 @@ VIEWS --> - mail.activity.team.view.form mail.activity.team
+
+ +
+ options='{"terminology": "archive"}'/>
+ - +