[PATCH 2 of 5 saem_ref v4] Initialize index of sortable related entities on creation

Frank Bessou frank.bessou at logilab.fr
Mon Oct 8 10:36:44 CEST 2018


# HG changeset patch
# User Frank Bessou <frank.bessou at logilab.fr>
# Date 1537868892 -7200
#      Tue Sep 25 11:48:12 2018 +0200
# Node ID b80e430ac1341bf929346979b62e84438d18deed
# Parent  ffe30bd3c9efba3d958ec331d789bc15387bc164
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r b80e430ac134
# EXP-Topic CONSEJIRA-578-584
Initialize index of sortable related entities on creation

We will want to display newly created entities at the end of the list so
we use their eid as index.

We introduce an ISortable adapter which we make selectable for mandates
and functions. This adapater will be used to ease the manipulation of
the sortable things and is used here to select the hook.

diff --git a/cubicweb_saem_ref/entities/__init__.py b/cubicweb_saem_ref/entities/__init__.py
--- a/cubicweb_saem_ref/entities/__init__.py
+++ b/cubicweb_saem_ref/entities/__init__.py
@@ -229,3 +229,16 @@ def naa(self):
     if self.authority and self.authority[0].ark_naa:
         return self.authority[0].ark_naa[0]
     return None
+
+
+class ISortable(EntityAdapter):
+    __regid__ = 'ISortable'
+    __abstract__ = True
+
+
+class AuthorityRecordFunctionSortable(ISortable):
+    __select__ = is_instance('AgentFunction')
+
+
+class AuthorityRecordMandateSortable(ISortable):
+    __select__ = is_instance('Mandate')
diff --git a/cubicweb_saem_ref/hooks.py b/cubicweb_saem_ref/hooks.py
--- a/cubicweb_saem_ref/hooks.py
+++ b/cubicweb_saem_ref/hooks.py
@@ -566,6 +566,19 @@ class AuthorityRecordUsedBySetDefault(ho
             {'u': self.eidto, 'ar': self.eidfrom})
 
 
+# Define index for sorted composite relation ###################################
+
+class InitializeIndexOnCreation(hook.Hook):
+    """An entity which is part of a collection is created"""
+    __regid__ = 'saem.sortable.created'
+    __select__ = hook.Hook.__select__ & adaptable('ISortable')
+    events = ('before_add_entity',)
+
+    def __call__(self):
+        # Ensure we use an index greater than every other
+        self.entity.cw_edited['index'] = self.entity.eid
+
+
 def registration_callback(vreg):
     vreg.register_all(globals().values(), __name__)
 
diff --git a/test/unittest_hooks.py b/test/unittest_hooks.py
--- a/test/unittest_hooks.py
+++ b/test/unittest_hooks.py
@@ -219,6 +219,13 @@ class SAEMRefHooksTC(CubicWebTC):
                               reverse_use_email=cnx.user.eid)
             cnx.commit()
 
+    def test_sortable_index_is_eid_on_creation(self):
+        with self.admin_access.cnx() as cnx:
+            arecord = testutils.authority_record(cnx, u'test')
+            mandate = cnx.create_entity('Mandate', term=u'casting', mandate_agent=arecord)
+            cnx.commit()
+            self.assertEqual(mandate.eid, mandate.index)
+
 
 class ARKGenerationHooksTC(CubicWebTC):
 



More information about the saem-devel mailing list