[PATCH 11 of 18 seda] [seda tree] Extract reparenting into a generic function

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


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1513005027 -3600
#      Mon Dec 11 16:10:27 2017 +0100
# Node ID ed3c64b26c7efd3de3ebea9ba0bc8e8bb271b007
# Parent  5df3817c05103349712009ab02436427a834e031
[seda tree] Extract reparenting into a generic function

and move it to the itree module since this is entity logic, not view.

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
@@ -1,6 +1,6 @@
-# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr -- mailto:contact at logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
 # the terms of the GNU Lesser General Public License as published by the Free
 # Software Foundation, either version 2.1 of the License, or (at your option)
@@ -16,10 +16,11 @@
 """cubicweb-seda ITreeBase adapters"""
 
 from cubicweb.predicates import is_instance
 from cubicweb.view import EntityAdapter
 
+from ..xsd2yams import MULTIPLE_CHILDREN
 from . import simplified_profile
 
 
 def parent_archive_unit(entity):
     """Return the first encountered parent which is an ArchiveUnit"""
@@ -144,5 +145,15 @@ class ITreeBaseArchiveTransferAdapter(IC
                     ('seda_physical_data_object', 'object'),
                     ('seda_archive_unit', 'object')]
 
     def parent(self):
         return None
+
+
+ETYPE_PARENT_RTYPE = dict(MULTIPLE_CHILDREN)
+
+
+def reparent(entity, new_parent_eid):
+    """Move `entity` as a children of `new_parent_eid`.
+    """
+    rtype = ETYPE_PARENT_RTYPE[entity.cw_etype]
+    entity.cw_set(**{rtype: new_parent_eid})
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,10 +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 . import jqtree
 
 
 class ProfileTreeComponent(component.EntityCtxComponent):
     """Display the whole profile tree."""
@@ -73,13 +74,11 @@ class ArchiveUnitIJQTreeAdapter(jqtree.I
         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))
-        rset = self._cw.execute(
-            'SET X seda_archive_unit P WHERE X eid %(x)s, P eid %(p)s',
-            {'x': self.entity.eid, 'p': parent.eid})
+        reparent(self.entity, parent.eid)
 
 
 class SimplifiedArchiveUnitIJQTreeAdapter(ArchiveUnitIJQTreeAdapter):
     __select__ = ArchiveUnitIJQTreeAdapter.__select__ & simplified_profile()
 
@@ -105,10 +104,7 @@ class SimplifiedBinaryDataObjectIJQTreeA
         return True
 
     def reparent(self, peid):
         archunit = self._cw.entity_from_eid(peid)
         parent = archunit.first_level_choice.content_sequence
-        rset = self._cw.execute(
-            'SET REF seda_data_object_reference SEQ WHERE'
-            ' REF seda_data_object_reference_id X,'
-            ' X eid %(x)s, SEQ eid %(seq)s',
-            {'x': self.entity.eid, 'seq': parent.eid})
+        child = self.entity.reverse_seda_data_object_reference_id[0]
+        reparent(child, parent.eid)


More information about the saem-devel mailing list