author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 15 Apr 2010 10:59:49 +0200 | |
branch | stable |
changeset 5267 | 7bac6791bbc2 |
parent 4882 | 4a42d700c52d |
child 5174 | 78438ad513ca |
child 5377 | 84d14ddfae13 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract component class and base components definition for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3840
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:
1661
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 |
|
3013
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
11 |
from simplejson import dumps |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
12 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
13 |
from logilab.common.deprecation import class_renamed |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
14 |
from logilab.mtconverter import xml_escape |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
15 |
|
799
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
16 |
from cubicweb import role |
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2799
diff
changeset
|
17 |
from cubicweb.view import Component |
655 | 18 |
from cubicweb.selectors import ( |
19 |
paginated_rset, one_line_rset, primary_view, match_context_prop, |
|
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2799
diff
changeset
|
20 |
partial_has_related_entities) |
0 | 21 |
|
22 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
23 |
class EntityVComponent(Component): |
0 | 24 |
"""abstract base class for additinal components displayed in content |
25 |
headers and footer according to: |
|
1433 | 26 |
|
0 | 27 |
* the displayed entity's type |
28 |
* a context (currently 'header' or 'footer') |
|
29 |
||
30 |
it should be configured using .accepts, .etype, .rtype, .target and |
|
31 |
.context class attributes |
|
32 |
""" |
|
1433 | 33 |
|
0 | 34 |
__registry__ = 'contentnavigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
35 |
__select__ = one_line_rset() & primary_view() & match_context_prop() |
1433 | 36 |
|
2799
b703639614e7
refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
37 |
cw_property_defs = { |
0 | 38 |
_('visible'): dict(type='Boolean', default=True, |
2237
7e546c3d6ea5
[primary] fix #344249
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1977
diff
changeset
|
39 |
help=_('display the component or not')), |
0 | 40 |
_('order'): dict(type='Int', default=99, |
41 |
help=_('display order of the component')), |
|
3840
2eff4348b1e4
prepare support for a 'context toolbar', used in tracker but will much probably be backported in default primary view. Turn pdfview component into a toolbar element
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3750
diff
changeset
|
42 |
_('context'): dict(type='String', default='navtop', |
1433 | 43 |
vocabulary=(_('navtop'), _('navbottom'), |
3840
2eff4348b1e4
prepare support for a 'context toolbar', used in tracker but will much probably be backported in default primary view. Turn pdfview component into a toolbar element
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3750
diff
changeset
|
44 |
_('navcontenttop'), _('navcontentbottom'), |
2eff4348b1e4
prepare support for a 'context toolbar', used in tracker but will much probably be backported in default primary view. Turn pdfview component into a toolbar element
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3750
diff
changeset
|
45 |
_('ctxtoolbar')), |
0 | 46 |
help=_('context where this component should be displayed')), |
47 |
} |
|
1433 | 48 |
|
3840
2eff4348b1e4
prepare support for a 'context toolbar', used in tracker but will much probably be backported in default primary view. Turn pdfview component into a toolbar element
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3750
diff
changeset
|
49 |
context = 'navcontentbottom' |
1433 | 50 |
|
880 | 51 |
def call(self, view=None): |
1661 | 52 |
return self.cell_call(0, 0, view=view) |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
53 |
|
880 | 54 |
def cell_call(self, row, col, view=None): |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
55 |
raise NotImplementedError() |
0 | 56 |
|
1433 | 57 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
58 |
class NavigationComponent(Component): |
0 | 59 |
"""abstract base class for navigation components""" |
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3023
diff
changeset
|
60 |
__regid__ = 'navigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
61 |
__select__ = paginated_rset() |
1433 | 62 |
|
2799
b703639614e7
refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
63 |
cw_property_defs = { |
1511
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
64 |
_('visible'): dict(type='Boolean', default=True, |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
65 |
help=_('display the component or not')), |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
66 |
} |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
67 |
|
0 | 68 |
page_size_property = 'navigation.page-size' |
69 |
start_param = '__start' |
|
70 |
stop_param = '__stop' |
|
71 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
72 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
73 |
previous_page_link_templ = next_page_link_templ = page_link_templ |
|
3012
97696093ba54
[page nav] display non clickable previous/next page when it does'nt make sense instead of nothing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3010
diff
changeset
|
74 |
no_previous_page_link = u'<<' |
97696093ba54
[page nav] display non clickable previous/next page when it does'nt make sense instead of nothing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3010
diff
changeset
|
75 |
no_next_page_link = u'>>' |
1433 | 76 |
|
1339 | 77 |
def __init__(self, req, rset, **kwargs): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
78 |
super(NavigationComponent, self).__init__(req, rset=rset, **kwargs) |
0 | 79 |
self.starting_from = 0 |
80 |
self.total = rset.rowcount |
|
81 |
||
82 |
def get_page_size(self): |
|
83 |
try: |
|
84 |
return self._page_size |
|
85 |
except AttributeError: |
|
3468
b02fa4db2868
[tests] make unittest_viewselectors pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3457
diff
changeset
|
86 |
page_size = self.cw_extra_kwargs.get('page_size') |
822 | 87 |
if page_size is None: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
88 |
if 'page_size' in self._cw.form: |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
89 |
page_size = int(self._cw.form['page_size']) |
822 | 90 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
91 |
page_size = self._cw.property_value(self.page_size_property) |
799
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
92 |
self._page_size = page_size |
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
93 |
return page_size |
0 | 94 |
|
95 |
def set_page_size(self, page_size): |
|
96 |
self._page_size = page_size |
|
1433 | 97 |
|
0 | 98 |
page_size = property(get_page_size, set_page_size) |
1433 | 99 |
|
0 | 100 |
def page_boundaries(self): |
101 |
try: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
102 |
stop = int(self._cw.form[self.stop_param]) + 1 |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
103 |
start = int(self._cw.form[self.start_param]) |
0 | 104 |
except KeyError: |
105 |
start, stop = 0, self.page_size |
|
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
106 |
if start >= len(self.cw_rset): |
3607
a7c5e62c7e00
don't crash if specified page size is greater than rset size
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3014
diff
changeset
|
107 |
start, stop = 0, self.page_size |
0 | 108 |
self.starting_from = start |
109 |
return start, stop |
|
1433 | 110 |
|
0 | 111 |
def clean_params(self, params): |
112 |
if self.start_param in params: |
|
113 |
del params[self.start_param] |
|
114 |
if self.stop_param in params: |
|
115 |
del params[self.stop_param] |
|
116 |
||
3013
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
117 |
def page_url(self, path, params, start, stop): |
4882
4a42d700c52d
[web] stop using deprecated merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
118 |
params = dict(params) |
4a42d700c52d
[web] stop using deprecated merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
119 |
params.update({self.start_param : start, |
4a42d700c52d
[web] stop using deprecated merge_dicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
120 |
self.stop_param : stop,}) |
3013
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
121 |
if path == 'json': |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
122 |
rql = params.pop('rql', self.cw_rset.printable_rql()) |
3013
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
123 |
# latest 'true' used for 'swap' mode |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
124 |
url = 'javascript: replacePageChunk(%s, %s, %s, %s, true)' % ( |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
125 |
dumps(params.get('divid', 'paginated-content')), |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
126 |
dumps(rql), dumps(params.pop('vid', None)), dumps(params)) |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
127 |
else: |
3457
0924d0d08d60
[api] __regid__, cw_* and friends
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
128 |
url = self._cw.build_url(path, **params) |
3013
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
129 |
return url |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
130 |
|
0 | 131 |
def page_link(self, path, params, start, stop, content): |
3013
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
132 |
url = xml_escape(self.page_url(path, params, start, stop)) |
0 | 133 |
if start == self.starting_from: |
134 |
return self.selected_page_link_templ % (url, content, content) |
|
135 |
return self.page_link_templ % (url, content, content) |
|
136 |
||
3014
62ce5a139b52
[page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3013
diff
changeset
|
137 |
def previous_link(self, path, params, content='<<', title=_('previous_results')): |
0 | 138 |
start = self.starting_from |
139 |
if not start : |
|
140 |
return self.no_previous_page_link |
|
141 |
start = max(0, start - self.page_size) |
|
142 |
stop = start + self.page_size - 1 |
|
3014
62ce5a139b52
[page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3013
diff
changeset
|
143 |
url = xml_escape(self.page_url(path, params, start, stop)) |
0 | 144 |
return self.previous_page_link_templ % (url, title, content) |
145 |
||
3014
62ce5a139b52
[page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3013
diff
changeset
|
146 |
def next_link(self, path, params, content='>>', title=_('next_results')): |
0 | 147 |
start = self.starting_from + self.page_size |
148 |
if start >= self.total: |
|
149 |
return self.no_next_page_link |
|
150 |
stop = start + self.page_size - 1 |
|
3014
62ce5a139b52
[page nav] previous/next page links should also consider url's base path
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3013
diff
changeset
|
151 |
url = xml_escape(self.page_url(path, params, start, stop)) |
0 | 152 |
return self.next_page_link_templ % (url, title, content) |
153 |
||
154 |
||
155 |
class RelatedObjectsVComponent(EntityVComponent): |
|
156 |
"""a section to display some related entities""" |
|
883
44f1aba675de
missing selectors, no more need for relation_possible when using has_related_entities (implied)
sylvain.thenault@logilab.fr
parents:
880
diff
changeset
|
157 |
__select__ = EntityVComponent.__select__ & partial_has_related_entities() |
1433 | 158 |
|
0 | 159 |
vid = 'list' |
1433 | 160 |
|
0 | 161 |
def rql(self): |
655 | 162 |
"""override this method if you want to use a custom rql query""" |
0 | 163 |
return None |
1433 | 164 |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
165 |
def cell_call(self, row, col, view=None): |
0 | 166 |
rql = self.rql() |
167 |
if rql is None: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
168 |
entity = self.cw_rset.get_entity(row, col) |
799
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
169 |
rset = entity.related(self.rtype, role(self)) |
0 | 170 |
else: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
171 |
eid = self.cw_rset[row][col] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3408
diff
changeset
|
172 |
rset = self._cw.execute(self.rql(), {'x': eid}, 'x') |
0 | 173 |
if not rset.rowcount: |
174 |
return |
|
175 |
self.w(u'<div class="%s">' % self.div_class()) |
|
3777 | 176 |
self.w(u'<h4>%s</h4>\n' % self._cw._(self.title).capitalize()) |
3750
b7fa3d93640a
display title in the component to avoid relying on view supporting title argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3607
diff
changeset
|
177 |
self.wview(self.vid, rset) |
0 | 178 |
self.w(u'</div>') |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
179 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
180 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
181 |
VComponent = class_renamed('VComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
182 |
'VComponent is deprecated, use Component') |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
183 |
SingletonVComponent = class_renamed('SingletonVComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
184 |
'SingletonVComponent is deprecated, use ' |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
185 |
'Component and explicit registration control') |