[PATCH saem_ref] Clear broader_concept relation before setting when reparenting a Concept

Denis Laxalde denis.laxalde at logilab.fr
Tue Oct 9 13:04:22 CEST 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1539079429 -7200
#      Tue Oct 09 12:03:49 2018 +0200
# Node ID 7c077803c5fbc8f571912d6e34417d1d61127184
# Parent  da2184d04d0f08fdcbd440f85fe5b8fb7984a29b
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 7c077803c5fb
Clear broader_concept relation before setting when reparenting a Concept

Before this change, calling JQTree adapter's reparent() method on a
concept under another concept would add another broader_concept concept
relation instead of replacing the existing one. This is because
.cw_set() *does not* replace relations but adds them. So we must always
clear the broader_concept relation (calling
.cw_set(broader_concept=None)) and reset it (in case the target parent
is another concept).

Adding non-regression tests. Follow-up on f37b9abae3a9.

diff --git a/cubicweb_saem_ref/views/skos.py b/cubicweb_saem_ref/views/skos.py
--- a/cubicweb_saem_ref/views/skos.py
+++ b/cubicweb_saem_ref/views/skos.py
@@ -153,10 +153,10 @@ class ConceptJQTreeAdapter(jqtree.IJQTre
 
     def reparent(self, peid, index):
         parent = self._cw.entity_from_eid(peid)
+        self.entity.cw_set(broader_concept=None)
         if parent == self.entity.in_scheme[0]:
-            self.entity.cw_set(broader_concept=None)
             return
-        self.entity.cw_set(broader_concept=[parent])
+        self.entity.cw_set(broader_concept=parent)
 
 
 class ConceptSchemeITreeBaseAdapter(EntityAdapter):
diff --git a/test/unittest_entities_skos.py b/test/unittest_entities_skos.py
--- a/test/unittest_entities_skos.py
+++ b/test/unittest_entities_skos.py
@@ -84,6 +84,24 @@ class ConceptSchemeITreeBaseAdapterTC(Cu
             scheme.clear_all_caches()
             self.assertFalse(scheme.cw_adapt_to('ITreeBase').is_leaf())
 
+    def test_jqtree_reparent(self):
+        with self.admin_access.cnx() as cnx:
+            scheme = cnx.create_entity('ConceptScheme', ark_naa=testutils.naa(cnx))
+            cnx.commit()
+            a = scheme.add_concept(u'a')
+            b = scheme.add_concept(u'b')
+            c = scheme.add_concept(u'c', broader_concept=b)
+            cnx.commit()
+            c_tree = c.cw_adapt_to('IJQTree')
+            c_tree.reparent(a.eid, None)
+            cnx.commit()
+            c.cw_clear_all_caches()
+            self.assertEqual(c.broader_concept, (a, ))
+            c_tree.reparent(scheme.eid, None)
+            cnx.commit()
+            c.cw_clear_all_caches()
+            self.assertEqual(c.broader_concept, ())
+
 
 if __name__ == '__main__':
     import unittest



More information about the saem-devel mailing list