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

Frank Bessou frank.bessou at logilab.fr
Tue Sep 25 12:03:10 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 db566d93c849069cb9961e37cc1786d90f32730a
# Parent  975f4a115ea61e901ba4d88bb264cb6f833ab0e8
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r db566d93c849
# 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 = ('after_add_entity',)
+
+    def __call__(self):
+        # Ensure we use an index greater than every other
+        self.entity.cw_set(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,22 @@ 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:
+            self.create_user(cnx, u'alice')
+            cnx.commit()
+
+    def test_sortable_index_is_eid_on_creation(self):
+        with self.new_access(u'alice').cnx() as cnx:
+            arecord = testutils.authority_record(cnx, u'test')
+            mandate = cnx.create_entity('Mandate', term=u'casting', mandate_agent=arecord)
+            self.assertEqual(mandate.eid, mandate.index)
+
+
 class ExtractArkTC(unittest.TestCase):
 
     configcls = PostgresApptestConfiguration



More information about the saem-devel mailing list