[PATCH 3 of 5 saem_ref v2] Display sortable entities in a sorted view

Denis Laxalde denis.laxalde at logilab.fr
Fri Oct 5 11:47:20 CEST 2018


Frank Bessou a écrit :
> # HG changeset patch
> # User Frank Bessou <frank.bessou at logilab.fr>
> # Date 1537868359 -7200
> #      Tue Sep 25 11:39:19 2018 +0200
> # Node ID b59c34f39b95a7c1a84889d9c2b4ab38821546a9
> # Parent  81b2e9d0c3a4a2346e0c9e85fd7e67a7d1b6ebab
> # Available At http://hg.logilab.org/review/cubes/saem_ref
> #              hg pull http://hg.logilab.org/review/cubes/saem_ref -r b59c34f39b95
> # EXP-Topic CONSEJIRA-578-584
> Display sortable entities in a sorted view
> 
> We introduce a related_rset method in RelatedEntitiesListView this
> allows to apply post-processes on the rset its subclasses.
> 
> The SortableListViewMixin provide an implementation for the related_rset
> method. It sorts the rset according to the 'index' relation.
> This mixin is used to display functions and mandates on authority
> records.
> 
> diff --git a/cubicweb_saem_ref/views/__init__.py b/cubicweb_saem_ref/views/__init__.py
> --- a/cubicweb_saem_ref/views/__init__.py
> +++ b/cubicweb_saem_ref/views/__init__.py
> @@ -315,6 +315,14 @@ class RelatedEntitiesListView(EntityView
>      target_etype = None
>      subvid_kwargs = None
>  
> +    def related_rset(self, entity):
> +        if self.target_etype is not None:
> +            targettypes = (self.target_etype, )
> +        else:
> +            targettypes = None
> +        return entity.related(self.rtype, role=self.role,
> +                              targettypes=targettypes)
> +
>      @property
>      def title(self):
>          return self.rtype + '_object' if self.role == 'object' else self.rtype
> @@ -322,14 +330,9 @@ class RelatedEntitiesListView(EntityView
>      def entity_call(self, entity, **kwargs):
>          kwargs.update(self.subvid_kwargs or {})
>          kwargs['__redirectpath'] = entity.rest_path()
> -        if self.target_etype is not None:
> -            targettypes = (self.target_etype, )
> -        else:
> -            targettypes = None
> -        rset = entity.related(self.rtype, role=self.role,
> -                              targettypes=targettypes)
>          if self.title:
>              self.w(tags.h2(self._cw._(self.title)))
> +        rset = self.related_rset(entity)
>          if len(rset) == 1:
>              self._cw.view(self.subvid, rset=rset, w=self.w, **kwargs)
>          else:
> diff --git a/cubicweb_saem_ref/views/authorityrecord.py b/cubicweb_saem_ref/views/authorityrecord.py
> --- a/cubicweb_saem_ref/views/authorityrecord.py
> +++ b/cubicweb_saem_ref/views/authorityrecord.py
> @@ -377,7 +377,14 @@ class AuthorityRecordPlaceAsConceptView(
>  pvs.tag_object_of(('*', 'function_agent', 'AuthorityRecord'), 'hidden')
>  
>  
> -class AuthorityRecordFunctionView(RelatedEntitiesListView):
> +class SortableListViewMixin(object):
> +
> +    def related_rset(self, entity):
> +        rset = super(SortableListViewMixin, self).related_rset(entity)
> +        return rset.sorted_rset(lambda entity: entity.index)
> +
> +
> +class AuthorityRecordFunctionView(SortableListViewMixin, RelatedEntitiesListView):
>      """View for AgentFunction, to be display in the context of an AuthorityRecord"""
>      __regid__ = 'saem.authorityrecord.functions'
>      rtype = 'function_agent'
> @@ -390,7 +397,7 @@ class AuthorityRecordFunctionView(Relate
>  pvs.tag_object_of(('*', 'mandate_agent', 'AuthorityRecord'), 'hidden')
>  
>  
> -class AuthorityRecordMandateView(RelatedEntitiesListView):
> +class AuthorityRecordMandateView(SortableListViewMixin, RelatedEntitiesListView):
>      """View for Mandate, to be displayed in the context of an AuthorityRecord"""
>      __regid__ = 'saem.authorityrecord.mandate'
>      rtype = 'mandate_agent'
> diff --git a/test/test_views.py b/test/test_views.py
> --- a/test/test_views.py
> +++ b/test/test_views.py
> @@ -35,6 +35,7 @@ from cubicweb.devtools.testlib import Cu
>  from cubicweb_skos.rdfio import default_graph
>  
>  from cubicweb_saem_ref.views.widgets import process_incomplete_date
> +from cubicweb_saem_ref.views.authorityrecord import SortableListViewMixin
>  
>  import testutils
>  
> @@ -667,5 +668,38 @@ class BaseViewsTC(CubicWebTC):
>              self.assertEqual(req.status_out, 404)
>  
>  
> +class SortableListViewMixinTC(CubicWebTC):
> +
> +    configcls = PostgresApptestConfiguration
> +
> +    class FakeListView(object):
> +
> +        @staticmethod
> +        def related_rset(entity):
> +            return entity.related('mandate_agent', role='object')
> +
> +    class SortableFakeListView(SortableListViewMixin, FakeListView):
> +        pass
> +
> +    def test_related_rset_sorted_by_index(self):
> +        with self.admin_access.cnx() as cnx:
> +            arecord = testutils.authority_record(cnx, u'record')
> +
> +            m1 = cnx.create_entity('Mandate', term=u'1', mandate_agent=arecord)
> +            m2 = cnx.create_entity('Mandate', term=u'2', mandate_agent=arecord)
> +            m3 = cnx.create_entity('Mandate', term=u'3', mandate_agent=arecord)

cnx.commit()

> +            fake_view = self.SortableFakeListView()
> +            expected = (m1.eid, m2.eid, m3.eid)
> +            actual = [e.eid for e in fake_view.related_rset(arecord).entities()]
> +            self.assertSequenceEqual(expected, actual)

Can't you use assertEqual? Arguments need to be of the same type I guess.

> +
> +            m1.cw_set(index=2)
> +            m2.cw_set(index=3)
> +            m3.cw_set(index=1)

cnx.commit()

> +            expected = (m3.eid, m1.eid, m2.eid)
> +            actual = [e.eid for e in fake_view.related_rset(arecord).entities()]
> +            self.assertSequenceEqual(expected, actual)

Same here.

> +
>  if __name__ == '__main__':
>      unittest.main()
> 



More information about the saem-devel mailing list