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

Sylvain Thénault sylvain.thenault at logilab.fr
Fri May 19 09:44:39 CEST 2017



Le 19/05/2017 à 09:29, Denis Laxalde a écrit :
> Sylvain Thenault a écrit :
>> # 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/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)
>
> Looks strange to have registration_callback defined in the middle of the
> file. Could you keep it at the end?
> I can do this while applying if this okay for you.

please do, this was done inadvertently.

>> +
>> +
>> +# 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()
>>
>

-- 
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