author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 25 May 2010 09:50:12 +0200 | |
changeset 5575 | 8a531340c3ef |
parent 5556 | 9ab2b4c74baf |
child 5595 | d179e8229642 |
permissions | -rw-r--r-- |
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 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
2259 | 21 |
_ = unicode |
0 | 22 |
|
23 |
from rql.nodes import VariableRef, Constant |
|
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 | 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 | 32 |
from cubicweb.web.component import EntityVComponent, NavigationComponent |
33 |
||
34 |
||
35 |
class PageNavigation(NavigationComponent): |
|
36 |
||
37 |
def call(self): |
|
38 |
"""displays a resultset by page""" |
|
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 | 42 |
page_size = self.page_size |
43 |
start = 0 |
|
44 |
blocklist = [] |
|
45 |
params = dict(req.form) |
|
46 |
self.clean_params(params) |
|
47 |
basepath = req.relative_path(includeparams=False) |
|
48 |
while start < rset.rowcount: |
|
49 |
stop = min(start + page_size - 1, rset.rowcount - 1) |
|
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 | 52 |
start = stop + 1 |
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 ' % self.previous_link(basepath, params)) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
55 |
w(u'[ %s ]' % u' | '.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' %s' % self.next_link(basepath, params)) |
0 | 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 | 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 | 63 |
class SortedNavigation(NavigationComponent): |
64 |
"""sorted navigation apply if navigation is needed (according to page size) |
|
65 |
and if the result set is sorted |
|
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 | 69 |
# number of considered chars to build page links |
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 | 72 |
def display_func(self, rset, col, attrname): |
73 |
if attrname is not None: |
|
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 | 77 |
entity = rset.get_entity(row, col) |
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 | 80 |
def index_display(row): |
81 |
return unicode(rset[row][col]) |
|
82 |
else: |
|
83 |
def index_display(row): |
|
84 |
return rset.get_entity(row, col).view('text') |
|
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 | 87 |
def call(self): |
88 |
"""displays links to navigate accross pages of a result set |
|
89 |
||
90 |
Displayed result is done according to a variable on which the sort |
|
91 |
is done, and looks like: |
|
92 |
[ana - cro] | [cro - ghe] | ... | [tim - zou] |
|
93 |
""" |
|
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 | 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 | 98 |
# attrname = the name of attribute according to which the sort |
99 |
# is done if any |
|
100 |
for sorterm in rset.syntax_tree().children[0].orderby: |
|
101 |
if isinstance(sorterm.term, Constant): |
|
102 |
col = sorterm.term.value - 1 |
|
103 |
index_display = self.display_func(rset, col, None) |
|
104 |
break |
|
105 |
var = sorterm.term.get_nodes(VariableRef)[0].variable |
|
106 |
col = None |
|
107 |
for ref in var.references(): |
|
108 |
rel = ref.relation() |
|
109 |
if rel is None: |
|
110 |
continue |
|
111 |
attrname = rel.r_type |
|
112 |
if attrname == 'is': |
|
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 | 115 |
col = var.selected_index() |
116 |
attrname = None |
|
117 |
if col is None: |
|
118 |
# final relation or not selected non final relation |
|
119 |
if var is rel.children[0]: |
|
120 |
relvar = rel.children[1].children[0].get_nodes(VariableRef)[0] |
|
121 |
else: |
|
122 |
relvar = rel.children[0].variable |
|
123 |
col = relvar.selected_index() |
|
124 |
if col is not None: |
|
125 |
break |
|
126 |
else: |
|
127 |
# no relation but maybe usable anyway if selected |
|
128 |
col = var.selected_index() |
|
129 |
attrname = None |
|
130 |
if col is not None: |
|
131 |
index_display = self.display_func(rset, col, attrname) |
|
132 |
break |
|
133 |
else: |
|
134 |
# nothing usable found, use the first column |
|
135 |
index_display = self.display_func(rset, 0, None) |
|
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 | 138 |
self.clean_params(params) |
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 | 141 |
while start < rset.rowcount: |
142 |
stop = min(start + page_size - 1, rset.rowcount - 1) |
|
143 |
cell = self.format_link_content(index_display(start), index_display(stop)) |
|
144 |
blocklist.append(self.page_link(basepath, params, start, stop, cell)) |
|
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 | 147 |
|
148 |
def format_link_content(self, startstr, stopstr): |
|
149 |
text = u'%s - %s' % (startstr.lower()[:self.nb_chars], |
|
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 | 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 | 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 ' % self.previous_link(basepath, params)) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
156 |
self.w(u'[ %s ]' % u' | '.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' %s' % self.next_link(basepath, params)) |
0 | 158 |
self.w(u'</div>') |
159 |
||
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 | 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 | 183 |
# register msg not generated since no entity implements IPrevNext in cubicweb |
184 |
# itself |
|
185 |
title = _('contentnavigation_prevnext') |
|
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 | 189 |
context = 'navbottom' |
190 |
order = 10 |
|
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 | 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 | 198 |
self.w(u'<div class="prevnext">') |
199 |
if previous: |
|
200 |
self.w(u'<div class="previousEntity left">') |
|
201 |
self.w(self.previous_link(previous, textsize)) |
|
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 | 205 |
if next: |
206 |
self.w(u'<div class="nextEntity right">') |
|
207 |
self.w(self.next_link(next, textsize)) |
|
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 | 211 |
self.w(u'</div>') |
212 |
self.w(u'<div class="clear"></div>') |
|
213 |
||
214 |
def previous_link(self, previous, textsize): |
|
215 |
return u'<a href="%s" title="%s"><< %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 | 220 |
def next_link(self, next, textsize): |
221 |
return u'<a href="%s" title="%s">%s >></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 | 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 | 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) |