web/views/navigation.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 May 2010 20:47:55 +0200
changeset 5556 9ab2b4c74baf
parent 5424 8ecbcbff9777
child 5595 d179e8229642
permissions -rw-r--r--
[entity] introduce a new 'adapters' registry This changeset introduces the notion in adapters (as in Zope Component Architecture) in a cubicweb way, eg using a specific registry of appobjects. This allows nicer code structure, by avoid clutering entity classes and moving code usually specific to a place of the ui (or something else) together with the code that use the interface. We don't use actual interface anymore, they are implied by adapters (which may be abstract), whose reg id is an interface name. Appobjects that used to 'implements(IFace)' should now be rewritten by: * coding an IFaceAdapter(EntityAdapter) defining (implementing if desired) the interface, usually with __regid__ = 'IFace' * use "adaptable('IFace')" as selector instead Also, the implements_adapter_compat decorator eases backward compatibility with adapter's methods that may still be found on entities implementing the interface. Notice that unlike ZCA, we don't support automatic adapters chain (yagni?). All interfaces defined in cubicweb have been turned into adapters, also some new ones have been introduced to cleanup Entity / AnyEntity classes namespace. At the end, the pluggable mixins mecanism should disappear in favor of adapters as well.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""navigation components definition for CubicWeb web client"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
2259
5d90bd26af8b cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    21
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from rql.nodes import VariableRef, Constant
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
    25
from logilab.mtconverter import xml_escape
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2381
diff changeset
    26
from logilab.common.deprecation import deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
692
800592b8d39b replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 588
diff changeset
    28
from cubicweb.selectors import (paginated_rset, sorted_rset,
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    29
                                adaptable, implements)
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    30
from cubicweb.uilib import cut
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    31
from cubicweb.view import EntityAdapter, implements_adapter_compat
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
from cubicweb.web.component import EntityVComponent, NavigationComponent
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
class PageNavigation(NavigationComponent):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    def call(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        """displays a resultset by page"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        w = self.w
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    40
        req = self._cw
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    41
        rset = self.cw_rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
        page_size = self.page_size
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
        start = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        blocklist = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        params = dict(req.form)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        self.clean_params(params)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        basepath = req.relative_path(includeparams=False)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        while start < rset.rowcount:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
            stop = min(start + page_size - 1, rset.rowcount - 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
            blocklist.append(self.page_link(basepath, params, start, stop,
588
f37ee42cd0e9 permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 445
diff changeset
    51
                                            self.index_display(start, stop)))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
            start = stop + 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        w(u'<div class="pagination">')
3014
62ce5a139b52 [page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
    54
        w(u'%s&#160;' % self.previous_link(basepath, params))
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2650
diff changeset
    55
        w(u'[&#160;%s&#160;]' % u'&#160;| '.join(blocklist))
3014
62ce5a139b52 [page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
    56
        w(u'&#160;%s' % self.next_link(basepath, params))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        w(u'</div>')
1657
14e95f7aa033 delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents: 873
diff changeset
    58
588
f37ee42cd0e9 permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 445
diff changeset
    59
    def index_display(self, start, stop):
f37ee42cd0e9 permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 445
diff changeset
    60
        return u'%s - %s' % (start+1, stop+1)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    62
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
class SortedNavigation(NavigationComponent):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    """sorted navigation apply if navigation is needed (according to page size)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
    and if the result set is sorted
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    """
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 692
diff changeset
    67
    __select__ = paginated_rset() & sorted_rset()
1657
14e95f7aa033 delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents: 873
diff changeset
    68
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    # number of considered chars to build page links
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    nb_chars = 5
1657
14e95f7aa033 delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents: 873
diff changeset
    71
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    def display_func(self, rset, col, attrname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        if attrname is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
            def index_display(row):
3751
ff404cef5244 take care to outer joins
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
    75
                if not rset[row][col]: # outer join
ff404cef5244 take care to outer joins
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
    76
                    return u''
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
                entity = rset.get_entity(row, col)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
                return entity.printable_value(attrname, format='text/plain')
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4023
diff changeset
    79
        elif self._cw.vreg.schema.eschema(rset.description[0][col]).final:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
            def index_display(row):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
                return unicode(rset[row][col])
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
            def index_display(row):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
                return rset.get_entity(row, col).view('text')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        return index_display
1657
14e95f7aa033 delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents: 873
diff changeset
    86
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    def call(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
        """displays links to navigate accross pages of a result set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        Displayed result is done according to a variable on which the sort
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        is done, and looks like:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        [ana - cro] | [cro - ghe] | ... | [tim - zou]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        w = self.w
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    95
        rset = self.cw_rset
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        page_size = self.page_size
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4023
diff changeset
    97
        rschema = self._cw.vreg.schema.rschema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        # attrname = the name of attribute according to which the sort
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        # is done if any
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        for sorterm in rset.syntax_tree().children[0].orderby:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
            if isinstance(sorterm.term, Constant):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
                col = sorterm.term.value - 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
                index_display = self.display_func(rset, col, None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
                break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
            var = sorterm.term.get_nodes(VariableRef)[0].variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
            col = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
            for ref in var.references():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
                rel = ref.relation()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
                if rel is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
                attrname = rel.r_type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
                if attrname == 'is':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
                    continue
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3018
diff changeset
   114
                if not rschema(attrname).final:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
                    col = var.selected_index()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
                    attrname = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
                if col is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
                    # final relation or not selected non final relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
                    if var is rel.children[0]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
                        relvar = rel.children[1].children[0].get_nodes(VariableRef)[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
                    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
                        relvar = rel.children[0].variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
                    col = relvar.selected_index()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
                if col is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
                    break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
                # no relation but maybe usable anyway if selected
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
                col = var.selected_index()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
                attrname = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
            if col is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
                index_display = self.display_func(rset, col, attrname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
                break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
            # nothing usable found, use the first column
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
            index_display = self.display_func(rset, 0, None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        blocklist = []
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   137
        params = dict(self._cw.form)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        self.clean_params(params)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        start = 0
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   140
        basepath = self._cw.relative_path(includeparams=False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        while start < rset.rowcount:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
            stop = min(start + page_size - 1, rset.rowcount - 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
            cell = self.format_link_content(index_display(start), index_display(stop))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
            blocklist.append(self.page_link(basepath, params, start, stop, cell))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
            start = stop + 1
3014
62ce5a139b52 [page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
   146
        self.write_links(basepath, params, blocklist)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
    def format_link_content(self, startstr, stopstr):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        text = u'%s - %s' % (startstr.lower()[:self.nb_chars],
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
                             stopstr.lower()[:self.nb_chars])
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   151
        return xml_escape(text)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
3014
62ce5a139b52 [page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
   153
    def write_links(self, basepath, params, blocklist):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        self.w(u'<div class="pagination">')
3014
62ce5a139b52 [page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
   155
        self.w(u'%s&#160;' % self.previous_link(basepath, params))
2996
866a2c135c33 B #345282 xhtml requires to use &#160; instead of &nbsp;
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2650
diff changeset
   156
        self.w(u'[&#160;%s&#160;]' % u'&#160;| '.join(blocklist))
3014
62ce5a139b52 [page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2996
diff changeset
   157
        self.w(u'&#160;%s' % self.next_link(basepath, params))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
        self.w(u'</div>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   161
from cubicweb.interfaces import IPrevNext
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   162
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   163
class IPrevNextAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   164
    """interface for entities which can be linked to a previous and/or next
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   165
    entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   166
    """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   167
    __regid__ = 'IPrevNext'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   168
    __select__ = implements(IPrevNext) # XXX for bw compat, else should be abstract
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   169
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   170
    @implements_adapter_compat('IPrevNext')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   171
    def next_entity(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   172
        """return the 'next' entity"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   173
        raise NotImplementedError
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   174
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   175
    @implements_adapter_compat('IPrevNext')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   176
    def previous_entity(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   177
        """return the 'previous' entity"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   178
        raise NotImplementedError
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   179
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   180
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
class NextPrevNavigationComponent(EntityVComponent):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
   182
    __regid__ = 'prevnext'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
    # register msg not generated since no entity implements IPrevNext in cubicweb
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
    # itself
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
    title = _('contentnavigation_prevnext')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
    help = _('contentnavigation_prevnext_description')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   187
    __select__ = (EntityVComponent.__select__
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   188
                  & adaptable('IPrevNext'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
    context = 'navbottom'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
    order = 10
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
    def call(self, view=None):
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   192
        entity = self.cw_rset.get_entity(0, 0)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   193
        adapter = entity.cw_adapt_to('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   194
        previous = adapter.previous_entity()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
   195
        next = adapter.next_entity()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        if previous or next:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   197
            textsize = self._cw.property_value('navigation.short-line-size')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
            self.w(u'<div class="prevnext">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
            if previous:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
                self.w(u'<div class="previousEntity left">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
                self.w(self.previous_link(previous, textsize))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
                self.w(u'</div>')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   203
                self._cw.html_headers.add_raw('<link rel="prev" href="%s" />'
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   204
                                              % xml_escape(previous.absolute_url()))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
            if next:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
                self.w(u'<div class="nextEntity right">')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
                self.w(self.next_link(next, textsize))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
                self.w(u'</div>')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   209
                self._cw.html_headers.add_raw('<link rel="next" href="%s" />'
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   210
                                              % xml_escape(next.absolute_url()))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
            self.w(u'</div>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
            self.w(u'<div class="clear"></div>')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
    def previous_link(self, previous, textsize):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
        return u'<a href="%s" title="%s">&lt;&lt; %s</a>' % (
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   216
            xml_escape(previous.absolute_url()),
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   217
            self._cw._('i18nprevnext_previous'),
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   218
            xml_escape(cut(previous.dc_title(), textsize)))
1657
14e95f7aa033 delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents: 873
diff changeset
   219
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
    def next_link(self, next, textsize):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
        return u'<a href="%s" title="%s">%s &gt;&gt;</a>' % (
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   222
            xml_escape(next.absolute_url()),
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   223
            self._cw._('i18nprevnext_next'),
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2259
diff changeset
   224
            xml_escape(cut(next.dc_title(), textsize)))
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   225
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   226
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   227
def do_paginate(view, rset=None, w=None, show_all_option=True, page_size=None):
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   228
    """write pages index in w stream (default to view.w) and then limit the result
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   229
    set (default to view.rset) to the currently displayed page
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   230
    """
4265
0912f111b028 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   231
    req = view._cw
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   232
    if rset is None:
4265
0912f111b028 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   233
        rset = view.cw_rset
4357
ef8e20bfa287 case where 'w' argument is None should be handled in do_paginate
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4336
diff changeset
   234
    if w is None:
ef8e20bfa287 case where 'w' argument is None should be handled in do_paginate
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4336
diff changeset
   235
        w = view.w
4252
6c4f109c2b03 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4072 4244
diff changeset
   236
    nav = req.vreg['components'].select_or_none(
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   237
        'navigation', req, rset=rset, page_size=page_size)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   238
    if nav:
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   239
        if w is None:
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   240
            w = view.w
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   241
        # get boundaries before component rendering
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   242
        start, stop = nav.page_boundaries()
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   243
        nav.render(w=w)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   244
        params = dict(req.form)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   245
        nav.clean_params(params)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   246
        # make a link to see them all
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   247
        if show_all_option:
4382
6fb02edd05da 3.6 api update, cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4357
diff changeset
   248
            url = xml_escape(req.build_url(__force_display=1, **params))
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   249
            w(u'<span><a href="%s">%s</a></span>\n'
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   250
              % (url, req._('show %s results') % len(rset)))
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   251
        rset.limit(offset=start, limit=stop-start, inplace=True)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   252
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   253
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   254
def paginate(view, show_all_option=True, w=None, page_size=None, rset=None):
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   255
    """paginate results if the view is paginable and we're not explictly told to
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   256
    display everything (by setting __force_display in req.form)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   257
    """
4336
45e5b8bd2f71 backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4335 4332
diff changeset
   258
    if view.paginable and not view._cw.form.get('__force_display'):
4357
ef8e20bfa287 case where 'w' argument is None should be handled in do_paginate
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4336
diff changeset
   259
        do_paginate(view, rset, w, show_all_option, page_size)
4244
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   260
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   261
# monkey patch base View class to add a .paginate([...])
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   262
# method to be called to write pages index in the view and then limit the result
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   263
# set to the current page
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   264
from cubicweb.view import View
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   265
View.do_paginate = do_paginate
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   266
View.paginate = paginate
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   267
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   268
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   269
#@deprecated (see below)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   270
def limit_rset_using_paged_nav(self, req, rset, w, forcedisplay=False,
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   271
                               show_all_option=True, page_size=None):
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   272
    if not (forcedisplay or req.form.get('__force_display') is not None):
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   273
        do_paginate(self, rset, w, show_all_option, page_size)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   274
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   275
View.pagination = deprecated('[3.2] .pagination is deprecated, use paginate')(
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   276
    limit_rset_using_paged_nav)
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   277
limit_rset_using_paged_nav = deprecated('[3.6] limit_rset_using_paged_nav is deprecated, use do_paginate')(
2c3de1953d00 rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4213
diff changeset
   278
    limit_rset_using_paged_nav)