author | sylvain.thenault@logilab.fr |
Fri, 20 Feb 2009 01:14:15 +0100 | |
branch | tls-sprint |
changeset 876 | f652660ae9a6 |
parent 838 | f2c56312b03a |
child 880 | d46cf89ead07 |
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, |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
822
diff
changeset
|
17 |
partial_has_related_entities, partial_relation_possible, |
655 | 18 |
condition_compat, accepts_compat, has_relation_compat) |
670 | 19 |
from cubicweb.common.registerers import accepts_registerer |
0 | 20 |
|
21 |
_ = unicode |
|
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: |
|
26 |
|
|
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 |
""" |
|
33 |
||
34 |
__registry__ = 'contentnavigation' |
|
670 | 35 |
__registerer__ = accepts_registerer |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
36 |
__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
|
37 |
registered = accepts_compat(has_relation_compat(condition_compat(View.registered))) |
0 | 38 |
|
39 |
property_defs = { |
|
40 |
_('visible'): dict(type='Boolean', default=True, |
|
41 |
help=_('display the box or not')), |
|
42 |
_('order'): dict(type='Int', default=99, |
|
43 |
help=_('display order of the component')), |
|
44 |
_('context'): dict(type='String', default='header', |
|
45 |
vocabulary=(_('navtop'), _('navbottom'), |
|
46 |
_('navcontenttop'), _('navcontentbottom')), |
|
47 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
48 |
help=_('context where this component should be displayed')), |
|
49 |
_('htmlclass'):dict(type='String', default='mainRelated', |
|
50 |
help=_('html class of the component')), |
|
51 |
} |
|
52 |
||
53 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
|
54 |
||
55 |
def call(self, view): |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
56 |
return self.cell_call(0, 0, view) |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
57 |
|
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
58 |
def cell_call(self, row, col, view): |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
59 |
raise NotImplementedError() |
0 | 60 |
|
61 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
62 |
class NavigationComponent(Component): |
0 | 63 |
"""abstract base class for navigation components""" |
655 | 64 |
id = 'navigation' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
687
diff
changeset
|
65 |
__select__ = paginated_rset() |
655 | 66 |
|
0 | 67 |
page_size_property = 'navigation.page-size' |
68 |
start_param = '__start' |
|
69 |
stop_param = '__stop' |
|
70 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
71 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
72 |
previous_page_link_templ = next_page_link_templ = page_link_templ |
|
73 |
no_previous_page_link = no_next_page_link = u'' |
|
74 |
||
75 |
def __init__(self, req, rset): |
|
76 |
super(NavigationComponent, self).__init__(req, rset) |
|
77 |
self.starting_from = 0 |
|
78 |
self.total = rset.rowcount |
|
79 |
||
80 |
def get_page_size(self): |
|
81 |
try: |
|
82 |
return self._page_size |
|
83 |
except AttributeError: |
|
822 | 84 |
page_size = self.extra_kwargs.get('page_size') |
85 |
if page_size is None: |
|
86 |
if 'page_size' in self.req.form: |
|
87 |
page_size = int(self.req.form['page_size']) |
|
88 |
else: |
|
89 |
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
|
90 |
self._page_size = page_size |
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
91 |
return page_size |
0 | 92 |
|
93 |
def set_page_size(self, page_size): |
|
94 |
self._page_size = page_size |
|
95 |
||
96 |
page_size = property(get_page_size, set_page_size) |
|
97 |
||
98 |
def page_boundaries(self): |
|
99 |
try: |
|
100 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
101 |
start = int(self.req.form[self.start_param]) |
|
102 |
except KeyError: |
|
103 |
start, stop = 0, self.page_size |
|
104 |
self.starting_from = start |
|
105 |
return start, stop |
|
106 |
||
107 |
def clean_params(self, params): |
|
108 |
if self.start_param in params: |
|
109 |
del params[self.start_param] |
|
110 |
if self.stop_param in params: |
|
111 |
del params[self.stop_param] |
|
112 |
||
113 |
def page_link(self, path, params, start, stop, content): |
|
114 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
115 |
self.stop_param : stop,})) |
|
116 |
url = html_escape(url) |
|
117 |
if start == self.starting_from: |
|
118 |
return self.selected_page_link_templ % (url, content, content) |
|
119 |
return self.page_link_templ % (url, content, content) |
|
120 |
||
121 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
122 |
start = self.starting_from |
|
123 |
if not start : |
|
124 |
return self.no_previous_page_link |
|
125 |
start = max(0, start - self.page_size) |
|
126 |
stop = start + self.page_size - 1 |
|
127 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
128 |
self.stop_param : stop,})) |
|
129 |
url = html_escape(url) |
|
130 |
return self.previous_page_link_templ % (url, title, content) |
|
131 |
||
132 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
133 |
start = self.starting_from + self.page_size |
|
134 |
if start >= self.total: |
|
135 |
return self.no_next_page_link |
|
136 |
stop = start + self.page_size - 1 |
|
137 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
138 |
self.stop_param : stop,})) |
|
139 |
url = html_escape(url) |
|
140 |
return self.next_page_link_templ % (url, title, content) |
|
141 |
||
142 |
||
143 |
class RelatedObjectsVComponent(EntityVComponent): |
|
144 |
"""a section to display some related entities""" |
|
145 |
vid = 'list' |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
822
diff
changeset
|
146 |
__select__ = partial_relation_possible() & partial_has_related_entities() |
799
ad129d374ee2
fix page_size handling, use abstract_has_related_entities
sylvain.thenault@logilab.fr
parents:
747
diff
changeset
|
147 |
|
0 | 148 |
def rql(self): |
655 | 149 |
"""override this method if you want to use a custom rql query""" |
0 | 150 |
return None |
151 |
||
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
152 |
def cell_call(self, row, col, view=None): |
0 | 153 |
rql = self.rql() |
154 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
155 |
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
|
156 |
rset = entity.related(self.rtype, role(self)) |
0 | 157 |
else: |
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
158 |
eid = self.rset[row][col] |
0 | 159 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
160 |
if not rset.rowcount: |
|
161 |
return |
|
162 |
self.w(u'<div class="%s">' % self.div_class()) |
|
163 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
164 |
self.w(u'</div>') |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
165 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
166 |
|
681
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
167 |
VComponent = class_renamed('VComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
168 |
'VComponent is deprecated, use Component') |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
169 |
SingletonVComponent = class_renamed('SingletonVComponent', Component, |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
170 |
'SingletonVComponent is deprecated, use ' |
7cb402fa3958
use class_renamed, not class_moved
sylvain.thenault@logilab.fr
parents:
670
diff
changeset
|
171 |
'Component and explicit registration control') |