[PATCH saem] [ui/patches] Move most pagination related monkey-patches to views

Sylvain Thenault sylvain.thenault at logilab.fr
Thu May 18 17:33:26 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1495121557 -7200
#      Thu May 18 17:32:37 2017 +0200
# Node ID 0d138d03ca4dd1e8d822e280f3c343a4971eac4a
# Parent  7aca90bfbdf3aae98f434400e1368428443441bf
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 0d138d03ca4d
[ui/patches] Move most pagination related monkey-patches to views

Those are patching appobject, hence should not live in site_cubicweb since this
module is loaded once at startup time while appobjects are loaded dynamically
afterwards.

In our case, our monkey-patch was later overwritten by some others from the
squareui cube, leading to weird behaviour of the pagination.

Closes extranet #21942222

diff --git a/cubicweb_saem_ref/site_cubicweb.py b/cubicweb_saem_ref/site_cubicweb.py
--- a/cubicweb_saem_ref/site_cubicweb.py
+++ b/cubicweb_saem_ref/site_cubicweb.py
@@ -218,99 +218,24 @@ def is_hook_category_activated(self, cat
         return category in self._hooks_categories
     return category not in self._hooks_categories
 
 
 # avoid disappearance of navtop components (https://www.cubicweb.org/17074195)
-
-from logilab.mtconverter import xml_escape  # noqa
-from cubicweb.uilib import js  # noqa
-from cubicweb.utils import UStringIO, json_dumps, js_href  # noqa
-from cubicweb.web import component  # noqa
-from cubicweb.web.views import ajaxcontroller, basetemplates, facets  # noqa
-
+# other part lies in views/patches.py
 
- at monkeypatch(facets.FilterBox)
-def _get_context(self):
-    view = self.cw_extra_kwargs.get('view')
-    context = getattr(view, 'filter_box_context_info', lambda: None)()
-    if context:
-        rset, vid, divid, paginate = context
-    else:
-        rset = self.cw_rset
-        vid, divid = None, 'contentmain'
-        paginate = view and view.paginable
-    return rset, vid, divid, paginate
+from cubicweb.utils import json_dumps, js_href  # noqa
+from cubicweb.web import component  # noqa
 
 
 @monkeypatch(component.NavigationComponent)
 def ajax_page_url(self, **params):
     divid = params.setdefault('divid', 'contentmain')
     params['rql'] = self.cw_rset.printable_rql()
     return js_href("$(%s).loadxhtml(AJAX_PREFIX_URL, %s, 'get', 'swap')" % (
         json_dumps('#' + divid), component.js.ajaxFuncArgs('view', params)))
 
 
- at monkeypatch(basetemplates.TheMainTemplate)
-def content_column(self, view, content_cols):
-    w = self.w
-    w(u'<div id="main-center" class="%(prefix)s%(col)s" role="main">' % {
-        'prefix': self.twbs_col_cls, 'col': content_cols})
-    components = self._cw.vreg['components']
-    self.content_components(view, components)
-    w(u'<div id="pageContent">')
-    self.content_header(view)
-    vtitle = self._cw.form.get('vtitle')
-    if vtitle:
-        w(u'<div class="vtitle">%s</div>\n' % xml_escape(vtitle))
-    self.state_header()
-    self.content_navrestriction_components(view, components)
-    w(u'<div id="contentmain">\n')
-    nav_html = UStringIO()
-    if view and not view.handle_pagination:
-        view.paginate(w=nav_html.write)
-    w(nav_html.getvalue())
-    view.render(w=w)
-    w(nav_html.getvalue())
-    w(u'</div>\n')  # closes id=contentmain
-    self.content_footer(view)
-    w(u'</div>\n')  # closes div#pageContent
-    w(u'</div>\n')  # closes div.%(prefix)s-%(col)s
-
-
- at monkeypatch(ajaxcontroller.AjaxFunction)
-def _call_view(self, view, paginate=False, **kwargs):
-    divid = self._cw.form.get('divid')
-    # we need to call pagination before with the stream set
-    try:
-        stream = view.set_stream()
-    except AttributeError:
-        stream = UStringIO()
-        kwargs['w'] = stream.write
-        assert not paginate
-    if divid == 'contentmain':
-        # ensure divid isn't reused by the view (e.g. table view)
-        del self._cw.form['divid']
-        # mimick main template behaviour
-        paginate = True
-    if divid == 'contentmain':
-        stream.write(u'<div id="contentmain">')
-    nav_html = UStringIO()
-    if paginate and not view.handle_pagination:
-        view.paginate(w=nav_html.write)
-    stream.write(nav_html.getvalue())
-    view.render(**kwargs)
-    stream.write(nav_html.getvalue())
-    if divid == 'contentmain':
-        stream.write(u'</div>')
-    extresources = self._cw.html_headers.getvalue(skiphead=True)
-    if extresources:
-        stream.write(u'<div class="ajaxHtmlHead">\n')
-        stream.write(extresources)
-        stream.write(u'</div>\n')
-    return stream.getvalue()
-
-
 # Fixed rql reqwrite (https://www.cubicweb.org/ticket/17074119)
 
 from cubicweb.rqlrewrite import RQLRewriter, n, remove_solutions  # noqa
 
 
diff --git a/cubicweb_saem_ref/views/patches.py b/cubicweb_saem_ref/views/patches.py
--- a/cubicweb_saem_ref/views/patches.py
+++ b/cubicweb_saem_ref/views/patches.py
@@ -41,5 +41,87 @@ def render(self, registry, oid, eid=None
 
 
 def registration_callback(vreg):
     vreg.register_all(globals().values(), __name__, (render,))
     vreg.register_and_replace(render, ajaxcontroller.render)
+
+
+# avoid disappearance of navtop components (https://www.cubicweb.org/17074195) #####################
+# other part lies in site_cubicweb.py
+
+from logilab.common.decorators import monkeypatch  # noqa
+from logilab.mtconverter import xml_escape  # noqa
+from cubicweb.utils import UStringIO  # noqa
+from cubicweb.web.views import ajaxcontroller, basetemplates, facets  # noqa
+
+
+ at monkeypatch(facets.FilterBox)
+def _get_context(self):
+    view = self.cw_extra_kwargs.get('view')
+    context = getattr(view, 'filter_box_context_info', lambda: None)()
+    if context:
+        rset, vid, divid, paginate = context
+    else:
+        rset = self.cw_rset
+        vid, divid = None, 'contentmain'
+        paginate = view and view.paginable
+    return rset, vid, divid, paginate
+
+
+ at monkeypatch(basetemplates.TheMainTemplate)
+def content_column(self, view, content_cols):
+    w = self.w
+    w(u'<div id="main-center" class="%(prefix)s%(col)s" role="main">' % {
+        'prefix': self.twbs_col_cls, 'col': content_cols})
+    components = self._cw.vreg['components']
+    self.content_components(view, components)
+    w(u'<div id="pageContent">')
+    self.content_header(view)
+    vtitle = self._cw.form.get('vtitle')
+    if vtitle:
+        w(u'<div class="vtitle">%s</div>\n' % xml_escape(vtitle))
+    self.state_header()
+    self.content_navrestriction_components(view, components)
+    w(u'<div id="contentmain">\n')
+    nav_html = UStringIO()
+    if view and not view.handle_pagination:
+        view.paginate(w=nav_html.write)
+    w(nav_html.getvalue())
+    view.render(w=w)
+    w(nav_html.getvalue())
+    w(u'</div>\n')  # closes id=contentmain
+    self.content_footer(view)
+    w(u'</div>\n')  # closes div#pageContent
+    w(u'</div>\n')  # closes div.%(prefix)s-%(col)s
+
+
+ at monkeypatch(ajaxcontroller.AjaxFunction)
+def _call_view(self, view, paginate=False, **kwargs):
+    divid = self._cw.form.get('divid')
+    # we need to call pagination before with the stream set
+    try:
+        stream = view.set_stream()
+    except AttributeError:
+        stream = UStringIO()
+        kwargs['w'] = stream.write
+        assert not paginate
+    if divid == 'contentmain':
+        # ensure divid isn't reused by the view (e.g. table view)
+        del self._cw.form['divid']
+        # mimick main template behaviour
+        paginate = True
+    if divid == 'contentmain':
+        stream.write(u'<div id="contentmain">')
+    nav_html = UStringIO()
+    if paginate and not view.handle_pagination:
+        view.paginate(w=nav_html.write)
+    stream.write(nav_html.getvalue())
+    view.render(**kwargs)
+    stream.write(nav_html.getvalue())
+    if divid == 'contentmain':
+        stream.write(u'</div>')
+    extresources = self._cw.html_headers.getvalue(skiphead=True)
+    if extresources:
+        stream.write(u'<div class="ajaxHtmlHead">\n')
+        stream.write(extresources)
+        stream.write(u'</div>\n')
+    return stream.getvalue()


More information about the saem-devel mailing list