[hooks/integrity] kill CheckUniqueHook
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 13 Apr 2015 14:34:11 +0200
changeset 11366 80dec361a5d0
parent 11365 eedf0684533c
child 11367 5379c5dbc2c5
[hooks/integrity] kill CheckUniqueHook It's the backend's job. The noticeable consequence (as can be seen in tests updated here) is that error message are a bit less user-friendly.
cubicweb/hooks/integrity.py
cubicweb/hooks/test/unittest_hooks.py
cubicweb/web/test/unittest_application.py
cubicweb/web/test/unittest_views_basecontrollers.py
--- a/cubicweb/hooks/integrity.py	Mon Jun 20 15:10:24 2016 +0200
+++ b/cubicweb/hooks/integrity.py	Mon Apr 13 14:34:11 2015 +0200
@@ -244,25 +244,6 @@
                         (self.entity.eid, attr, None, constraints))
 
 
-class CheckUniqueHook(IntegrityHook):
-    __regid__ = 'checkunique'
-    events = ('before_add_entity', 'before_update_entity')
-
-    def __call__(self):
-        entity = self.entity
-        eschema = entity.e_schema
-        for attr, val in entity.cw_edited.items():
-            if eschema.subjrels[attr].final and eschema.has_unique_values(attr):
-                if val is None:
-                    continue
-                rql = '%s X WHERE X %s %%(val)s' % (entity.e_schema, attr)
-                rset = self._cw.execute(rql, {'val': val})
-                if rset and rset[0][0] != entity.eid:
-                    msg = _('the value "%s" is already used, use another one')
-                    raise validation_error(entity, {(attr, 'subject'): msg},
-                                           (val,))
-
-
 class DontRemoveOwnersGroupHook(IntegrityHook):
     """delete the composed of a composite relation when this relation is deleted
     """
--- a/cubicweb/hooks/test/unittest_hooks.py	Mon Jun 20 15:10:24 2016 +0200
+++ b/cubicweb/hooks/test/unittest_hooks.py	Mon Apr 13 14:34:11 2015 +0200
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -27,7 +27,8 @@
 from six import text_type
 
 from pytz import utc
-from cubicweb import ValidationError, AuthenticationError, BadConnectionId
+
+from cubicweb import ValidationError
 from cubicweb.devtools.testlib import CubicWebTC
 
 
@@ -213,7 +214,9 @@
             ex = cm.exception
             ex.translate(text_type)
             self.assertIsInstance(ex.entity, int)
-            self.assertEqual(ex.errors, {'login-subject': 'the value "admin" is already used, use another one'})
+            self.assertEqual(ex.errors,
+                             {'': u'some relations violate a unicity constraint',
+                              'login': u'login is part of violated unicity constraint'})
 
 
 if __name__ == '__main__':
--- a/cubicweb/web/test/unittest_application.py	Mon Jun 20 15:10:24 2016 +0200
+++ b/cubicweb/web/test/unittest_application.py	Mon Apr 13 14:34:11 2015 +0200
@@ -241,8 +241,11 @@
             self.assertIsInstance(forminfo['eidmap']['X'], int)
             self.assertIsInstance(forminfo['eidmap']['Y'], int)
             self.assertEqual(forminfo['error'].entity, forminfo['eidmap']['X'])
-            self.assertEqual(forminfo['error'].errors,
-                             {'login-subject': u'the value "admin" is already used, use another one'})
+            expected_errors = {
+                '': u'some relations violate a unicity constraint',
+                'login': u'login is part of violated unicity constraint',
+            }
+            self.assertEqual(forminfo['error'].errors, expected_errors)
             self.assertEqual(forminfo['values'], req.form)
 
     def _edit_parent(self, dir_eid, parent_eid, role='subject',
--- a/cubicweb/web/test/unittest_views_basecontrollers.py	Mon Jun 20 15:10:24 2016 +0200
+++ b/cubicweb/web/test/unittest_views_basecontrollers.py	Mon Apr 13 14:34:11 2015 +0200
@@ -96,9 +96,12 @@
                     }
             with self.assertRaises(ValidationError) as cm:
                 self.ctrl_publish(req)
-                cm.exception.translate(text_type)
-                self.assertEqual({'login-subject': 'the value "admin" is already used, use another one'},
-                                 cm.exception.errors)
+            cm.exception.translate(text_type)
+            expected = {
+                '': u'some relations violate a unicity constraint',
+                'login': u'login is part of violated unicity constraint',
+            }
+            self.assertEqual(cm.exception.errors, expected)
 
     def test_simultaneous_edition_only_one_commit(self):
         """ Allow two simultaneous edit view of the same entity as long as only one commits