author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Fri, 22 May 2009 00:46:41 +0200 | |
branch | stable |
changeset 1899 | 361774742f3e |
parent 1661 | 00340ebedc24 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract component class and base components definition for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
9 |
from logilab.common.deprecation import class_renamed |
743 | 10 |
from logilab.mtconverter import html_escape |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
11 |
|
799
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
12 |
from cubicweb import role |
743 | 13 |
from cubicweb.utils import merge_dicts |
14 |
from cubicweb.view import View, Component |
|
655 | 15 |
from cubicweb.selectors import ( |
16 |
paginated_rset, one_line_rset, primary_view, match_context_prop, |
|
1132 | 17 |
partial_has_related_entities, condition_compat, accepts_compat, |
18 |
has_relation_compat) |
|
0 | 19 |
|
20 |
_ = unicode |
|
21 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
22 |
class EntityVComponent(Component): |
0 | 23 |
"""abstract base class for additinal components displayed in content |
24 |
headers and footer according to: |
|
1433 | 25 |
|
0 | 26 |
* the displayed entity's type |
27 |
* a context (currently 'header' or 'footer') |
|
28 |
||
29 |
it should be configured using .accepts, .etype, .rtype, .target and |
|
30 |
.context class attributes |
|
31 |
""" |
|
1433 | 32 |
|
0 | 33 |
__registry__ = 'contentnavigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
34 |
__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
|
35 |
registered = accepts_compat(has_relation_compat(condition_compat(View.registered))) |
1433 | 36 |
|
0 | 37 |
property_defs = { |
38 |
_('visible'): dict(type='Boolean', default=True, |
|
39 |
help=_('display the box or not')), |
|
40 |
_('order'): dict(type='Int', default=99, |
|
41 |
help=_('display order of the component')), |
|
42 |
_('context'): dict(type='String', default='header', |
|
1433 | 43 |
vocabulary=(_('navtop'), _('navbottom'), |
0 | 44 |
_('navcontenttop'), _('navcontentbottom')), |
45 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
46 |
help=_('context where this component should be displayed')), |
|
47 |
_('htmlclass'):dict(type='String', default='mainRelated', |
|
48 |
help=_('html class of the component')), |
|
49 |
} |
|
1433 | 50 |
|
0 | 51 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
1433 | 52 |
|
880 | 53 |
def call(self, view=None): |
1661 | 54 |
return self.cell_call(0, 0, view=view) |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
55 |
|
880 | 56 |
def cell_call(self, row, col, view=None): |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
57 |
raise NotImplementedError() |
0 | 58 |
|
1433 | 59 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
60 |
class NavigationComponent(Component): |
0 | 61 |
"""abstract base class for navigation components""" |
655 | 62 |
id = 'navigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
63 |
__select__ = paginated_rset() |
1433 | 64 |
|
1511
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
65 |
property_defs = { |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
66 |
_('visible'): dict(type='Boolean', default=True, |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
67 |
help=_('display the component or not')), |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
68 |
} |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
69 |
|
0 | 70 |
page_size_property = 'navigation.page-size' |
71 |
start_param = '__start' |
|
72 |
stop_param = '__stop' |
|
73 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
74 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
75 |
previous_page_link_templ = next_page_link_templ = page_link_templ |
|
76 |
no_previous_page_link = no_next_page_link = u'' |
|
1433 | 77 |
|
1339 | 78 |
def __init__(self, req, rset, **kwargs): |
79 |
super(NavigationComponent, self).__init__(req, rset, **kwargs) |
|
0 | 80 |
self.starting_from = 0 |
81 |
self.total = rset.rowcount |
|
82 |
||
83 |
def get_page_size(self): |
|
84 |
try: |
|
85 |
return self._page_size |
|
86 |
except AttributeError: |
|
822 | 87 |
page_size = self.extra_kwargs.get('page_size') |
88 |
if page_size is None: |
|
89 |
if 'page_size' in self.req.form: |
|
90 |
page_size = int(self.req.form['page_size']) |
|
91 |
else: |
|
92 |
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
|
93 |
self._page_size = page_size |
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
94 |
return page_size |
0 | 95 |
|
96 |
def set_page_size(self, page_size): |
|
97 |
self._page_size = page_size |
|
1433 | 98 |
|
0 | 99 |
page_size = property(get_page_size, set_page_size) |
1433 | 100 |
|
0 | 101 |
def page_boundaries(self): |
102 |
try: |
|
103 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
104 |
start = int(self.req.form[self.start_param]) |
|
105 |
except KeyError: |
|
106 |
start, stop = 0, self.page_size |
|
107 |
self.starting_from = start |
|
108 |
return start, stop |
|
1433 | 109 |
|
0 | 110 |
def clean_params(self, params): |
111 |
if self.start_param in params: |
|
112 |
del params[self.start_param] |
|
113 |
if self.stop_param in params: |
|
114 |
del params[self.stop_param] |
|
115 |
||
116 |
def page_link(self, path, params, start, stop, content): |
|
117 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
118 |
self.stop_param : stop,})) |
|
119 |
url = html_escape(url) |
|
120 |
if start == self.starting_from: |
|
121 |
return self.selected_page_link_templ % (url, content, content) |
|
122 |
return self.page_link_templ % (url, content, content) |
|
123 |
||
124 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
125 |
start = self.starting_from |
|
126 |
if not start : |
|
127 |
return self.no_previous_page_link |
|
128 |
start = max(0, start - self.page_size) |
|
129 |
stop = start + self.page_size - 1 |
|
130 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
131 |
self.stop_param : stop,})) |
|
132 |
url = html_escape(url) |
|
133 |
return self.previous_page_link_templ % (url, title, content) |
|
134 |
||
135 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
136 |
start = self.starting_from + self.page_size |
|
137 |
if start >= self.total: |
|
138 |
return self.no_next_page_link |
|
139 |
stop = start + self.page_size - 1 |
|
140 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
141 |
self.stop_param : stop,})) |
|
142 |
url = html_escape(url) |
|
143 |
return self.next_page_link_templ % (url, title, content) |
|
144 |
||
145 |
||
146 |
class RelatedObjectsVComponent(EntityVComponent): |
|
147 |
"""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
|
148 |
__select__ = EntityVComponent.__select__ & partial_has_related_entities() |
1433 | 149 |
|
0 | 150 |
vid = 'list' |
1433 | 151 |
|
0 | 152 |
def rql(self): |
655 | 153 |
"""override this method if you want to use a custom rql query""" |
0 | 154 |
return None |
1433 | 155 |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
156 |
def cell_call(self, row, col, view=None): |
0 | 157 |
rql = self.rql() |
158 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
159 |
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
|
160 |
rset = entity.related(self.rtype, role(self)) |
0 | 161 |
else: |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
162 |
eid = self.rset[row][col] |
0 | 163 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
164 |
if not rset.rowcount: |
|
165 |
return |
|
166 |
self.w(u'<div class="%s">' % self.div_class()) |
|
167 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
168 |
self.w(u'</div>') |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
169 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
170 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
171 |
VComponent = class_renamed('VComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
172 |
'VComponent is deprecated, use Component') |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
173 |
SingletonVComponent = class_renamed('SingletonVComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
174 |
'SingletonVComponent is deprecated, use ' |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
175 |
'Component and explicit registration control') |