author | sylvain.thenault@logilab.fr |
Tue, 07 Apr 2009 09:10:26 +0200 | |
changeset 1278 | 10fa95dd91ab |
parent 593 | 6f6549780096 |
child 640 | 8e64f12be69c |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract component class and base components definition for CubicWeb web client |
2 |
||
3 |
:organization: Logilab |
|
4 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
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, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
14 |
rql_condition, accept, primary_view, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
15 |
match_context_prop, has_relation, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
16 |
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, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
36 |
match_context_prop, etype_rtype_selector, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
37 |
has_relation, accept, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
38 |
rql_condition) |
0 | 39 |
|
40 |
property_defs = { |
|
41 |
_('visible'): dict(type='Boolean', default=True, |
|
42 |
help=_('display the box or not')), |
|
43 |
_('order'): dict(type='Int', default=99, |
|
44 |
help=_('display order of the component')), |
|
45 |
_('context'): dict(type='String', default='header', |
|
46 |
vocabulary=(_('navtop'), _('navbottom'), |
|
47 |
_('navcontenttop'), _('navcontentbottom')), |
|
48 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
49 |
help=_('context where this component should be displayed')), |
|
50 |
_('htmlclass'):dict(type='String', default='mainRelated', |
|
51 |
help=_('html class of the component')), |
|
52 |
} |
|
53 |
||
54 |
accepts = ('Any',) |
|
55 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
|
56 |
condition = None |
|
57 |
||
58 |
def call(self, view): |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
59 |
return self.cell_call(0, 0, view) |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
60 |
|
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
61 |
def cell_call(self, row, col, view): |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
62 |
raise NotImplementedError() |
0 | 63 |
|
64 |
||
65 |
class NavigationComponent(VComponent): |
|
66 |
"""abstract base class for navigation components""" |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
67 |
__selectors__ = (paginated_rset,) |
0 | 68 |
id = 'navigation' |
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'' |
|
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
76 |
|
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
77 |
@classmethod |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
78 |
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
|
79 |
"""by default web app objects are usually instantiated on |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
80 |
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
|
81 |
row and col |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
82 |
""" |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
83 |
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
|
84 |
if page_size is not None: |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
85 |
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
|
86 |
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
|
87 |
instance.page_size = int(req.form['page_size']) |
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
88 |
return instance |
0 | 89 |
|
90 |
def __init__(self, req, rset): |
|
91 |
super(NavigationComponent, self).__init__(req, rset) |
|
92 |
self.starting_from = 0 |
|
93 |
self.total = rset.rowcount |
|
94 |
||
95 |
def get_page_size(self): |
|
96 |
try: |
|
97 |
return self._page_size |
|
98 |
except AttributeError: |
|
99 |
self._page_size = self.req.property_value(self.page_size_property) |
|
100 |
return self._page_size |
|
101 |
||
102 |
def set_page_size(self, page_size): |
|
103 |
self._page_size = page_size |
|
104 |
||
105 |
page_size = property(get_page_size, set_page_size) |
|
106 |
||
107 |
def page_boundaries(self): |
|
108 |
try: |
|
109 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
110 |
start = int(self.req.form[self.start_param]) |
|
111 |
except KeyError: |
|
112 |
start, stop = 0, self.page_size |
|
113 |
self.starting_from = start |
|
114 |
return start, stop |
|
115 |
||
116 |
def clean_params(self, params): |
|
117 |
if self.start_param in params: |
|
118 |
del params[self.start_param] |
|
119 |
if self.stop_param in params: |
|
120 |
del params[self.stop_param] |
|
121 |
||
122 |
def page_link(self, path, params, start, stop, content): |
|
123 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
124 |
self.stop_param : stop,})) |
|
125 |
url = html_escape(url) |
|
126 |
if start == self.starting_from: |
|
127 |
return self.selected_page_link_templ % (url, content, content) |
|
128 |
return self.page_link_templ % (url, content, content) |
|
129 |
||
130 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
131 |
start = self.starting_from |
|
132 |
if not start : |
|
133 |
return self.no_previous_page_link |
|
134 |
start = max(0, start - self.page_size) |
|
135 |
stop = start + self.page_size - 1 |
|
136 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
137 |
self.stop_param : stop,})) |
|
138 |
url = html_escape(url) |
|
139 |
return self.previous_page_link_templ % (url, title, content) |
|
140 |
||
141 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
142 |
start = self.starting_from + self.page_size |
|
143 |
if start >= self.total: |
|
144 |
return self.no_next_page_link |
|
145 |
stop = start + self.page_size - 1 |
|
146 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
147 |
self.stop_param : stop,})) |
|
148 |
url = html_escape(url) |
|
149 |
return self.next_page_link_templ % (url, title, content) |
|
150 |
||
151 |
||
152 |
class RelatedObjectsVComponent(EntityVComponent): |
|
153 |
"""a section to display some related entities""" |
|
330
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
154 |
__selectors__ = (one_line_rset, primary_view, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
155 |
etype_rtype_selector, has_relation, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
156 |
match_context_prop, accept) |
0 | 157 |
vid = 'list' |
158 |
||
159 |
def rql(self): |
|
160 |
"""override this method if you want to use a custom rql query. |
|
161 |
""" |
|
162 |
return None |
|
163 |
||
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
164 |
def cell_call(self, row, col, view=None): |
0 | 165 |
rql = self.rql() |
166 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
167 |
entity = self.rset.get_entity(row, col) |
0 | 168 |
if self.target == 'object': |
169 |
role = 'subject' |
|
170 |
else: |
|
171 |
role = 'object' |
|
172 |
rset = entity.related(self.rtype, role) |
|
173 |
else: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
174 |
eid = self.rset[row][col] |
0 | 175 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
176 |
if not rset.rowcount: |
|
177 |
return |
|
178 |
self.w(u'<div class="%s">' % self.div_class()) |
|
179 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
180 |
self.w(u'</div>') |