author | sylvain.thenault@logilab.fr |
Fri, 30 Jan 2009 15:26:51 +0100 | |
changeset 529 | 6e84e93fa7ec |
parent 330 | 705866d6eee8 |
child 589 | e1e100276f59 |
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'' |
|
76 |
||
77 |
def __init__(self, req, rset): |
|
78 |
super(NavigationComponent, self).__init__(req, rset) |
|
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: |
|
86 |
self._page_size = self.req.property_value(self.page_size_property) |
|
87 |
return self._page_size |
|
88 |
||
89 |
def set_page_size(self, page_size): |
|
90 |
self._page_size = page_size |
|
91 |
||
92 |
page_size = property(get_page_size, set_page_size) |
|
93 |
||
94 |
def page_boundaries(self): |
|
95 |
try: |
|
96 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
97 |
start = int(self.req.form[self.start_param]) |
|
98 |
except KeyError: |
|
99 |
start, stop = 0, self.page_size |
|
100 |
self.starting_from = start |
|
101 |
return start, stop |
|
102 |
||
103 |
def clean_params(self, params): |
|
104 |
if self.start_param in params: |
|
105 |
del params[self.start_param] |
|
106 |
if self.stop_param in params: |
|
107 |
del params[self.stop_param] |
|
108 |
||
109 |
def page_link(self, path, params, start, stop, content): |
|
110 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
111 |
self.stop_param : stop,})) |
|
112 |
url = html_escape(url) |
|
113 |
if start == self.starting_from: |
|
114 |
return self.selected_page_link_templ % (url, content, content) |
|
115 |
return self.page_link_templ % (url, content, content) |
|
116 |
||
117 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
118 |
start = self.starting_from |
|
119 |
if not start : |
|
120 |
return self.no_previous_page_link |
|
121 |
start = max(0, start - self.page_size) |
|
122 |
stop = start + self.page_size - 1 |
|
123 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
124 |
self.stop_param : stop,})) |
|
125 |
url = html_escape(url) |
|
126 |
return self.previous_page_link_templ % (url, title, content) |
|
127 |
||
128 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
129 |
start = self.starting_from + self.page_size |
|
130 |
if start >= self.total: |
|
131 |
return self.no_next_page_link |
|
132 |
stop = start + self.page_size - 1 |
|
133 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
134 |
self.stop_param : stop,})) |
|
135 |
url = html_escape(url) |
|
136 |
return self.next_page_link_templ % (url, title, content) |
|
137 |
||
138 |
||
139 |
class RelatedObjectsVComponent(EntityVComponent): |
|
140 |
"""a section to display some related entities""" |
|
330
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
141 |
__selectors__ = (one_line_rset, primary_view, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
142 |
etype_rtype_selector, has_relation, |
705866d6eee8
selectors update
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
237
diff
changeset
|
143 |
match_context_prop, accept) |
0 | 144 |
vid = 'list' |
145 |
||
146 |
def rql(self): |
|
147 |
"""override this method if you want to use a custom rql query. |
|
148 |
""" |
|
149 |
return None |
|
150 |
||
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
151 |
def cell_call(self, row, col, view=None): |
0 | 152 |
rql = self.rql() |
153 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
154 |
entity = self.rset.get_entity(row, col) |
0 | 155 |
if self.target == 'object': |
156 |
role = 'subject' |
|
157 |
else: |
|
158 |
role = 'object' |
|
159 |
rset = entity.related(self.rtype, role) |
|
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>') |