[seda tree] Extract reparenting into a generic function
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Dec 2017 16:10:27 +0100
changeset 2906 69ce3f55cd88
parent 2905 b6b20ce5dead
child 2907 45a1b86c2805
[seda tree] Extract reparenting into a generic function and move it to the itree module since this is entity logic, not view.
cubicweb_seda/entities/itree.py
cubicweb_seda/views/sedatree.py
--- a/cubicweb_seda/entities/itree.py	Mon Dec 11 13:22:32 2017 +0100
+++ b/cubicweb_seda/entities/itree.py	Mon Dec 11 16:10:27 2017 +0100
@@ -1,4 +1,4 @@
-# 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@logilab.fr
 #
 # This program is free software: you can redistribute it and/or modify it under
@@ -18,6 +18,7 @@
 from cubicweb.predicates import is_instance
 from cubicweb.view import EntityAdapter
 
+from ..xsd2yams import MULTIPLE_CHILDREN
 from . import simplified_profile
 
 
@@ -146,3 +147,13 @@
 
     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})
--- a/cubicweb_seda/views/sedatree.py	Mon Dec 11 13:22:32 2017 +0100
+++ b/cubicweb_seda/views/sedatree.py	Mon Dec 11 16:10:27 2017 +0100
@@ -20,6 +20,7 @@
 from cubicweb.web import component
 
 from ..entities import simplified_profile, component_unit
+from ..entities.itree import reparent
 from . import jqtree
 
 
@@ -75,9 +76,7 @@
         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):
@@ -107,8 +106,5 @@
     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)