~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to msf_button_access_rights/button_access_rule.py

  • Committer: mmu-openerp
  • Date: 2013-03-13 11:05:40 UTC
  • mfrom: (1349.29.95 button-wm)
  • mto: This revision was merged to the branch mainline in revision 1524.
  • Revision ID: mmu@openerp.com-20130313110540-gk589kzzzkw5ojro
[MERGE] uf-1652 button access rights

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- encoding: utf-8 -*-
 
3
##############################################################################
 
4
#
 
5
#    OpenERP, Open Source Management Solution
 
6
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
 
7
#    Developer: Max Mumford
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU Affero General Public License as
 
11
#    published by the Free Software Foundation, either version 3 of the
 
12
#    License, or (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU Affero General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU Affero General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from osv import osv
 
25
from osv import fields
 
26
from osv import orm
 
27
import psycopg2
 
28
 
 
29
class button_access_rule(osv.osv):
 
30
    """
 
31
    Lets user create access rules for buttons in views.
 
32
    This class defines which model, view, button, and groups to target
 
33
    """
 
34
 
 
35
    _name = "msf_button_access_rights.button_access_rule"
 
36
    
 
37
    def _get_group_names(self, cr, uid, ids, field_name, arg, context):
 
38
        res = dict.fromkeys(ids, '')
 
39
        records = self.browse(cr, uid, ids)
 
40
        for record in records:
 
41
            res[record.id] = ', '.join([g.name for g in record.group_ids])
 
42
        return res
 
43
 
 
44
    _columns = {
 
45
        'name': fields.char('Name', size=256, required=True),
 
46
        'label': fields.char('Label', size=256),
 
47
        'type': fields.selection((('workflow','Workflow'), ('object','Object'), ('action', 'Action')), 'Button Type'),
 
48
        'model_id': fields.many2one('ir.model', 'Model', help='The type of data to which this rule applies', required=True, ondelete='cascade'),
 
49
        'view_id': fields.many2one('ir.ui.view', 'View', help='The view to which this rule applies', required=True, ondelete='cascade'),
 
50
        'group_ids': fields.many2many('res.groups', 'button_access_rule_groups_rel', 'button_access_rule_id', 'group_id', 'Groups', help='A list of groups who have access to this button. If you leave this empty, everybody will have access.'),
 
51
        'comment': fields.text('Comment', help='A description of what this rule does'),
 
52
        'group_names': fields.function(_get_group_names, type='char', method=True, string='Group Names', help='A list of all group names given button access by this rule'),
 
53
        'active': fields.boolean('Active', help='If checked, this rule will be applied.'),
 
54
    }
 
55
 
 
56
    _defaults = {
 
57
        'active': True,
 
58
    }
 
59
    
 
60
    _sql_constraints = [
 
61
        ('name_view_unique', 'unique (name, view_id)', "The combination of Button Name and View ID must be unique - i.e. you cannot have two rules for the same button in the same view"),
 
62
    ]
 
63
    
 
64
    def _get_family_ids(self, cr, view_id):
 
65
        """
 
66
        Return a list of ids for all the children of view_id (and contains the view_id itself)
 
67
        """
 
68
        family_ids = [view_id]
 
69
        last_ids = [view_id]
 
70
        view_pool = self.pool.get('ir.ui.view')
 
71
        
 
72
        while(last_ids):
 
73
            last_ids = view_pool.search(cr, 1, [('inherit_id','in',last_ids)])
 
74
            family_ids = family_ids + last_ids
 
75
            
 
76
        return family_ids
 
77
               
 
78
button_access_rule()