author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Thu, 07 Jan 2010 17:41:50 +0100 | |
branch | stable |
changeset 4213 | d4eeaf0393e2 |
parent 4212 | ab6573088b4a |
child 4244 | 2c3de1953d00 |
permissions | -rw-r--r-- |
0 | 1 |
"""navigation components definition for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4057
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2259 | 9 |
_ = unicode |
0 | 10 |
|
11 |
from rql.nodes import VariableRef, Constant |
|
12 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
13 |
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
|
14 |
from logilab.common.deprecation import deprecated |
0 | 15 |
|
16 |
from cubicweb.interfaces import IPrevNext |
|
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
|
17 |
from cubicweb.selectors import (paginated_rset, sorted_rset, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
588
diff
changeset
|
18 |
primary_view, match_context_prop, |
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
588
diff
changeset
|
19 |
one_line_rset, implements) |
0 | 20 |
from cubicweb.common.uilib import cut |
21 |
from cubicweb.web.component import EntityVComponent, NavigationComponent |
|
22 |
||
23 |
||
24 |
class PageNavigation(NavigationComponent): |
|
25 |
||
26 |
def call(self): |
|
27 |
"""displays a resultset by page""" |
|
28 |
w = self.w |
|
29 |
req = self.req |
|
30 |
rset = self.rset |
|
31 |
page_size = self.page_size |
|
32 |
start = 0 |
|
33 |
blocklist = [] |
|
34 |
params = dict(req.form) |
|
35 |
self.clean_params(params) |
|
36 |
basepath = req.relative_path(includeparams=False) |
|
37 |
while start < rset.rowcount: |
|
38 |
stop = min(start + page_size - 1, rset.rowcount - 1) |
|
39 |
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
|
40 |
self.index_display(start, stop))) |
0 | 41 |
start = stop + 1 |
42 |
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
|
43 |
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
|
44 |
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
|
45 |
w(u' %s' % self.next_link(basepath, params)) |
0 | 46 |
w(u'</div>') |
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
47 |
|
588
f37ee42cd0e9
permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
445
diff
changeset
|
48 |
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
|
49 |
return u'%s - %s' % (start+1, stop+1) |
0 | 50 |
|
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
|
51 |
|
0 | 52 |
class SortedNavigation(NavigationComponent): |
53 |
"""sorted navigation apply if navigation is needed (according to page size) |
|
54 |
and if the result set is sorted |
|
55 |
""" |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
56 |
__select__ = paginated_rset() & sorted_rset() |
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
57 |
|
0 | 58 |
# number of considered chars to build page links |
59 |
nb_chars = 5 |
|
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
60 |
|
0 | 61 |
def display_func(self, rset, col, attrname): |
62 |
req = self.req |
|
63 |
if attrname is not None: |
|
64 |
def index_display(row): |
|
3751
ff404cef5244
take care to outer joins
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
65 |
if not rset[row][col]: # outer join |
ff404cef5244
take care to outer joins
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
66 |
return u'' |
0 | 67 |
entity = rset.get_entity(row, col) |
68 |
return entity.printable_value(attrname, format='text/plain') |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3018
diff
changeset
|
69 |
elif self.schema.eschema(rset.description[0][col]).final: |
0 | 70 |
def index_display(row): |
71 |
return unicode(rset[row][col]) |
|
72 |
else: |
|
73 |
def index_display(row): |
|
74 |
return rset.get_entity(row, col).view('text') |
|
75 |
return index_display |
|
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
76 |
|
0 | 77 |
def call(self): |
78 |
"""displays links to navigate accross pages of a result set |
|
79 |
||
80 |
Displayed result is done according to a variable on which the sort |
|
81 |
is done, and looks like: |
|
82 |
[ana - cro] | [cro - ghe] | ... | [tim - zou] |
|
83 |
""" |
|
84 |
w = self.w |
|
85 |
rset = self.rset |
|
86 |
page_size = self.page_size |
|
87 |
rschema = self.schema.rschema |
|
88 |
# attrname = the name of attribute according to which the sort |
|
89 |
# is done if any |
|
90 |
for sorterm in rset.syntax_tree().children[0].orderby: |
|
91 |
if isinstance(sorterm.term, Constant): |
|
92 |
col = sorterm.term.value - 1 |
|
93 |
index_display = self.display_func(rset, col, None) |
|
94 |
break |
|
95 |
var = sorterm.term.get_nodes(VariableRef)[0].variable |
|
96 |
col = None |
|
97 |
for ref in var.references(): |
|
98 |
rel = ref.relation() |
|
99 |
if rel is None: |
|
100 |
continue |
|
101 |
attrname = rel.r_type |
|
102 |
if attrname == 'is': |
|
103 |
continue |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3018
diff
changeset
|
104 |
if not rschema(attrname).final: |
0 | 105 |
col = var.selected_index() |
106 |
attrname = None |
|
107 |
if col is None: |
|
108 |
# final relation or not selected non final relation |
|
109 |
if var is rel.children[0]: |
|
110 |
relvar = rel.children[1].children[0].get_nodes(VariableRef)[0] |
|
111 |
else: |
|
112 |
relvar = rel.children[0].variable |
|
113 |
col = relvar.selected_index() |
|
114 |
if col is not None: |
|
115 |
break |
|
116 |
else: |
|
117 |
# no relation but maybe usable anyway if selected |
|
118 |
col = var.selected_index() |
|
119 |
attrname = None |
|
120 |
if col is not None: |
|
121 |
index_display = self.display_func(rset, col, attrname) |
|
122 |
break |
|
123 |
else: |
|
124 |
# nothing usable found, use the first column |
|
125 |
index_display = self.display_func(rset, 0, None) |
|
126 |
blocklist = [] |
|
127 |
params = dict(self.req.form) |
|
128 |
self.clean_params(params) |
|
129 |
start = 0 |
|
130 |
basepath = self.req.relative_path(includeparams=False) |
|
131 |
while start < rset.rowcount: |
|
132 |
stop = min(start + page_size - 1, rset.rowcount - 1) |
|
133 |
cell = self.format_link_content(index_display(start), index_display(stop)) |
|
134 |
blocklist.append(self.page_link(basepath, params, start, stop, cell)) |
|
135 |
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
|
136 |
self.write_links(basepath, params, blocklist) |
0 | 137 |
|
138 |
def format_link_content(self, startstr, stopstr): |
|
139 |
text = u'%s - %s' % (startstr.lower()[:self.nb_chars], |
|
140 |
stopstr.lower()[:self.nb_chars]) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
141 |
return xml_escape(text) |
0 | 142 |
|
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
|
143 |
def write_links(self, basepath, params, blocklist): |
0 | 144 |
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
|
145 |
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
|
146 |
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
|
147 |
self.w(u' %s' % self.next_link(basepath, params)) |
0 | 148 |
self.w(u'</div>') |
149 |
||
150 |
||
588
f37ee42cd0e9
permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
445
diff
changeset
|
151 |
def limit_rset_using_paged_nav(self, req, rset, w, forcedisplay=False, |
873 | 152 |
show_all_option=True, page_size=None): |
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
|
153 |
if not (forcedisplay or req.form.get('__force_display') is not None): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
154 |
nav = self.vreg['components'].select_object('navigation', req, |
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
|
155 |
rset=rset, page_size=page_size) |
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
|
156 |
if nav: |
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
|
157 |
# get boundaries before component rendering |
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
|
158 |
start, stop = nav.page_boundaries() |
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
|
159 |
nav.render(w=w) |
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
|
160 |
params = dict(req.form) |
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
|
161 |
nav.clean_params(params) |
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
|
162 |
# make a link to see them all |
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
|
163 |
if show_all_option: |
2381 | 164 |
url = xml_escape(self.build_url(__force_display=1, **params)) |
3018 | 165 |
w(u'<span><a href="%s">%s</a></span>\n' |
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
|
166 |
% (url, req._('show %s results') % len(rset))) |
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
|
167 |
rset.limit(offset=start, limit=stop-start, inplace=True) |
0 | 168 |
|
169 |
||
170 |
# monkey patch base View class to add a .pagination(req, rset, w, forcedisplay) |
|
171 |
# method to be called on view's result set and printing pages index in the view |
|
834 | 172 |
from cubicweb.view import View |
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2381
diff
changeset
|
173 |
View.pagination = deprecated('.pagination is deprecated, use paginate')(limit_rset_using_paged_nav) |
0 | 174 |
|
4057
a5ec0475601e
new rset argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3751
diff
changeset
|
175 |
def paginate(view, show_all_option=True, w=None, page_size=None, rset=None): |
a5ec0475601e
new rset argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3751
diff
changeset
|
176 |
if rset is None: |
a5ec0475601e
new rset argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3751
diff
changeset
|
177 |
rset = view.rset |
4213
d4eeaf0393e2
[web/views] if view has attribute paginable=False, do not try to paginate it
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
178 |
if getattr(view, 'paginable', True): |
d4eeaf0393e2
[web/views] if view has attribute paginable=False, do not try to paginate it
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
179 |
limit_rset_using_paged_nav(view, view.req, rset, w or view.w, |
d4eeaf0393e2
[web/views] if view has attribute paginable=False, do not try to paginate it
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
180 |
not view.need_navigation, show_all_option, |
d4eeaf0393e2
[web/views] if view has attribute paginable=False, do not try to paginate it
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
181 |
page_size=page_size) |
0 | 182 |
View.paginate = paginate |
183 |
||
184 |
class NextPrevNavigationComponent(EntityVComponent): |
|
185 |
id = 'prevnext' |
|
186 |
# register msg not generated since no entity implements IPrevNext in cubicweb |
|
187 |
# itself |
|
188 |
title = _('contentnavigation_prevnext') |
|
189 |
help = _('contentnavigation_prevnext_description') |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
190 |
__select__ = (one_line_rset() & primary_view() |
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
191 |
& match_context_prop() & implements(IPrevNext)) |
0 | 192 |
context = 'navbottom' |
193 |
order = 10 |
|
194 |
def call(self, view=None): |
|
195 |
entity = self.entity(0) |
|
196 |
previous = entity.previous_entity() |
|
197 |
next = entity.next_entity() |
|
198 |
if previous or next: |
|
199 |
textsize = self.req.property_value('navigation.short-line-size') |
|
200 |
self.w(u'<div class="prevnext">') |
|
201 |
if previous: |
|
202 |
self.w(u'<div class="previousEntity left">') |
|
203 |
self.w(self.previous_link(previous, textsize)) |
|
204 |
self.w(u'</div>') |
|
205 |
self.req.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
|
206 |
% xml_escape(previous.absolute_url())) |
0 | 207 |
if next: |
208 |
self.w(u'<div class="nextEntity right">') |
|
209 |
self.w(self.next_link(next, textsize)) |
|
210 |
self.w(u'</div>') |
|
211 |
self.req.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
|
212 |
% xml_escape(next.absolute_url())) |
0 | 213 |
self.w(u'</div>') |
214 |
self.w(u'<div class="clear"></div>') |
|
215 |
||
216 |
def previous_link(self, previous, textsize): |
|
217 |
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
|
218 |
xml_escape(previous.absolute_url()), |
0 | 219 |
self.req._('i18nprevnext_previous'), |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
220 |
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
|
221 |
|
0 | 222 |
def next_link(self, next, textsize): |
223 |
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
|
224 |
xml_escape(next.absolute_url()), |
0 | 225 |
self.req._('i18nprevnext_next'), |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
226 |
xml_escape(cut(next.dc_title(), textsize))) |