author | sylvain.thenault@logilab.fr |
Mon, 16 Feb 2009 20:51:24 +0100 | |
branch | tls-sprint |
changeset 640 | 8e64f12be69c |
parent 593 | 6f6549780096 |
child 652 | 603c782dc092 |
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 |
||
9 |
from cubicweb.common.appobject import Component, SingletonComponent |
|
10 |
from cubicweb.common.utils import merge_dicts |
|
11 |
from cubicweb.common.view import VComponent, SingletonVComponent |
|
12 |
from cubicweb.common.registerers import action_registerer |
|
330
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
13 |
from cubicweb.common.selectors import (paginated_rset, one_line_rset, |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
14 |
primary_view, match_context_prop, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
15 |
condition_compat) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
16 |
#rql_condition, accept, has_relation, etype_rtype_selector |
0 | 17 |
from cubicweb.common.uilib import html_escape |
18 |
||
19 |
_ = unicode |
|
20 |
||
21 |
||
22 |
class EntityVComponent(VComponent): |
|
23 |
"""abstract base class for additinal components displayed in content |
|
24 |
headers and footer according to: |
|
25 |
|
|
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 |
""" |
|
32 |
||
33 |
__registry__ = 'contentnavigation' |
|
34 |
__registerer__ = action_registerer |
|
330
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
35 |
__selectors__ = (one_line_rset, primary_view, |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
36 |
match_context_prop, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
37 |
# etype_rtype_selector, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
38 |
# has_relation, accept, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
39 |
# rql_condition) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
40 |
) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
41 |
registered = accepts_compat(condition_compat(VComponent.registered.im_func)) |
0 | 42 |
|
43 |
property_defs = { |
|
44 |
_('visible'): dict(type='Boolean', default=True, |
|
45 |
help=_('display the box or not')), |
|
46 |
_('order'): dict(type='Int', default=99, |
|
47 |
help=_('display order of the component')), |
|
48 |
_('context'): dict(type='String', default='header', |
|
49 |
vocabulary=(_('navtop'), _('navbottom'), |
|
50 |
_('navcontenttop'), _('navcontentbottom')), |
|
51 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
52 |
help=_('context where this component should be displayed')), |
|
53 |
_('htmlclass'):dict(type='String', default='mainRelated', |
|
54 |
help=_('html class of the component')), |
|
55 |
} |
|
56 |
||
57 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
|
58 |
||
59 |
def call(self, view): |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
60 |
return self.cell_call(0, 0, view) |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
61 |
|
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
62 |
def cell_call(self, row, col, view): |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
63 |
raise NotImplementedError() |
0 | 64 |
|
65 |
||
66 |
class NavigationComponent(VComponent): |
|
67 |
"""abstract base class for navigation components""" |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
68 |
__selectors__ = (paginated_rset,) |
0 | 69 |
id = 'navigation' |
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'' |
|
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
77 |
|
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
78 |
@classmethod |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
79 |
def selected(cls, req, rset, row=None, col=None, page_size=None, **kwargs): |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
80 |
"""by default web app objects are usually instantiated on |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
81 |
selection according to a request, a result set, and optional |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
82 |
row and col |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
83 |
""" |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
84 |
instance = super(NavigationComponent, cls).selected(req, rset, row, col, **kwargs) |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
85 |
if page_size is not None: |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
86 |
instance.page_size = page_size |
593
6f6549780096
page_size can be given in url attributes
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
589
diff
changeset
|
87 |
elif 'page_size' in req.form: |
6f6549780096
page_size can be given in url attributes
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
589
diff
changeset
|
88 |
instance.page_size = int(req.form['page_size']) |
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
89 |
return instance |
0 | 90 |
|
91 |
def __init__(self, req, rset): |
|
92 |
super(NavigationComponent, self).__init__(req, rset) |
|
93 |
self.starting_from = 0 |
|
94 |
self.total = rset.rowcount |
|
95 |
||
96 |
def get_page_size(self): |
|
97 |
try: |
|
98 |
return self._page_size |
|
99 |
except AttributeError: |
|
100 |
self._page_size = self.req.property_value(self.page_size_property) |
|
101 |
return self._page_size |
|
102 |
||
103 |
def set_page_size(self, page_size): |
|
104 |
self._page_size = page_size |
|
105 |
||
106 |
page_size = property(get_page_size, set_page_size) |
|
107 |
||
108 |
def page_boundaries(self): |
|
109 |
try: |
|
110 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
111 |
start = int(self.req.form[self.start_param]) |
|
112 |
except KeyError: |
|
113 |
start, stop = 0, self.page_size |
|
114 |
self.starting_from = start |
|
115 |
return start, stop |
|
116 |
||
117 |
def clean_params(self, params): |
|
118 |
if self.start_param in params: |
|
119 |
del params[self.start_param] |
|
120 |
if self.stop_param in params: |
|
121 |
del params[self.stop_param] |
|
122 |
||
123 |
def page_link(self, path, params, start, stop, content): |
|
124 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
125 |
self.stop_param : stop,})) |
|
126 |
url = html_escape(url) |
|
127 |
if start == self.starting_from: |
|
128 |
return self.selected_page_link_templ % (url, content, content) |
|
129 |
return self.page_link_templ % (url, content, content) |
|
130 |
||
131 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
132 |
start = self.starting_from |
|
133 |
if not start : |
|
134 |
return self.no_previous_page_link |
|
135 |
start = max(0, start - self.page_size) |
|
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.previous_page_link_templ % (url, title, content) |
|
141 |
||
142 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
143 |
start = self.starting_from + self.page_size |
|
144 |
if start >= self.total: |
|
145 |
return self.no_next_page_link |
|
146 |
stop = start + self.page_size - 1 |
|
147 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
148 |
self.stop_param : stop,})) |
|
149 |
url = html_escape(url) |
|
150 |
return self.next_page_link_templ % (url, title, content) |
|
151 |
||
152 |
||
153 |
class RelatedObjectsVComponent(EntityVComponent): |
|
154 |
"""a section to display some related entities""" |
|
330
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
155 |
__selectors__ = (one_line_rset, primary_view, |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
156 |
match_context_prop, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
157 |
# etype_rtype_selector, has_relation, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
158 |
# accept) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
593
diff
changeset
|
159 |
) |
0 | 160 |
vid = 'list' |
161 |
||
162 |
def rql(self): |
|
163 |
"""override this method if you want to use a custom rql query. |
|
164 |
""" |
|
165 |
return None |
|
166 |
||
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
167 |
def cell_call(self, row, col, view=None): |
0 | 168 |
rql = self.rql() |
169 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
170 |
entity = self.rset.get_entity(row, col) |
0 | 171 |
if self.target == 'object': |
172 |
role = 'subject' |
|
173 |
else: |
|
174 |
role = 'object' |
|
175 |
rset = entity.related(self.rtype, role) |
|
176 |
else: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
177 |
eid = self.rset[row][col] |
0 | 178 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
179 |
if not rset.rowcount: |
|
180 |
return |
|
181 |
self.w(u'<div class="%s">' % self.div_class()) |
|
182 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
183 |
self.w(u'</div>') |