author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 16 Sep 2009 17:52:54 +0200 | |
branch | 3.5 |
changeset 3268 | e9f4ea71e696 |
parent 3014 | 62ce5a139b52 |
child 3023 | 7864fee8b4ec |
child 3607 | a7c5e62c7e00 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract component class and base 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:
1661
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:
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 |
743 | 17 |
from cubicweb.utils import merge_dicts |
18 |
from cubicweb.view import View, Component |
|
655 | 19 |
from cubicweb.selectors import ( |
20 |
paginated_rset, one_line_rset, primary_view, match_context_prop, |
|
1132 | 21 |
partial_has_related_entities, condition_compat, accepts_compat, |
22 |
has_relation_compat) |
|
0 | 23 |
|
24 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
25 |
class EntityVComponent(Component): |
0 | 26 |
"""abstract base class for additinal components displayed in content |
27 |
headers and footer according to: |
|
1433 | 28 |
|
0 | 29 |
* the displayed entity's type |
30 |
* a context (currently 'header' or 'footer') |
|
31 |
||
32 |
it should be configured using .accepts, .etype, .rtype, .target and |
|
33 |
.context class attributes |
|
34 |
""" |
|
1433 | 35 |
|
0 | 36 |
__registry__ = 'contentnavigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
37 |
__select__ = one_line_rset() & primary_view() & match_context_prop() |
687
c23315c11185
removing explicit access to im_func, this is not necessary anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
681
diff
changeset
|
38 |
registered = accepts_compat(has_relation_compat(condition_compat(View.registered))) |
1433 | 39 |
|
0 | 40 |
property_defs = { |
41 |
_('visible'): dict(type='Boolean', default=True, |
|
2237
7e546c3d6ea5
[primary] fix #344249
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1977
diff
changeset
|
42 |
help=_('display the component or not')), |
0 | 43 |
_('order'): dict(type='Int', default=99, |
44 |
help=_('display order of the component')), |
|
45 |
_('context'): dict(type='String', default='header', |
|
1433 | 46 |
vocabulary=(_('navtop'), _('navbottom'), |
0 | 47 |
_('navcontenttop'), _('navcontentbottom')), |
48 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
49 |
help=_('context where this component should be displayed')), |
|
50 |
} |
|
1433 | 51 |
|
0 | 52 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
1433 | 53 |
|
880 | 54 |
def call(self, view=None): |
1661 | 55 |
return self.cell_call(0, 0, view=view) |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
56 |
|
880 | 57 |
def cell_call(self, row, col, view=None): |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
58 |
raise NotImplementedError() |
0 | 59 |
|
1433 | 60 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
61 |
class NavigationComponent(Component): |
0 | 62 |
"""abstract base class for navigation components""" |
655 | 63 |
id = 'navigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
64 |
__select__ = paginated_rset() |
1433 | 65 |
|
1511
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
66 |
property_defs = { |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
67 |
_('visible'): dict(type='Boolean', default=True, |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
68 |
help=_('display the component or not')), |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
69 |
} |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
70 |
|
0 | 71 |
page_size_property = 'navigation.page-size' |
72 |
start_param = '__start' |
|
73 |
stop_param = '__stop' |
|
74 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
75 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
76 |
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
|
77 |
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
|
78 |
no_next_page_link = u'>>' |
1433 | 79 |
|
1339 | 80 |
def __init__(self, req, rset, **kwargs): |
81 |
super(NavigationComponent, self).__init__(req, rset, **kwargs) |
|
0 | 82 |
self.starting_from = 0 |
83 |
self.total = rset.rowcount |
|
84 |
||
85 |
def get_page_size(self): |
|
86 |
try: |
|
87 |
return self._page_size |
|
88 |
except AttributeError: |
|
822 | 89 |
page_size = self.extra_kwargs.get('page_size') |
90 |
if page_size is None: |
|
91 |
if 'page_size' in self.req.form: |
|
92 |
page_size = int(self.req.form['page_size']) |
|
93 |
else: |
|
94 |
page_size = self.req.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
|
95 |
self._page_size = page_size |
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
96 |
return page_size |
0 | 97 |
|
98 |
def set_page_size(self, page_size): |
|
99 |
self._page_size = page_size |
|
1433 | 100 |
|
0 | 101 |
page_size = property(get_page_size, set_page_size) |
1433 | 102 |
|
0 | 103 |
def page_boundaries(self): |
104 |
try: |
|
105 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
106 |
start = int(self.req.form[self.start_param]) |
|
107 |
except KeyError: |
|
108 |
start, stop = 0, self.page_size |
|
109 |
self.starting_from = start |
|
110 |
return start, stop |
|
1433 | 111 |
|
0 | 112 |
def clean_params(self, params): |
113 |
if self.start_param in params: |
|
114 |
del params[self.start_param] |
|
115 |
if self.stop_param in params: |
|
116 |
del params[self.stop_param] |
|
117 |
||
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
|
118 |
def page_url(self, path, params, start, stop): |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
119 |
params = merge_dicts(params, {self.start_param : start, |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
120 |
self.stop_param : stop,}) |
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': |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
122 |
rql = params.pop('rql', self.rset.printable_rql()) |
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: |
6a40a9a72957
[page nav] json navigation (when coming from a facets restriction for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3012
diff
changeset
|
128 |
url = self.build_url(path, **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
|
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: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
168 |
entity = self.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: |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
171 |
eid = self.rset[row][col] |
0 | 172 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
173 |
if not rset.rowcount: |
|
174 |
return |
|
175 |
self.w(u'<div class="%s">' % self.div_class()) |
|
176 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
177 |
self.w(u'</div>') |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
178 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
179 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
180 |
VComponent = class_renamed('VComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
181 |
'VComponent is deprecated, use Component') |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
182 |
SingletonVComponent = class_renamed('SingletonVComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
183 |
'SingletonVComponent is deprecated, use ' |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
184 |
'Component and explicit registration control') |