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

Frank Bessou frank.bessou at logilab.fr
Fri Oct 5 16:47:32 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 6440eeb84824c19eaf1aebb377e6523f35290c69
# Parent  ffe30bd3c9efba3d958ec331d789bc15387bc164
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 6440eeb84824
# 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
@@ -371,6 +371,18 @@ class AuthorityRecordHookTC(CubicWebTC):
             self.assertFalse(arecord.reverse_use_authorityrecord)
 
 
+class InitializeSortableIndexHookTC(CubicWebTC):
+
+    configcls = PostgresApptestConfiguration
+
+    def setup_database(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 ExtractArkTC(unittest.TestCase):
 
     configcls = PostgresApptestConfiguration



More information about the saem-devel mailing list