[Merge] Merge with trunk addons
[odoo/odoo.git] / bin / addons / base / ir / ir_values.py
index de6ae7d..97f6f75 100644 (file)
@@ -1,34 +1,26 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
-# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
-# $Id$
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
 #
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
-#
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 #
 ##############################################################################
 
 from osv import osv,fields
+from osv.orm import except_orm
 import pickle
 from tools.translate import _
 
@@ -51,25 +43,49 @@ class ir_values(osv.osv):
         if context is None:
             context = {}
         ctx = context.copy()
-        if 'read_delta' in ctx:
-            del ctx['read_delta']
+        if self.CONCURRENCY_CHECK_FIELD in ctx:
+            del ctx[self.CONCURRENCY_CHECK_FIELD]
         if not self.browse(cursor, user, id, context=context).object:
-            value = pickle.dumps(eval(value))
+            value = pickle.dumps(value)
         self.write(cursor, user, id, {name[:-9]: value}, context=ctx)
 
+    def onchange_object_id(self, cr, uid, ids, object_id, context={}):
+        if not object_id: return {}
+        act = self.pool.get('ir.model').browse(cr, uid, object_id, context=context)
+        return {
+                'value': {'model': act.model}
+        }
+
+    def onchange_action_id(self, cr, uid, ids, action_id, context={}):
+        if not action_id: return {}
+        act = self.pool.get('ir.actions.actions').browse(cr, uid, action_id, context=context)
+        return {
+                'value': {'value_unpickle': act.type+','+str(act.id)}
+        }
+
     _columns = {
         'name': fields.char('Name', size=128),
-        'model': fields.char('Object', size=128),
+        'model_id': fields.many2one('ir.model', 'Object', size=128,
+            help="This field is not used, it only helps you to select a good model."),
+        'model': fields.char('Object Name', size=128),
+        'action_id': fields.many2one('ir.actions.actions', 'Action',
+            help="This field is not used, it only helps you to select the right action."),
         'value': fields.text('Value'),
         'value_unpickle': fields.function(_value_unpickle, fnct_inv=_value_pickle,
             method=True, type='text', string='Value'),
         'object': fields.boolean('Is Object'),
-        'key': fields.char('Type', size=128),
-        'key2': fields.char('Value', size=256),
+        'key': fields.selection([('action','Action'),('default','Default')], 'Type', size=128),
+        'key2' : fields.selection([('client_action_multi', 'client_action_multi'),
+                                   ('client_action_relate', 'client_action_relate'),
+                                   ('tree_but_open', 'tree_but_open'),
+                                   ('tree_but_action', 'tree_but_action'),
+                                   ('client_print_multi', 'client_print_multi')],
+                                  'Event Type',
+                                  help="The kind of action or button in the client side that will trigger the action."),
         'meta': fields.text('Meta Datas'),
         'meta_unpickle': fields.function(_value_unpickle, fnct_inv=_value_pickle,
-            method=True, type='text', string='Meta Datas'),
-        'res_id': fields.integer('Resource ID'),
+            method=True, type='text', string='Metadata'),
+        'res_id': fields.integer('Object ID', help="Keep 0 if the action must appear on all resources."),
         'user_id': fields.many2one('res.users', 'User', ondelete='cascade'),
         'company_id': fields.many2one('res.company', 'Company')
     }
@@ -174,14 +190,20 @@ class ir_values(osv.osv):
 
             result = []
             ok = True
+            result_ids = {}
             while ok:
                 if not where_opt:
-                    cr.execute('select id from ir_values where ' +\
+                    cr.execute('select id,name,value,object,meta, key from ir_values where ' +\
                             ' and '.join(where1)+' and user_id is null', where2)
                 else:
-                    cr.execute('select id from ir_values where ' +\
+                    cr.execute('select id,name,value,object,meta, key from ir_values where ' +\
                             ' and '.join(where1+where_opt), where2)
-                result.extend([x[0] for x in cr.fetchall()])
+                for rec in cr.fetchall():
+                    if rec[0] in result_ids:
+                        continue
+                    if rec[2]:                    
+                        result.append(rec)
+                        result_ids[rec[0]] = True
                 if len(where_opt):
                     where_opt.pop()
                 else:
@@ -192,13 +214,6 @@ class ir_values(osv.osv):
 
         if not result:
             return []
-        cid = self.pool.get('res.users').browse(cr, uid, uid, context={}).company_id.id
-        cr.execute('select id,name,value,object,meta, key ' \
-                'from ir_values ' \
-                'where id in ('+','.join(map(str,result))+') ' \
-                    'and (company_id is null or company_id = %d) '\
-                'ORDER BY user_id', (cid,))
-        result = cr.fetchall()
 
         def _result_get(x, keys):
             if x[1] in keys:
@@ -207,20 +222,25 @@ class ir_values(osv.osv):
             if x[3]:
                 model,id = x[2].split(',')
                 id = int(id)
-                datas = self.pool.get(model).read(cr, uid, id, False, context)
+                fields = self.pool.get(model).fields_get_keys(cr, uid)
+                pos = 0
+                while pos<len(fields):
+                    if fields[pos] in ('report_sxw_content', 'report_rml_content',
+                        'report_sxw', 'report_rml', 'report_sxw_content_data',
+                        'report_rml_content_data'):
+                        del fields[pos]
+                    else:
+                        pos+=1
+                try:
+                    datas = self.pool.get(model).read(cr, uid, [id], fields, context)
+                except except_orm:
+                    return False
+                datas= datas and datas[0] or None
                 if not datas:
                     #ir_del(cr, uid, x[0])
                     return False
-                def clean(x):
-                    for key in ('report_sxw_content', 'report_rml_content',
-                            'report_sxw', 'report_rml', 'report_sxw_content_data',
-                            'report_rml_content_data'):
-                        if key in x:
-                            del x[key]
-                    return x
-                datas = clean(datas)
             else:
-                datas = pickle.loads(x[2])
+                datas = pickle.loads(str(x[2].encode('utf-8')))
             if meta:
                 meta2 = pickle.loads(x[4])
                 return (x[0],x[1],datas,meta2)
@@ -234,8 +254,7 @@ class ir_values(osv.osv):
                     if r[2].has_key('groups_id'):
                         groups = r[2]['groups_id']
                         if len(groups) > 0:
-                            group_ids = ','.join([ str(x) for x in r[2]['groups_id']])
-                            cr.execute("select count(*) from res_groups_users_rel where gid in (%s) and uid='%s'" % (group_ids, uid))
+                            cr.execute("SELECT count(*) FROM res_groups_users_rel WHERE gid = ANY(%s) AND uid=%s",(groups, uid))
                             gr_ids = cr.fetchall()
                             if not gr_ids[0][0] > 0:
                                 res2.remove(r)