* extract cycle checking into its own method, and fix it
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 30 Jul 2009 20:01:00 +0200
changeset 49 6698d7177dd0
parent 48 5a3d2c1f6137
child 50 6a76822089d7
* extract cycle checking into its own method, and fix it * use entity_from_eid where appropriate
hooks.py
--- a/hooks.py	Thu Jul 30 19:10:42 2009 +0200
+++ b/hooks.py	Thu Jul 30 20:01:00 2009 +0200
@@ -41,28 +41,29 @@
     def precommit_event(self):
         session = self.session
         # test for indirect cycles
-        toeid = self.toeid
-        fromeid = self.fromeid
-        parents = [toeid]
-        parent = session.entity(toeid)
-        while parent.subkeyword_of:
-            parent = parent.subkeyword_of[0]
-            if parent.eid in parents:
-                raise ValidationError(None,
-                                      session._('detected subkeyword cycle for keyword %(kw)s') %
-                                      {'kw' : fromeid})
-        subkw = session.eid_rset(fromeid).get_entity(0, 0)
+        self.check_cycle()
+        subkw = session.entity_from_eid(self.fromeid)
         if subkw.included_in:
-            kw = session.eid_rset(toeid).get_entity(0, 0)
+            kw = session.entity_from_eid(self.toeid)
             if subkw.included_in[0].eid != kw.included_in[0].eid:
                 msgid = "keywords %(subkw)s and %(kw)s belong to different classifications"
-                raise ValidationError(subkw.eid,  {'subkeyword_of': session._(msgid) %
-                                                   {'subkw' : subkw.eid, 'kw' : kw.eid}})
+                raise ValidationError(subkw.eid, {'subkeyword_of': session._(msgid) %
+                                                  {'subkw' : subkw.eid, 'kw' : kw.eid}})
         else:
             session.execute('SET SK included_in C WHERE SK eid %(x)s, '
                             'SK subkeyword_of K, K included_in C',
                             {'x': subkw.eid}, 'x')
 
+    def check_cycle(self):
+        parents = set([self.toeid])
+        parent = self.session.entity_from_eid(self.toeid)
+        while parent.subkeyword_of:
+            parent = parent.subkeyword_of[0]
+            if parent.eid in parents:
+                msg = self.session._('detected subkeyword cycle')
+                raise ValidationError(self.fromeid, {'subkeyword_of': msg})
+            parents.append(add.eid)
+
 
 class SetIncludedInRelationHook(Hook):
     """sets the included_in relation on a subkeyword if not already set