[FIX] disallow the deletion of records set as default properties
authorChristophe Simonis <christophe@tinyerp.com>
Mon, 10 Aug 2009 16:04:02 +0000 (18:04 +0200)
committerChristophe Simonis <christophe@tinyerp.com>
Mon, 10 Aug 2009 16:04:02 +0000 (18:04 +0200)
[FIX] properties reset to default one correctly

lp bug: https://launchpad.net/bugs/396955 fixed

bzr revid: christophe@tinyerp.com-20090810160402-7vamjvletgevmjuc

bin/osv/fields.py
bin/osv/orm.py

index 4d1417b..51b5dd1 100644 (file)
@@ -827,15 +827,18 @@ class property(function):
                     int(prop.value.split(',')[1])) or False
 
         obj = obj.pool.get(self._obj)
-        existing_ids = obj.search(cr, uid, [('id','in',res.values())])
-        deleted_ids = []
-
-        for res_id in res.values():
-            if res_id and (res_id not in existing_ids):
-                if res_id not in deleted_ids:
-                    cr.execute('DELETE FROM ir_property WHERE value=%s', ((obj._name+','+str(res_id)),))
-                    deleted_ids.append(res_id)
-        names = dict(obj.name_get(cr, uid, filter(None, res.values()), context))
+
+        to_check = res.values()
+        if default_val and default_val not in to_check:
+            to_check += [default_val]
+        existing_ids = obj.search(cr, uid, [('id', 'in', to_check)])
+        
+        for id, res_id in res.items():
+            if res_id not in existing_ids:
+                cr.execute('DELETE FROM ir_property WHERE value=%s', ((obj._name+','+str(res_id)),))
+                res[id] = default_val
+
+        names = dict(obj.name_get(cr, uid, existing_ids, context))
         for r in res.keys():
             if res[r] and res[r] in names:
                 res[r] = (res[r], names[res[r]])
index ef0372d..4f16874 100644 (file)
@@ -2262,9 +2262,16 @@ class orm(orm_template):
 
         self.pool.get('ir.model.access').check(cr, uid, self._name, 'unlink')
 
+        properties = self.pool.get('ir.property')
+        domain = [('res_id', '=', False), 
+                  ('value', 'in', ['%s,%s' % (self._name, i) for i in ids]), 
+                 ]
+        if properties.search(cr, uid, domain, context=context):
+            raise except_orm(_('Error'), _('Unable to delete this document because it is used as a default property'))
+
         wf_service = netsvc.LocalService("workflow")
-        for id in ids:
-            wf_service.trg_delete(uid, self._name, id, cr)
+        for oid in ids:
+            wf_service.trg_delete(uid, self._name, oid, cr)
 
         #cr.execute('select * from '+self._table+' where id in ('+str_d+')', ids)
         #res = cr.dictfetchall()
@@ -2282,7 +2289,7 @@ class orm(orm_template):
             if d1:
                 cr.execute('SELECT id FROM "'+self._table+'" ' \
                         'WHERE id IN ('+str_d+')'+d1, sub_ids+d2)
-                if not cr.rowcount == len({}.fromkeys(ids)):
+                if not cr.rowcount == len(sub_ids):
                     raise except_orm(_('AccessError'),
                             _('You try to bypass an access rule (Document type: %s).') % \
                                     self._description)
@@ -2294,13 +2301,13 @@ class orm(orm_template):
                 cr.execute('delete from "'+self._table+'" ' \
                         'where id in ('+str_d+')', sub_ids)
 
-        for order, object, ids, fields in result_store:
+        for order, object, store_ids, fields in result_store:
             if object<>self._name:
                 obj =  self.pool.get(object)
-                cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, ids))+')')
-                ids = map(lambda x: x[0], cr.fetchall())
-                if ids:
-                    obj._store_set_values(cr, uid, ids, fields, context)
+                cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')')
+                rids = map(lambda x: x[0], cr.fetchall())
+                if rids:
+                    obj._store_set_values(cr, uid, rids, fields, context)
         return True
 
     #