[PATCH 12 of 18 seda] [itree] Move calculation of the next `ordering` value to itree

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


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1512997396 -3600
#      Mon Dec 11 14:03:16 2017 +0100
# Node ID f8d111c4af68113adbf46d6ed6250382a5b570ab
# Parent  ed3c64b26c7efd3de3ebea9ba0bc8e8bb271b007
[itree] Move calculation of the next `ordering` value to itree

so all implementation details of this ordering management is at the same place.

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
@@ -150,10 +150,19 @@ class ITreeBaseArchiveTransferAdapter(IC
 
 
 ETYPE_PARENT_RTYPE = dict(MULTIPLE_CHILDREN)
 
 
+def next_child_ordering(cnx, parent_eid, rtype):
+    """Return value for the `ordering` attribute of a child freshly appended through
+    `rtype` to parent entity with the given eid.
+    """
+    rql = 'Any MAX(O) WHERE X {rtype} P, P eid %(p)s, X ordering O'
+    ordering = cnx.execute(rql.format(rtype=rtype), {'p': parent_eid})[0][0]
+    return 1 if ordering is None else ordering + 1
+
+
 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/hooks.py b/cubicweb_seda/hooks.py
--- a/cubicweb_seda/hooks.py
+++ b/cubicweb_seda/hooks.py
@@ -25,10 +25,11 @@ from cubicweb import _
 from cubicweb.predicates import is_instance, score_entity
 from cubicweb.server import hook
 
 from .xsd2yams import MULTIPLE_CHILDREN
 from .entities import rule_type_from_etype, diag
+from .entities.itree import next_child_ordering
 from .entities.generated import (CHOICE_RTYPE,
                                  CHECK_CARD_ETYPES, CHECK_CHILDREN_CARD_RTYPES)
 
 
 SEDA_PARENT_RTYPES = {}
@@ -516,14 +517,11 @@ class SetOrderingHook(hook.Hook):
 
     events = ('after_add_relation',)
 
     def __call__(self):
         eid, parent_eid = self.eidfrom, self.eidto
-        rql = 'Any MAX(O) WHERE X {rtype} P, P eid %(p)s, X ordering O'
-        ordering = self._cw.execute(rql.format(rtype=self.rtype),
-                                    {'p': parent_eid})[0][0]
-        ordering = 0 if ordering is None else ordering
+        ordering = next_child_ordering(self._cw, parent_eid, self.rtype)
         entity = self._cw.entity_from_eid(eid)
         entity.cw_set(ordering=ordering + 1)
 
 
 def registration_callback(vreg):


More information about the saem-devel mailing list