From 0f5424eac4a337a3a1a3e1e5876fa26a3fffb1ed Mon Sep 17 00:00:00 2001
From: Xavier Morel <xmo@openerp.com>
Date: Tue, 27 May 2014 12:21:52 +0200
Subject: [PATCH] [IMP] replace check_mode python-level constraint by an SQL
 CHECK constraint

---
 openerp/addons/base/ir/ir_ui_view.py    | 14 ++++++--------
 openerp/addons/base/tests/test_views.py |  9 +++++++--
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/openerp/addons/base/ir/ir_ui_view.py b/openerp/addons/base/ir/ir_ui_view.py
index 9808adf50acc..2e74f116223a 100644
--- a/openerp/addons/base/ir/ir_ui_view.py
+++ b/openerp/addons/base/ir/ir_ui_view.py
@@ -206,16 +206,14 @@ class view(osv.osv):
                         return False
         return True
 
-    def _check_mode(self, cr, uid, ids, context=None):
-        for v in self.read(cr, uid, ids, ['inherit_id', 'mode'], context=context):
-            if v['mode'] == 'extension' and not v['inherit_id']:
-                raise Exception(
-                    _("A view extending nothing can not be an extension view"))
-        return True
-
+    _sql_constraints = [
+        ('inheritance_mode',
+         "CHECK (mode != 'extension' OR inherit_id IS NOT NULL)",
+         "Invalid inheritance mode: if the mode is 'extension', the view must"
+         " extend an other view"),
+    ]
     _constraints = [
         (_check_xml, 'Invalid view definition', ['arch']),
-        (_check_mode, "Invalid mode for inheritance", ['mode', 'inherit_id']),
     ]
 
     def _auto_init(self, cr, context=None):
diff --git a/openerp/addons/base/tests/test_views.py b/openerp/addons/base/tests/test_views.py
index 83094c79a8b7..180bb6bff8a0 100644
--- a/openerp/addons/base/tests/test_views.py
+++ b/openerp/addons/base/tests/test_views.py
@@ -7,7 +7,10 @@ import unittest2
 from lxml import etree as ET
 from lxml.builder import E
 
+from psycopg2 import IntegrityError
+
 from openerp.tests import common
+import openerp.tools
 
 Field = E.field
 
@@ -829,6 +832,7 @@ class ViewModeField(ViewCase):
         }))
         self.assertEqual(view2.mode, 'extension')
 
+    @openerp.tools.mute_logger('openerp.sql_db')
     def testModeExplicit(self):
         view = self.browse(self.create({
             'inherit_id': None,
@@ -841,13 +845,14 @@ class ViewModeField(ViewCase):
         }))
         self.assertEqual(view.mode, 'primary')
 
-        with self.assertRaises(Exception):
+        with self.assertRaises(IntegrityError):
             self.create({
                 'inherit_id': None,
                 'mode': 'extension',
                 'arch': '<qweb/>'
             })
 
+    @openerp.tools.mute_logger('openerp.sql_db')
     def testPurePrimaryToExtension(self):
         """
         A primary view with inherit_id=None can't be converted to extension
@@ -856,7 +861,7 @@ class ViewModeField(ViewCase):
             'inherit_id': None,
             'arch': '<qweb/>'
         }))
-        with self.assertRaises(Exception):
+        with self.assertRaises(IntegrityError):
             view_pure_primary.write({'mode': 'extension'})
 
     def testInheritPrimaryToExtension(self):
-- 
GitLab