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 osv,fields
25 from tools.translate import _
27 class ir_values(osv.osv):
30 def _value_unpickle(self, cursor, user, ids, name, arg, context=None):
32 for report in self.browse(cursor, user, ids, context=context):
33 value = report[name[:-9]]
34 if not report.object and value:
36 value = str(pickle.loads(value))
39 res[report.id] = value
42 def _value_pickle(self, cursor, user, id, name, value, arg, context=None):
46 if self.CONCURRENCY_CHECK_FIELD in ctx:
47 del ctx[self.CONCURRENCY_CHECK_FIELD]
48 if not self.browse(cursor, user, id, context=context).object:
49 value = pickle.dumps(eval(value))
50 self.write(cursor, user, id, {name[:-9]: value}, context=ctx)
52 def onchange_object_id(self, cr, uid, ids, object_id, context={}):
53 if not object_id: return {}
54 act = self.pool.get('ir.model').browse(cr, uid, object_id, context=context)
56 'value': {'model': act.model}
59 def onchange_action_id(self, cr, uid, ids, action_id, context={}):
60 if not action_id: return {}
61 act = self.pool.get('ir.actions.actions').browse(cr, uid, action_id, context=context)
63 'value': {'value_unpickle': act.type+','+str(act.id)}
67 'name': fields.char('Name', size=128),
68 'model_id': fields.many2one('ir.model', 'Object', size=128,
69 help="This field is not used, it only helps you to select a good model."),
70 'model': fields.char('Object Name', size=128),
71 'action_id': fields.many2one('ir.actions.actions', 'Action',
72 help="This field is not used, it only helps you to select the right action."),
73 'value': fields.text('Value'),
74 'value_unpickle': fields.function(_value_unpickle, fnct_inv=_value_pickle,
75 method=True, type='text', string='Value'),
76 'object': fields.boolean('Is Object'),
77 'key': fields.selection([('action','Action'),('default','Default')], 'Type', size=128),
78 'key2': fields.char('Event Type', size=256,
79 help="The kind of action or button in the client side that will trigger the action."),
80 'meta': fields.text('Meta Datas'),
81 'meta_unpickle': fields.function(_value_unpickle, fnct_inv=_value_pickle,
82 method=True, type='text', string='Metadata'),
83 'res_id': fields.integer('Object ID', help="Keep 0 if the action must appear on all resources."),
84 'user_id': fields.many2one('res.users', 'User', ondelete='cascade'),
85 'company_id': fields.many2one('res.company', 'Company')
88 'key': lambda *a: 'action',
89 'key2': lambda *a: 'tree_but_open',
90 'company_id': lambda *a: False
93 def _auto_init(self, cr, context={}):
94 super(ir_values, self)._auto_init(cr, context)
95 cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_values_key_model_key2_index\'')
97 cr.execute('CREATE INDEX ir_values_key_model_key2_index ON ir_values (key, model, key2)')
100 def set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=False, preserve_user=False, company=False):
101 if type(value)==type(u''):
102 value = value.encode('utf8')
104 value = pickle.dumps(value)
106 meta = pickle.dumps(meta)
109 if type(model)==type([]) or type(model)==type(()):
114 if key in ('meta', 'default'):
115 ids = self.search(cr, uid, [
119 ('model', '=', model),
120 ('res_id', '=', res_id),
121 ('user_id', '=', preserve_user and uid)
124 ids = self.search(cr, uid, [
127 ('value', '=', value),
128 ('model', '=', model),
129 ('res_id', '=', res_id),
130 ('user_id', '=', preserve_user and uid)
132 self.unlink(cr, uid, ids)
139 'key2': key2 and key2[:200],
141 'user_id': preserve_user and uid,
144 cid = self.pool.get('res.users').browse(cr, uid, uid, context={}).company_id.id
145 vals['company_id']=cid
147 vals['res_id']= res_id
148 ids_res.append(self.create(cr, uid, vals))
151 def get(self, cr, uid, key, key2, models, meta=False, context={}, res_id_req=False, without_user=True, key2_req=True):
154 if type(m)==type([]) or type(m)==type(()):
159 where1 = ['key=%s','model=%s']
160 where2 = [key,str(m)]
163 where1.append('key2=%s')
164 where2.append(key2[:200])
167 if not key2_req or meta:
169 dest.append('key2 is null')
171 if res_id_req and (models[-1][0]==m):
173 where1.append('res_id=%d' % (res_id,))
175 where1.append('(res_id is NULL)')
177 if (models[-1][0]==m):
178 where1.append('(res_id=%d or (res_id is null))' % (res_id,))
179 where_opt.append('res_id=%d' % (res_id,))
181 where1.append('res_id=%d' % (res_id,))
183 # if not without_user:
184 where_opt.append('user_id=%d' % (uid,))
191 cr.execute('select id,name,value,object,meta, key from ir_values where ' +\
192 ' and '.join(where1)+' and user_id is null', where2)
194 cr.execute('select id,name,value,object,meta, key from ir_values where ' +\
195 ' and '.join(where1+where_opt), where2)
196 for rec in cr.fetchall():
197 if rec[0] in result_ids:
200 result_ids[rec[0]] = True
212 def _result_get(x, keys):
217 model,id = x[2].split(',')
219 fields = self.pool.get(model).fields_get_keys(cr, uid)
221 while pos<len(fields):
222 if fields[pos] in ('report_sxw_content', 'report_rml_content',
223 'report_sxw', 'report_rml', 'report_sxw_content_data',
224 'report_rml_content_data'):
228 datas = self.pool.get(model).read(cr, uid, [id], fields, context)
229 datas= datas and datas[0] or None
231 #ir_del(cr, uid, x[0])
234 datas = pickle.loads(str(x[2]))
236 meta2 = pickle.loads(x[4])
237 return (x[0],x[1],datas,meta2)
238 return (x[0],x[1],datas)
240 res = filter(bool, map(lambda x: _result_get(x, keys), list(result)))
243 if type(r[2])==type({}) and 'type' in r[2]:
244 if r[2]['type'] in ('ir.actions.report.xml','ir.actions.act_window','ir.actions.wizard'):
245 if r[2].has_key('groups_id'):
246 groups = r[2]['groups_id']
248 group_ids = ','.join([ str(x) for x in r[2]['groups_id']])
249 cr.execute("select count(*) from res_groups_users_rel where gid in (%s) and uid='%s'" % (group_ids, uid))
250 gr_ids = cr.fetchall()
251 if not gr_ids[0][0] > 0:
253 if r[1]=='Menuitem' and not res2:
254 raise osv.except_osv('Error !','You do not have the permission to perform this operation !!!')
256 # #raise osv.except_osv('Error !','You have not permission to perform operation !!!')
263 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: