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

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Feb 22 10:04:50 CET 2017

# 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 64b5653e35073548187ee78ca9ba81345406affa
# 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,
 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:
         if len(rset) == 1:
             self._cw.view(self.subvid, rset=rset, w=self.w, **kwargs)
+            # 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):

More information about the saem-devel mailing list