author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 26 Aug 2009 10:18:49 +0200 | |
branch | stable |
changeset 3010 | 684a0c546774 |
parent 2381 | caad2367d940 |
child 3012 | 97696093ba54 |
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 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
11 |
from logilab.common.deprecation import class_renamed |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
12 |
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
|
13 |
|
799
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
14 |
from cubicweb import role |
743 | 15 |
from cubicweb.utils import merge_dicts |
16 |
from cubicweb.view import View, Component |
|
655 | 17 |
from cubicweb.selectors import ( |
18 |
paginated_rset, one_line_rset, primary_view, match_context_prop, |
|
1132 | 19 |
partial_has_related_entities, condition_compat, accepts_compat, |
20 |
has_relation_compat) |
|
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() |
687
c23315c11185
removing explicit access to im_func, this is not necessary anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
681
diff
changeset
|
36 |
registered = accepts_compat(has_relation_compat(condition_compat(View.registered))) |
1433 | 37 |
|
0 | 38 |
property_defs = { |
39 |
_('visible'): dict(type='Boolean', default=True, |
|
2237
7e546c3d6ea5
[primary] fix #344249
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1977
diff
changeset
|
40 |
help=_('display the component or not')), |
0 | 41 |
_('order'): dict(type='Int', default=99, |
42 |
help=_('display order of the component')), |
|
43 |
_('context'): dict(type='String', default='header', |
|
1433 | 44 |
vocabulary=(_('navtop'), _('navbottom'), |
0 | 45 |
_('navcontenttop'), _('navcontentbottom')), |
46 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
47 |
help=_('context where this component should be displayed')), |
|
48 |
} |
|
1433 | 49 |
|
0 | 50 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
1433 | 51 |
|
880 | 52 |
def call(self, view=None): |
1661 | 53 |
return self.cell_call(0, 0, view=view) |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
54 |
|
880 | 55 |
def cell_call(self, row, col, view=None): |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
56 |
raise NotImplementedError() |
0 | 57 |
|
1433 | 58 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
59 |
class NavigationComponent(Component): |
0 | 60 |
"""abstract base class for navigation components""" |
655 | 61 |
id = 'navigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
62 |
__select__ = paginated_rset() |
1433 | 63 |
|
1511
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
64 |
property_defs = { |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
65 |
_('visible'): dict(type='Boolean', default=True, |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
66 |
help=_('display the component or not')), |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
67 |
} |
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
68 |
|
0 | 69 |
page_size_property = 'navigation.page-size' |
70 |
start_param = '__start' |
|
71 |
stop_param = '__stop' |
|
72 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
73 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
74 |
previous_page_link_templ = next_page_link_templ = page_link_templ |
|
75 |
no_previous_page_link = no_next_page_link = u'' |
|
1433 | 76 |
|
1339 | 77 |
def __init__(self, req, rset, **kwargs): |
78 |
super(NavigationComponent, self).__init__(req, 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: |
|
822 | 86 |
page_size = self.extra_kwargs.get('page_size') |
87 |
if page_size is None: |
|
88 |
if 'page_size' in self.req.form: |
|
89 |
page_size = int(self.req.form['page_size']) |
|
90 |
else: |
|
91 |
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
|
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: |
|
102 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
103 |
start = int(self.req.form[self.start_param]) |
|
104 |
except KeyError: |
|
105 |
start, stop = 0, self.page_size |
|
106 |
self.starting_from = start |
|
107 |
return start, stop |
|
1433 | 108 |
|
0 | 109 |
def clean_params(self, params): |
110 |
if self.start_param in params: |
|
111 |
del params[self.start_param] |
|
112 |
if self.stop_param in params: |
|
113 |
del params[self.stop_param] |
|
114 |
||
115 |
def page_link(self, path, params, start, stop, content): |
|
116 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
117 |
self.stop_param : stop,})) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
118 |
url = xml_escape(url) |
0 | 119 |
if start == self.starting_from: |
120 |
return self.selected_page_link_templ % (url, content, content) |
|
121 |
return self.page_link_templ % (url, content, content) |
|
122 |
||
123 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
124 |
start = self.starting_from |
|
125 |
if not start : |
|
126 |
return self.no_previous_page_link |
|
127 |
start = max(0, start - self.page_size) |
|
128 |
stop = start + self.page_size - 1 |
|
129 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
130 |
self.stop_param : stop,})) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
131 |
url = xml_escape(url) |
0 | 132 |
return self.previous_page_link_templ % (url, title, content) |
133 |
||
134 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
135 |
start = self.starting_from + self.page_size |
|
136 |
if start >= self.total: |
|
137 |
return self.no_next_page_link |
|
138 |
stop = start + self.page_size - 1 |
|
139 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
140 |
self.stop_param : stop,})) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2259
diff
changeset
|
141 |
url = xml_escape(url) |
0 | 142 |
return self.next_page_link_templ % (url, title, content) |
143 |
||
144 |
||
145 |
class RelatedObjectsVComponent(EntityVComponent): |
|
146 |
"""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
|
147 |
__select__ = EntityVComponent.__select__ & partial_has_related_entities() |
1433 | 148 |
|
0 | 149 |
vid = 'list' |
1433 | 150 |
|
0 | 151 |
def rql(self): |
655 | 152 |
"""override this method if you want to use a custom rql query""" |
0 | 153 |
return None |
1433 | 154 |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
155 |
def cell_call(self, row, col, view=None): |
0 | 156 |
rql = self.rql() |
157 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
158 |
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
|
159 |
rset = entity.related(self.rtype, role(self)) |
0 | 160 |
else: |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
161 |
eid = self.rset[row][col] |
0 | 162 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
163 |
if not rset.rowcount: |
|
164 |
return |
|
165 |
self.w(u'<div class="%s">' % self.div_class()) |
|
166 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
167 |
self.w(u'</div>') |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
168 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
169 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
170 |
VComponent = class_renamed('VComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
171 |
'VComponent is deprecated, use Component') |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
172 |
SingletonVComponent = class_renamed('SingletonVComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
173 |
'SingletonVComponent is deprecated, use ' |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
174 |
'Component and explicit registration control') |