[MERGE] mail/chatter complete review/refactoring
[odoo/odoo.git] / addons / mail / mail_group_menu.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2012-today OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 from osv import osv
23 from osv import fields
24 from tools.translate import _
25
26 class ir_ui_menu(osv.osv):
27     """ Override of ir.ui.menu class. When adding mail_thread module, each
28         new mail.group will create a menu entry. This overrides checks that
29         the current user is in the mail.group followers. If not, the menu
30         entry is taken off the list of menu ids. This way the user will see
31         menu entries for the mail.group he is following.
32     """
33     _inherit = 'ir.ui.menu'
34
35     _columns = {
36         'mail_group_id': fields.many2one('mail.group', 'Mail Group')
37     }
38
39     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
40         """ Override to take off menu entries (mail.group) the user is not
41             following. """
42         ids = super(ir_ui_menu, self).search(cr, uid, args, offset=0, limit=None, order=order, context=context, count=False)
43         partner_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).partner_id.id
44         follower_obj = self.pool.get('mail.followers')
45         for menu in self.browse(cr, uid, ids, context=context):
46             if menu.mail_group_id:
47                 sub_ids = follower_obj.search(cr, uid, [
48                     ('partner_id', '=', partner_id), ('res_model', '=', 'mail.group'),
49                     ('res_id', '=', menu.mail_group_id.id)
50                     ], context=context)
51                 if not sub_ids:
52                     ids.remove(menu.id)
53         return ids