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

Denis Laxalde denis.laxalde at logilab.fr
Fri Oct 5 11:46:08 CEST 2018


Frank Bessou a écrit :
> # HG changeset patch
> # User Frank Bessou <frank.bessou at logilab.fr>
> # Date 1537868892 -7200
> #      Tue Sep 25 11:48:12 2018 +0200
> # Node ID 81b2e9d0c3a4a2346e0c9e85fd7e67a7d1b6ebab
> # Parent  ffe30bd3c9efba3d958ec331d789bc15387bc164
> # Available At http://hg.logilab.org/review/cubes/saem_ref
> #              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 81b2e9d0c3a4
> # 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,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)

Can you add a cnx.commit() here? It'd help prevent integrity errors
(which is quite important here since hooks are involved).

> +            self.assertEqual(mandate.eid, mandate.index)
> +
> +
>  class ExtractArkTC(unittest.TestCase):
>  
>      configcls = PostgresApptestConfiguration
> 



More information about the saem-devel mailing list