[CLEAN] mail_thread: cleaned code of get_empty_list_help (using variables instead...
[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 openerp import SUPERUSER_ID
23 from openerp.osv import osv
24 from openerp.osv import fields
25
26
27 class ir_ui_menu(osv.osv):
28     """ Override of ir.ui.menu class. When adding mail_thread module, each
29         new mail.group will create a menu entry. This overrides checks that
30         the current user is in the mail.group followers. If not, the menu
31         entry is taken off the list of menu ids. This way the user will see
32         menu entries for the mail.group he is following.
33     """
34     _inherit = 'ir.ui.menu'
35
36     _columns = {
37         'mail_group_id': fields.many2one('mail.group', 'Mail Group')
38     }
39
40     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
41         """ Override to take off menu entries (mail.group) the user is not
42             following. Access are done using SUPERUSER_ID to avoid access
43             rights issues for an internal back-end algorithm. """
44         ids = super(ir_ui_menu, self).search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=False)
45         partner_id = self.pool.get('res.users').read(cr, uid, uid, ['partner_id'], context=context)['partner_id'][0]
46         follower_obj = self.pool.get('mail.followers')
47         for menu in self.browse(cr, uid, ids, context=context):
48             if menu.mail_group_id:
49                 sub_ids = follower_obj.search(cr, SUPERUSER_ID, [
50                     ('partner_id', '=', partner_id),
51                     ('res_model', '=', 'mail.group'),
52                     ('res_id', '=', menu.mail_group_id.id)
53                     ], context=context)
54                 if not sub_ids:
55                     ids.remove(menu.id)
56         if count:
57             return len(ids)
58         return ids