[saem-devel] [PATCH saem V2] [views] Automatically paginate content for subclasses of RelatedEntitiesListView

Sylvain Thénault sylvain.thenault at logilab.fr
Wed Feb 22 15:28:13 CET 2017


CI: https://jenkins.logilab.org/job/cubicweb-saem_ref/95/


Le 22/02/2017 à 15:27, Sylvain Thenault a écrit :
> # HG changeset patch
> # User Sylvain Thénault <sylvain.thenault at logilab.fr>
> # Date 1487753401 -3600
> #      Wed Feb 22 09:50:01 2017 +0100
> # Node ID a944f6a7b0a5d67e2dd2ae87de981e3cf44b084a
> # Parent  6caab7efc2ad7209daca92f2ae36122eeaab9544
> [views] Automatically paginate content for subclasses of RelatedEntitiesListView
>
> On the demo, we have a tab which display more than 3000 related entities, which
> takes age to display. Adding pagination in RelatedEntitiesListView fixes it and
> should do the same for every similar cases based on this same base class (i.e.
> every list displaying related objects).
>
> 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
> @@ -22,11 +22,12 @@ from logilab.common.decorators import mo
>  from logilab.mtconverter import xml_escape
>  from logilab.common.registry import yes
>  
>  from cubicweb import _
>  from cubicweb import NoSelectableObject, neg_role, tags
> -from cubicweb.uilib import domid
> +from cubicweb.utils import json_dumps, js_href
> +from cubicweb.uilib import domid, js
>  from cubicweb.view import EntityView
>  from cubicweb.predicates import (has_permission, is_instance, multi_lines_rset, match_kwargs,
>                                   partial_has_related_entities)
>  from cubicweb.web import component, formwidgets as fw
>  from cubicweb.web.views import basecomponents, uicfg, urlrewrite, tabs, primary
> @@ -334,11 +335,26 @@ class RelatedEntitiesListView(EntityView
>          if self.title:
>              self.w(tags.h2(self._cw._(self.title)))
>          if len(rset) == 1:
>              self._cw.view(self.subvid, rset=rset, w=self.w, **kwargs)
>          else:
> +            # assign divid to self for reuse in page_navigation_url below
> +            self.divid = u'{}{}'.format(domid(self.__regid__), entity.eid)
> +            self.w(u'<div id="{}">'.format(self.divid))
> +            self.do_paginate(rset=rset)
>              self._cw.view('list', rset=rset, w=self.w, subvid=self.subvid, **kwargs)
> +            self.w(u'</div>')
> +
> +    def page_navigation_url(self, navcomp, _path, params):
> +        divid = params['divid'] = self.divid
> +        # we don't want vid from _cw.form which target the parent tab
> +        params['vid'] = self.__regid__
> +        # generate rql instead of using cw_rset.printable_rql since it's usually an ambiguous query
> +        # using ARK identifier
> +        params['rql'] = 'Any X WHERE X eid {}'.format(self.cw_rset.one().eid)
> +        return js_href("$(%s).loadxhtml(AJAX_PREFIX_URL, %s, 'get', 'swap')" % (
> +            json_dumps('#' + divid), js.ajaxFuncArgs('view', params)))
>  
>  
>  class RelationInfo(tuple):
>  
>      def __new__(cls, rtype, role, targets=None, label=None):
> diff --git a/test/test_views.py b/test/test_views.py
> --- a/test/test_views.py
> +++ b/test/test_views.py
> @@ -210,10 +210,31 @@ class FuncViewsTC(CubicWebTC):
>              req.cnx.commit()
>              table_html = record.view('prov.activity-generated')
>              self.assertIn('admin', table_html)
>              self.assertIn('007', table_html)
>  
> +    def test_related_entities_navigation(self):
> +        with self.admin_access.web_request() as req:
> +            ou = testutils.organization_unit(req, u'Archival Dptmt', ['archival'])
> +            testutils.authority_record(req.cnx, u'bob', reverse_use_authorityrecord=ou)
> +            testutils.authority_record(req.cnx, u'alice', reverse_use_authorityrecord=ou)
> +            req.cnx.commit()
> +
> +            vid = 'saem.ou.authorityrecords'
> +            view = req.vreg['views'].select(vid, req, rset=ou.as_rset())
> +            html = view.render()
> +            nav_link = view.page_navigation_url(None, None, {})
> +
> +            expected_divid = '{}{}'.format(vid.replace('.', '_'), ou.eid)
> +            self.assertIn('<div id="{}">'.format(expected_divid), html)
> +            self.assertIn('#' + expected_divid, nav_link)
> +            params = json.loads('{{ {} }}'.format(nav_link.split('{')[1].split('}')[0]))
> +            self.assertEqual(params,
> +                             {'divid': expected_divid,
> +                              'vid': vid,
> +                              'rql': u'Any X WHERE X eid {}'.format(ou.eid)})
> +
>  
>  class SEDANavigationTC(CubicWebTC):
>  
>      def test_breadcrumbs(self):
>          with self.admin_access.cnx() as cnx:
> _______________________________________________
> saem-devel mailing list
> saem-devel at lists.cubicweb.org
> http://lists.cubicweb.org/mailman/listinfo/saem-devel

-- 
Sylvain Thénault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (05.62.17.16.42)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
CubicWeb, the semantic web framework:    http://www.cubicweb.org



More information about the saem-devel mailing list