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

Frank Bessou frank.bessou at logilab.fr
Tue Sep 25 12:03:11 CEST 2018


# HG changeset patch
# User Frank Bessou <frank.bessou at logilab.fr>
# Date 1537868359 -7200
#      Tue Sep 25 11:39:19 2018 +0200
# Node ID 39f6872f131110267899f858e395781416438b1b
# Parent  db566d93c849069cb9961e37cc1786d90f32730a
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 39f6872f1311
# 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
@@ -368,7 +368,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'
@@ -381,7 +388,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)
+            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)
+
+            m1.cw_set(index=2)
+            m2.cw_set(index=3)
+            m3.cw_set(index=1)
+            expected = (m3.eid, m1.eid, m2.eid)
+            actual = [e.eid for e in fake_view.related_rset(arecord).entities()]
+            self.assertSequenceEqual(expected, actual)
+
+
 if __name__ == '__main__':
     unittest.main()



More information about the saem-devel mailing list