1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import fields,osv
28 class ir_rule_group(osv.osv):
29 _name = 'ir.rule.group'
32 'name': fields.char('Name', size=128, select=1),
33 'model_id': fields.many2one('ir.model', 'Object',select=1, required=True),
34 'global': fields.boolean('Global', select=1, help="Make the rule global or it needs to be put on a group or user"),
35 'rules': fields.one2many('ir.rule', 'rule_group', 'Tests', help="The rule is satisfied if at least one test is True"),
36 'groups': fields.many2many('res.groups', 'group_rule_group_rel', 'rule_group_id', 'group_id', 'Groups'),
37 'users': fields.many2many('res.users', 'user_rule_group_rel', 'rule_group_id', 'user_id', 'Users'),
40 _order = 'model_id, global DESC'
43 'global': lambda *a: True,
46 def unlink(self, cr, uid, ids, context=None):
47 res = super(ir_rule_group, self).unlink(cr, uid, ids, context=context)
48 # Restart the cache on the domain_get method of ir.rule
49 self.pool.get('ir.rule').domain_get.clear_cache(cr.dbname)
52 def create(self, cr, user, vals, context=None):
53 res = super(ir_rule_group, self).create(cr, user, vals, context=context)
54 # Restart the cache on the domain_get method of ir.rule
55 self.pool.get('ir.rule').domain_get.clear_cache(cr.dbname)
58 def write(self, cr, uid, ids, vals, context=None):
59 res = super(ir_rule_group, self).write(cr, uid, ids, vals, context=context)
60 # Restart the cache on the domain_get method of ir.rule
61 self.pool.get('ir.rule').domain_get.clear_cache(cr.dbname)
67 class ir_rule(osv.osv):
69 _rec_name = 'field_id'
71 def _operand(self,cr,uid,context):
73 def get(object, level=3, recur=None, root_tech='', root=''):
77 fields = self.pool.get(object).fields_get(cr,uid)
82 if fields[k]['type'] in ('many2one'):
83 res.append((root_tech+'.'+k+'.id',
84 root+'/'+fields[k]['string']))
86 elif fields[k]['type'] in ('many2many', 'one2many'):
87 res.append(('\',\'.join(map(lambda x: str(x.id), '+root_tech+'.'+k+'))',
88 root+'/'+fields[k]['string']))
91 res.append((root_tech+'.'+k,
92 root+'/'+fields[k]['string']))
94 if (fields[k]['type'] in recur) and (level>0):
95 res.extend(get(fields[k]['relation'], level-1,
96 recur, root_tech+'.'+k, root+'/'+fields[k]['string']))
100 res = [("False", "False"), ("True", "True"), ("user.id", "User")]
101 res += get('res.users', level=1,
102 recur=['many2one'], root_tech='user', root='User')
105 def _domain_force_get(self, cr, uid, ids, field_name, arg, context={}):
107 for rule in self.browse(cr, uid, ids, context):
108 if rule.domain_force:
109 res[rule.id] = eval(rule.domain_force, {'user': self.pool.get('res.users').browse(cr, 1, uid),
112 if rule.operator in ('in', 'child_of'):
113 dom = eval("[('%s', '%s', [%s])]" % (rule.field_id.name, rule.operator,
114 rule.operand), {'user': self.pool.get('res.users').browse(cr, 1, uid),
117 dom = eval("[('%s', '%s', %s)]" % (rule.field_id.name, rule.operator,
118 rule.operand), {'user': self.pool.get('res.users').browse(cr, 1, uid),
124 'field_id': fields.many2one('ir.model.fields', 'Field',domain= "[('model_id','=', parent.model_id)]", select=1, required=True),
125 'operator':fields.selection((('=', '='), ('<>', '<>'), ('<=', '<='), ('>=', '>='), ('in', 'in'), ('child_of', 'child_of')), 'Operator', required=True),
126 'operand':fields.selection(_operand,'Operand', size=64, required=True),
127 'rule_group': fields.many2one('ir.rule.group', 'Group', select=2, required=True, ondelete="cascade"),
128 'domain_force': fields.char('Force Domain', size=250),
129 'domain': fields.function(_domain_force_get, method=True, string='Domain', type='char', size=250)
132 def onchange_all(self, cr, uid, ids, field_id, operator, operand):
133 if not (field_id or operator or operand):
136 def domain_get(self, cr, uid, model_name):
137 # root user above constraint
141 cr.execute("""SELECT r.id FROM
143 JOIN (ir_rule_group g
144 JOIN ir_model m ON (g.model_id = m.id))
145 ON (g.id = r.rule_group)
147 AND (g.id IN (SELECT rule_group_id FROM group_rule_group_rel g_rel
148 JOIN res_groups_users_rel u_rel ON (g_rel.group_id = u_rel.gid)
149 WHERE u_rel.uid = %s) OR g.global)""", (model_name, uid))
150 ids = map(lambda x:x[0], cr.fetchall())
153 obj = self.pool.get(model_name)
160 for rule in self.browse(cr, uid, ids):
162 if rule.rule_group['global']:
163 clause_global.setdefault(rule.rule_group.id, [])
164 clause_global[rule.rule_group.id].append(obj._where_calc(cr, uid, dom, active_test=False))
166 clause.setdefault(rule.rule_group.id, [])
167 clause[rule.rule_group.id].append(obj._where_calc(cr, uid, dom, active_test=False))
169 def _query(clause, test):
172 for g in clause.values():
176 query += ' '+test+' '
195 query, val = _query(clause, 'OR')
196 query_global, val_global = _query(clause_global, 'OR')
199 query = '('+query+') OR '+query_global
200 val.extend(val_global)
207 query = '('+query+')'
209 domain_get = tools.cache()(domain_get)
211 def unlink(self, cr, uid, ids, context=None):
212 res = super(ir_rule, self).unlink(cr, uid, ids, context=context)
213 # Restart the cache on the domain_get method of ir.rule
214 self.domain_get.clear_cache(cr.dbname)
217 def create(self, cr, user, vals, context=None):
218 res = super(ir_rule, self).create(cr, user, vals, context=context)
219 # Restart the cache on the domain_get method of ir.rule
220 self.domain_get.clear_cache(cr.dbname)
223 def write(self, cr, uid, ids, vals, context=None):
226 res = super(ir_rule, self).write(cr, uid, ids, vals, context=context)
227 # Restart the cache on the domain_get method
228 self.domain_get.clear_cache(cr.dbname)
233 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: