author | Katia Saurfelt <katia.saurfelt@logilab.fr> |
Wed, 01 Jul 2009 11:33:40 +0200 | |
branch | stable |
changeset 2213 | 713e220e7425 |
parent 1977 | 606923dff11b |
child 2058 | 7ef12c03447c |
child 2259 | 5d90bd26af8b |
permissions | -rw-r--r-- |
0 | 1 |
"""navigation components definition for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
4 |
:copyright: 2001-2009 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" |
|
9 |
||
10 |
from rql.nodes import VariableRef, Constant |
|
11 |
||
12 |
from logilab.mtconverter import html_escape |
|
834 | 13 |
from logilab.common.deprecation import obsolete |
0 | 14 |
|
15 |
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
|
16 |
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
|
17 |
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
|
18 |
one_line_rset, implements) |
0 | 19 |
from cubicweb.common.uilib import cut |
20 |
from cubicweb.web.component import EntityVComponent, NavigationComponent |
|
21 |
||
22 |
_ = unicode |
|
23 |
||
24 |
||
25 |
class PageNavigation(NavigationComponent): |
|
26 |
||
27 |
def call(self): |
|
28 |
"""displays a resultset by page""" |
|
29 |
w = self.w |
|
30 |
req = self.req |
|
31 |
rset = self.rset |
|
32 |
page_size = self.page_size |
|
33 |
start = 0 |
|
34 |
blocklist = [] |
|
35 |
params = dict(req.form) |
|
36 |
self.clean_params(params) |
|
37 |
basepath = req.relative_path(includeparams=False) |
|
38 |
while start < rset.rowcount: |
|
39 |
stop = min(start + page_size - 1, rset.rowcount - 1) |
|
40 |
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
|
41 |
self.index_display(start, stop))) |
0 | 42 |
start = stop + 1 |
43 |
w(u'<div class="pagination">') |
|
44 |
w(u'%s ' % self.previous_link(params)) |
|
45 |
w(u'[ %s ]' % u' | '.join(blocklist)) |
|
46 |
w(u' %s' % self.next_link(params)) |
|
47 |
w(u'</div>') |
|
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
48 |
|
588
f37ee42cd0e9
permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
445
diff
changeset
|
49 |
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
|
50 |
return u'%s - %s' % (start+1, stop+1) |
0 | 51 |
|
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): |
|
65 |
entity = rset.get_entity(row, col) |
|
66 |
return entity.printable_value(attrname, format='text/plain') |
|
67 |
elif self.schema.eschema(rset.description[0][col]).is_final(): |
|
68 |
def index_display(row): |
|
69 |
return unicode(rset[row][col]) |
|
70 |
else: |
|
71 |
def index_display(row): |
|
72 |
return rset.get_entity(row, col).view('text') |
|
73 |
return index_display |
|
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
74 |
|
0 | 75 |
def call(self): |
76 |
"""displays links to navigate accross pages of a result set |
|
77 |
||
78 |
Displayed result is done according to a variable on which the sort |
|
79 |
is done, and looks like: |
|
80 |
[ana - cro] | [cro - ghe] | ... | [tim - zou] |
|
81 |
""" |
|
82 |
w = self.w |
|
83 |
rset = self.rset |
|
84 |
page_size = self.page_size |
|
85 |
rschema = self.schema.rschema |
|
86 |
# attrname = the name of attribute according to which the sort |
|
87 |
# is done if any |
|
88 |
for sorterm in rset.syntax_tree().children[0].orderby: |
|
89 |
if isinstance(sorterm.term, Constant): |
|
90 |
col = sorterm.term.value - 1 |
|
91 |
index_display = self.display_func(rset, col, None) |
|
92 |
break |
|
93 |
var = sorterm.term.get_nodes(VariableRef)[0].variable |
|
94 |
col = None |
|
95 |
for ref in var.references(): |
|
96 |
rel = ref.relation() |
|
97 |
if rel is None: |
|
98 |
continue |
|
99 |
attrname = rel.r_type |
|
100 |
if attrname == 'is': |
|
101 |
continue |
|
102 |
if not rschema(attrname).is_final(): |
|
103 |
col = var.selected_index() |
|
104 |
attrname = None |
|
105 |
if col is None: |
|
106 |
# final relation or not selected non final relation |
|
107 |
if var is rel.children[0]: |
|
108 |
relvar = rel.children[1].children[0].get_nodes(VariableRef)[0] |
|
109 |
else: |
|
110 |
relvar = rel.children[0].variable |
|
111 |
col = relvar.selected_index() |
|
112 |
if col is not None: |
|
113 |
break |
|
114 |
else: |
|
115 |
# no relation but maybe usable anyway if selected |
|
116 |
col = var.selected_index() |
|
117 |
attrname = None |
|
118 |
if col is not None: |
|
119 |
index_display = self.display_func(rset, col, attrname) |
|
120 |
break |
|
121 |
else: |
|
122 |
# nothing usable found, use the first column |
|
123 |
index_display = self.display_func(rset, 0, None) |
|
124 |
blocklist = [] |
|
125 |
params = dict(self.req.form) |
|
126 |
self.clean_params(params) |
|
127 |
start = 0 |
|
128 |
basepath = self.req.relative_path(includeparams=False) |
|
129 |
while start < rset.rowcount: |
|
130 |
stop = min(start + page_size - 1, rset.rowcount - 1) |
|
131 |
cell = self.format_link_content(index_display(start), index_display(stop)) |
|
132 |
blocklist.append(self.page_link(basepath, params, start, stop, cell)) |
|
133 |
start = stop + 1 |
|
134 |
self.write_links(params, blocklist) |
|
135 |
||
136 |
def format_link_content(self, startstr, stopstr): |
|
137 |
text = u'%s - %s' % (startstr.lower()[:self.nb_chars], |
|
138 |
stopstr.lower()[:self.nb_chars]) |
|
139 |
return html_escape(text) |
|
140 |
||
141 |
def write_links(self, params, blocklist): |
|
142 |
self.w(u'<div class="pagination">') |
|
143 |
self.w(u'%s ' % self.previous_link(params)) |
|
144 |
self.w(u'[ %s ]' % u' | '.join(blocklist)) |
|
145 |
self.w(u' %s' % self.next_link(params)) |
|
146 |
self.w(u'</div>') |
|
147 |
||
148 |
||
588
f37ee42cd0e9
permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
445
diff
changeset
|
149 |
def limit_rset_using_paged_nav(self, req, rset, w, forcedisplay=False, |
873 | 150 |
show_all_option=True, page_size=None): |
0 | 151 |
showall = forcedisplay or req.form.get('__force_display') is not None |
588
f37ee42cd0e9
permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
445
diff
changeset
|
152 |
nav = not showall and self.vreg.select_component('navigation', req, rset, |
f37ee42cd0e9
permit override of display index on simple navigation
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
445
diff
changeset
|
153 |
page_size=page_size) |
0 | 154 |
if nav: |
155 |
# get boundaries before component rendering |
|
156 |
start, stop = nav.page_boundaries() |
|
1723 | 157 |
nav.render(w=w) |
0 | 158 |
params = dict(req.form) |
159 |
nav.clean_params(params) |
|
160 |
# make a link to see them all |
|
161 |
if show_all_option: |
|
162 |
url = html_escape(self.build_url(__force_display=1, **params)) |
|
163 |
w(u'<p><a href="%s">%s</a></p>\n' |
|
164 |
% (url, req._('show %s results') % len(rset))) |
|
165 |
rset.limit(offset=start, limit=stop-start, inplace=True) |
|
166 |
||
167 |
||
168 |
# monkey patch base View class to add a .pagination(req, rset, w, forcedisplay) |
|
169 |
# method to be called on view's result set and printing pages index in the view |
|
834 | 170 |
from cubicweb.view import View |
171 |
View.pagination = obsolete('.pagination is deprecated, use paginate')(limit_rset_using_paged_nav) |
|
0 | 172 |
|
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
173 |
def paginate(view, show_all_option=True, w=None, page_size=None): |
0 | 174 |
limit_rset_using_paged_nav(view, view.req, view.rset, w or view.w, |
1657
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
175 |
not view.need_navigation, show_all_option, |
14e95f7aa033
delete-trailing-whitespaces, add page_size argument to .paginate
sylvain.thenault@logilab.fr
parents:
873
diff
changeset
|
176 |
page_size=page_size) |
0 | 177 |
View.paginate = paginate |
178 |
||
179 |
class NextPrevNavigationComponent(EntityVComponent): |
|
180 |
id = 'prevnext' |
|
181 |
# register msg not generated since no entity implements IPrevNext in cubicweb |
|
182 |
# itself |
|
183 |
title = _('contentnavigation_prevnext') |
|
184 |
help = _('contentnavigation_prevnext_description') |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
185 |
__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
|
186 |
& match_context_prop() & implements(IPrevNext)) |
0 | 187 |
context = 'navbottom' |
188 |
order = 10 |
|
189 |
def call(self, view=None): |
|
190 |
entity = self.entity(0) |
|
191 |
previous = entity.previous_entity() |
|
192 |
next = entity.next_entity() |
|
193 |
if previous or next: |
|
194 |
textsize = self.req.property_value('navigation.short-line-size') |
|
195 |
self.w(u'<div class="prevnext">') |
|
196 |
if previous: |
|
197 |
self.w(u'<div class="previousEntity left">') |
|
198 |
self.w(self.previous_link(previous, textsize)) |
|
199 |
self.w(u'</div>') |
|
200 |
self.req.html_headers.add_raw('<link rel="prev" href="%s" />' |
|
201 |
% html_escape(previous.absolute_url())) |
|
202 |
if next: |
|
203 |
self.w(u'<div class="nextEntity right">') |
|
204 |
self.w(self.next_link(next, textsize)) |
|
205 |
self.w(u'</div>') |
|
206 |
self.req.html_headers.add_raw('<link rel="next" href="%s" />' |
|
207 |
% html_escape(next.absolute_url())) |
|
208 |
self.w(u'</div>') |
|
209 |
self.w(u'<div class="clear"></div>') |
|
210 |
||
211 |
def previous_link(self, previous, textsize): |
|
212 |
return u'<a href="%s" title="%s"><< %s</a>' % ( |
|
213 |
html_escape(previous.absolute_url()), |
|
214 |
self.req._('i18nprevnext_previous'), |
|
215 |
html_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
|
216 |
|
0 | 217 |
def next_link(self, next, textsize): |
218 |
return u'<a href="%s" title="%s">%s >></a>' % ( |
|
219 |
html_escape(next.absolute_url()), |
|
220 |
self.req._('i18nprevnext_next'), |
|
221 |
html_escape(cut(next.dc_title(), textsize))) |