[PATCH 17 of 18 seda] [seda tree] Skeleton for supporting DnD move to reorder sibling nodes

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


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1513006107 -3600
#      Mon Dec 11 16:28:27 2017 +0100
# Node ID 14ca365f3b34f6b21c94daafd8c5e89ae5343c1f
# Parent  ccba039cc94763b58ef0b270c232f37ae9527432
[seda tree] Skeleton for supporting DnD move to reorder sibling nodes

by implementing ajax entry point and API on the jqtree adapter.

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
@@ -31,10 +31,18 @@ def jqtree_reparent(self, child_eid, par
     adapted = child.cw_adapt_to('IJQTree')
     assert adapted is not None
     adapted.reparent(parent_eid, index)
 
 
+ at ajaxcontroller.ajaxfunc(output_type='json')
+def jqtree_reorder(self, parent_eid, child_eid, index):
+    parent = self._cw.entity_from_eid(parent_eid)
+    adapted = parent.cw_adapt_to('IJQTree')
+    assert adapted is not None
+    adapted.move_child_at_index(child_eid, index)
+
+
 class IJQTreeAdapter(EntityAdapter):
     """Adapt an entity into a JQTree node."""
     __abstract__ = True
     __regid__ = 'IJQTree'
 
@@ -51,10 +59,14 @@ class IJQTreeAdapter(EntityAdapter):
     def reparent(self, peid, index):
         """Set entity as child of entity with `peid`, at the `index` position.
         """
         raise NotImplementedError
 
+    def move_child_at_index(self, ceid, index):
+        """Move child entity `ceid` at the `index` position."""
+        raise NotImplementedError
+
     def json(self, on_demand=False):
         """Return a JSON dict of an entity."""
         entity = self.entity
         label = entity.view('jqtree.label')
         data = {
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
@@ -18,11 +18,11 @@
 from cubicweb import _
 from cubicweb.predicates import adaptable, is_instance
 from cubicweb.web import component
 
 from ..entities import simplified_profile, component_unit
-from ..entities.itree import reparent
+from ..entities.itree import move_child_at_index, reparent
 from . import jqtree
 
 
 class ProfileTreeComponent(component.EntityCtxComponent):
     """Display the whole profile tree."""
@@ -54,10 +54,13 @@ class ArchiveTransferIJQTreeAdapter(SEDA
 
     def maybe_parent_of(self):
         return ['SEDAArchiveUnit',
                 'SEDAPhysicalDataObject', 'SEDABinaryDataObject']
 
+    def move_child_at_index(self, ceid, index, reparenting=False):
+        move_child_at_index(self._cw, self.entity.eid, ceid, index, reparenting)
+
 
 class SimplifiedArchiveTransferIJQTreeAdapter(ArchiveTransferIJQTreeAdapter):
     __select__ = ArchiveTransferIJQTreeAdapter.__select__ & simplified_profile()
 
     def maybe_parent_of(self):
@@ -81,18 +84,32 @@ class ArchiveUnitIJQTreeAdapter(SEDAIJQT
         else:
             assert parent.cw_etype == 'SEDAArchiveTransfer', (
                 'cannot re-parent to entity type {0}'.format(parent.cw_etype))
         reparent(self.entity, parent.eid, index)
 
+    def move_child_at_index(self, ceid, index, reparenting=False):
+        move_child_at_index(self._cw,
+                            self.entity.first_level_choice.content_sequence.eid,
+                            ceid, index, reparenting)
+
 
 class SimplifiedArchiveUnitIJQTreeAdapter(ArchiveUnitIJQTreeAdapter):
     __select__ = ArchiveUnitIJQTreeAdapter.__select__ & simplified_profile()
 
     def maybe_parent_of(self):
         return [] if self.entity.is_archive_unit_ref else ['SEDAArchiveUnit',
                                                            'SEDABinaryDataObject']
 
+    def move_child_at_index(self, ceid, index, reparenting=False):
+        # when reordering a data object, we actually have to maintain ordering
+        # of its reference
+        child = self._cw.entity_from_eid(ceid)
+        if child.cw_etype == 'SEDABinaryDataObject':
+            child = child.reverse_seda_data_object_reference_id[0]
+        move_child_at_index(self._cw,
+                            self.entity.first_level_choice.content_sequence.eid,
+                            child.eid, index, reparenting)
 
 
 class DataObjectIJQTreeAdapter(SEDAIJQTreeAdapter):
     __select__ = (SEDAIJQTreeAdapter.__select__
                   & is_instance('SEDABinaryDataObject', 'SEDAPhysicalDataObject'))
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -574,10 +574,48 @@ class SEDATreeTC(CubicWebTC):
             self.assertEqual([x.eid for x in unit2_alt_seq.reverse_seda_data_object_reference],
                              [ref.eid])
             unit_alt_seq.cw_clear_all_caches()
             self.assertFalse(unit_alt_seq.reverse_seda_data_object_reference)
 
+    def test_binarydataobject_reorder_full(self):
+        with self.admin_access.cnx() as cnx:
+            transfer = cnx.entity_from_eid(self.transfer_eid)
+            transfer.cw_set(simplified_profile=False)
+            bdo1 = testutils.create_data_object(transfer)
+            bdo2 = testutils.create_data_object(transfer)
+            cnx.commit()
+
+            transfer.cw_adapt_to('IJQTree').move_child_at_index(bdo2.eid, 0)
+
+            transfer.cw_clear_all_caches()
+            self.assertEqual([x.eid for x in transfer.reverse_seda_binary_data_object],
+                             [bdo2.eid, bdo1.eid])
+
+            bdo2.cw_clear_all_caches()
+            transfer.cw_adapt_to('IJQTree').move_child_at_index(bdo2.eid, 2)
+
+            bdo1.cw_clear_all_caches()
+            bdo2.cw_clear_all_caches()
+
+            transfer.cw_clear_all_caches()
+            self.assertEqual([x.eid for x in transfer.cw_adapt_to('ITreeBase').iterchildren()],
+                             [bdo1.eid, bdo2.eid])
+
+    def test_binarydataobject_reorder_simplified(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)
+            bdo1 = testutils.create_data_object(unit_alt_seq)
+            bdo2 = testutils.create_data_object(unit_alt_seq)
+            cnx.commit()
+
+            unit.cw_adapt_to('IJQTree').move_child_at_index(bdo2.eid, 0)
+
+            transfer.cw_clear_all_caches()
+            self.assertEqual([x.eid for x in unit.cw_adapt_to('ITreeBase').iterchildren()],
+                             [bdo2.eid, bdo1.eid])
+
 
 class FakeForm(object):
     def __init__(self, req, edited_entity):
         self._cw = req
         self.edited_entity = edited_entity


More information about the saem-devel mailing list