[PATCH 16 of 18 seda] [seda tree] Skeleton for allowing to move a node at a specific position of the tree

Sylvain Thenault sylvain.thenault at logilab.fr
Mon Dec 11 16:53:48 CET 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1513005725 -3600
#      Mon Dec 11 16:22:05 2017 +0100
# Node ID ccba039cc94763b58ef0b270c232f37ae9527432
# Parent  b1a41946879abf93ebf2cd274524063de0690e08
[seda tree] Skeleton for allowing to move a node at a specific position of the tree

by implementing it on the JQTree adapter, but with only hard-coded position in
the js for now.

diff --git a/cubicweb_seda/data/cubes.jqtree.js b/cubicweb_seda/data/cubes.jqtree.js
--- a/cubicweb_seda/data/cubes.jqtree.js
+++ b/cubicweb_seda/data/cubes.jqtree.js
@@ -60,13 +60,12 @@ jqtree = {
         );
         $tree.bind(
             'tree.move',
             function(event) {
                 event.preventDefault();
-                // do the move first, and then POST back.
-                event.move_info.do_move();
-                asyncRemoteExec('jqtree_reparent', event.move_info.moved_node.id,
-                                event.move_info.target_node.id);
+                asyncRemoteExec('jqtree_reparent', move.moved_node.id, target_node.id, 0);
+                // do the move after POSTing.
+                move.do_move();
             }
         );
     }
 };
diff --git a/cubicweb_seda/entities/itree.py b/cubicweb_seda/entities/itree.py
--- a/cubicweb_seda/entities/itree.py
+++ b/cubicweb_seda/entities/itree.py
@@ -227,10 +227,12 @@ def prepare_child_removal(child):
         'SET C ordering CO - 1 WHERE C ordering CO, C ordering > XO, '
         'X ordering XO, X {rtype} P, C {rtype} P, X eid %(x)s'.format(rtype=rtype),
         {'x': child.eid}).rows
 
 
-def reparent(entity, new_parent_eid):
-    """Move `entity` as a children of `new_parent_eid`.
+def reparent(entity, new_parent_eid, index):
+    """Move `entity` as a children of `new_parent_eid` at `index` position.
     """
     rtype = ETYPE_PARENT_RTYPE[entity.cw_etype]
     entity.cw_set(**{rtype: new_parent_eid})
+    move_child_at_index(entity._cw, new_parent_eid, entity.eid, index,
+                        reparenting=True)
diff --git a/cubicweb_seda/views/jqtree.py b/cubicweb_seda/views/jqtree.py
--- a/cubicweb_seda/views/jqtree.py
+++ b/cubicweb_seda/views/jqtree.py
@@ -24,15 +24,15 @@ from cubicweb.view import EntityAdapter,
 from cubicweb.predicates import adaptable, match_form_params
 from cubicweb.web.views import ajaxcontroller, json
 
 
 @ajaxcontroller.ajaxfunc(output_type='json')
-def jqtree_reparent(self, child_eid, parent_eid):
+def jqtree_reparent(self, child_eid, parent_eid, index):
     child = self._cw.entity_from_eid(child_eid)
     adapted = child.cw_adapt_to('IJQTree')
     assert adapted is not None
-    adapted.reparent(parent_eid)
+    adapted.reparent(parent_eid, index)
 
 
 class IJQTreeAdapter(EntityAdapter):
     """Adapt an entity into a JQTree node."""
     __abstract__ = True
@@ -46,12 +46,13 @@ class IJQTreeAdapter(EntityAdapter):
         """Return True if entity may be moved from its location to another
         (i.e. may be reparented).
         """
         return False
 
-    def reparent(self, peid):
-        """Set entity as child of entity with `peid`."""
+    def reparent(self, peid, index):
+        """Set entity as child of entity with `peid`, at the `index` position.
+        """
         raise NotImplementedError
 
     def json(self, on_demand=False):
         """Return a JSON dict of an entity."""
         entity = self.entity
diff --git a/cubicweb_seda/views/sedatree.py b/cubicweb_seda/views/sedatree.py
--- a/cubicweb_seda/views/sedatree.py
+++ b/cubicweb_seda/views/sedatree.py
@@ -72,18 +72,18 @@ class ArchiveUnitIJQTreeAdapter(SEDAIJQT
         return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit']
 
     def maybe_moved(self):
         return True
 
-    def reparent(self, peid):
+    def reparent(self, peid, index):
         parent = self._cw.entity_from_eid(peid)
         if parent.cw_etype == 'SEDAArchiveUnit':
             parent = parent.first_level_choice.content_sequence
         else:
             assert parent.cw_etype == 'SEDAArchiveTransfer', (
                 'cannot re-parent to entity type {0}'.format(parent.cw_etype))
-        reparent(self.entity, parent.eid)
+        reparent(self.entity, parent.eid, index)
 
 
 class SimplifiedArchiveUnitIJQTreeAdapter(ArchiveUnitIJQTreeAdapter):
     __select__ = ArchiveUnitIJQTreeAdapter.__select__ & simplified_profile()
 
@@ -107,10 +107,10 @@ class SimplifiedBinaryDataObjectIJQTreeA
     rtype_to_archivetransfer = 'seda_binary_data_object'
 
     def maybe_moved(self):
         return True
 
-    def reparent(self, peid):
+    def reparent(self, peid, index):
         archunit = self._cw.entity_from_eid(peid)
         parent = archunit.first_level_choice.content_sequence
         child = self.entity.reverse_seda_data_object_reference_id[0]
-        reparent(child, parent.eid)
+        reparent(child, parent.eid, index)
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -535,28 +535,29 @@ class SEDATreeTC(CubicWebTC):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
             unit, unit_alt, unit_alt_seq = testutils.create_archive_unit(transfer)
             unit2 = testutils.create_archive_unit(unit_alt_seq)[0]
             cnx.commit()
-            unit2.cw_adapt_to('IJQTree').reparent(transfer.eid)
+            unit2.cw_adapt_to('IJQTree').reparent(transfer.eid, 0)
             cnx.commit()
             transfer.cw_clear_all_caches()
             self.assertEqual([x.eid for x in transfer.reverse_seda_archive_unit],
-                             [unit.eid, unit2.eid])
+                             [unit2.eid, unit.eid])
 
     def test_archiveunit_reparent_to_archiveunit(self):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
             unit, _, unit_alt_seq = testutils.create_archive_unit(transfer)
             unit2, _, unit2_alt_seq = testutils.create_archive_unit(transfer)
+            subunit, _, _ = testutils.create_archive_unit(unit2_alt_seq)
             cnx.commit()
             unit.cw_clear_all_caches()
-            unit.cw_adapt_to('IJQTree').reparent(unit2.eid)
+            unit.cw_adapt_to('IJQTree').reparent(unit2.eid, 0)
             cnx.commit()
             unit2_alt_seq.cw_clear_all_caches()
             self.assertEqual([x.eid for x in unit2_alt_seq.reverse_seda_archive_unit],
-                             [unit.eid])
+                             [unit.eid, subunit.eid])
 
     def test_binarydataobject_reparent(self):
         with self.admin_access.cnx() as cnx:
             transfer = cnx.entity_from_eid(self.transfer_eid)
             unit, _, unit_alt_seq = testutils.create_archive_unit(transfer)
@@ -565,11 +566,11 @@ class SEDATreeTC(CubicWebTC):
             ref = cnx.create_entity('SEDADataObjectReference',
                                     seda_data_object_reference=unit_alt_seq,
                                     seda_data_object_reference_id=bdo)
             cnx.commit()
             bdo.cw_clear_all_caches()
-            bdo.cw_adapt_to('IJQTree').reparent(unit2.eid)
+            bdo.cw_adapt_to('IJQTree').reparent(unit2.eid, 0)
             cnx.commit()
             unit2_alt_seq.cw_clear_all_caches()
             self.assertEqual([x.eid for x in unit2_alt_seq.reverse_seda_data_object_reference],
                              [ref.eid])
             unit_alt_seq.cw_clear_all_caches()


More information about the saem-devel mailing list