author | sylvain.thenault@logilab.fr |
Tue, 17 Feb 2009 14:45:54 +0100 | |
branch | tls-sprint |
changeset 661 | 4f61eb8a96b7 |
parent 659 | 5d754a1933cb |
child 670 | 6c332f5c969c |
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 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
9 |
from logilab.common.deprecation import class_moved |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
10 |
|
655 | 11 |
from cubicweb.selectors import ( |
12 |
paginated_rset, one_line_rset, primary_view, match_context_prop, |
|
13 |
condition_compat, accepts_compat, has_relation_compat) |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
14 |
from cubicweb.common.appobject import Component |
0 | 15 |
from cubicweb.common.utils import merge_dicts |
655 | 16 |
from cubicweb.common.view import View |
0 | 17 |
from cubicweb.common.registerers import action_registerer |
18 |
from cubicweb.common.uilib import html_escape |
|
19 |
||
20 |
_ = unicode |
|
21 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
22 |
class EntityVComponent(Component): |
0 | 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 |
|
655 | 35 |
__selectors__ = (one_line_rset, primary_view, match_context_prop,) |
36 |
registered = accepts_compat(has_relation_compat(condition_compat(View.registered.im_func))) |
|
0 | 37 |
|
38 |
property_defs = { |
|
39 |
_('visible'): dict(type='Boolean', default=True, |
|
40 |
help=_('display the box or not')), |
|
41 |
_('order'): dict(type='Int', default=99, |
|
42 |
help=_('display order of the component')), |
|
43 |
_('context'): dict(type='String', default='header', |
|
44 |
vocabulary=(_('navtop'), _('navbottom'), |
|
45 |
_('navcontenttop'), _('navcontentbottom')), |
|
46 |
#vocabulary=(_('header'), _('incontext'), _('footer')), |
|
47 |
help=_('context where this component should be displayed')), |
|
48 |
_('htmlclass'):dict(type='String', default='mainRelated', |
|
49 |
help=_('html class of the component')), |
|
50 |
} |
|
51 |
||
52 |
context = 'navcontentbottom' # 'footer' | 'header' | 'incontext' |
|
53 |
||
54 |
def call(self, view): |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
55 |
return self.cell_call(0, 0, view) |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
56 |
|
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
57 |
def cell_call(self, row, col, view): |
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
58 |
raise NotImplementedError() |
0 | 59 |
|
60 |
||
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
61 |
class NavigationComponent(Component): |
0 | 62 |
"""abstract base class for navigation components""" |
655 | 63 |
id = 'navigation' |
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
64 |
__selectors__ = (paginated_rset,) |
655 | 65 |
|
0 | 66 |
page_size_property = 'navigation.page-size' |
67 |
start_param = '__start' |
|
68 |
stop_param = '__stop' |
|
69 |
page_link_templ = u'<span class="slice"><a href="%s" title="%s">%s</a></span>' |
|
70 |
selected_page_link_templ = u'<span class="selectedSlice"><a href="%s" title="%s">%s</a></span>' |
|
71 |
previous_page_link_templ = next_page_link_templ = page_link_templ |
|
72 |
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
|
73 |
|
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
74 |
@classmethod |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
75 |
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
|
76 |
"""by default web app objects are usually instantiated on |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
77 |
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
|
78 |
row and col |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
79 |
""" |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
80 |
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
|
81 |
if page_size is not None: |
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
82 |
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
|
83 |
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
|
84 |
instance.page_size = int(req.form['page_size']) |
589
e1e100276f59
add page_size argument
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
529
diff
changeset
|
85 |
return instance |
0 | 86 |
|
87 |
def __init__(self, req, rset): |
|
88 |
super(NavigationComponent, self).__init__(req, rset) |
|
89 |
self.starting_from = 0 |
|
90 |
self.total = rset.rowcount |
|
91 |
||
92 |
def get_page_size(self): |
|
93 |
try: |
|
94 |
return self._page_size |
|
95 |
except AttributeError: |
|
96 |
self._page_size = self.req.property_value(self.page_size_property) |
|
97 |
return self._page_size |
|
98 |
||
99 |
def set_page_size(self, page_size): |
|
100 |
self._page_size = page_size |
|
101 |
||
102 |
page_size = property(get_page_size, set_page_size) |
|
103 |
||
104 |
def page_boundaries(self): |
|
105 |
try: |
|
106 |
stop = int(self.req.form[self.stop_param]) + 1 |
|
107 |
start = int(self.req.form[self.start_param]) |
|
108 |
except KeyError: |
|
109 |
start, stop = 0, self.page_size |
|
110 |
self.starting_from = start |
|
111 |
return start, stop |
|
112 |
||
113 |
def clean_params(self, params): |
|
114 |
if self.start_param in params: |
|
115 |
del params[self.start_param] |
|
116 |
if self.stop_param in params: |
|
117 |
del params[self.stop_param] |
|
118 |
||
119 |
def page_link(self, path, params, start, stop, content): |
|
120 |
url = self.build_url(path, **merge_dicts(params, {self.start_param : start, |
|
121 |
self.stop_param : stop,})) |
|
122 |
url = html_escape(url) |
|
123 |
if start == self.starting_from: |
|
124 |
return self.selected_page_link_templ % (url, content, content) |
|
125 |
return self.page_link_templ % (url, content, content) |
|
126 |
||
127 |
def previous_link(self, params, content='<<', title=_('previous_results')): |
|
128 |
start = self.starting_from |
|
129 |
if not start : |
|
130 |
return self.no_previous_page_link |
|
131 |
start = max(0, start - self.page_size) |
|
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.previous_page_link_templ % (url, title, content) |
|
137 |
||
138 |
def next_link(self, params, content='>>', title=_('next_results')): |
|
139 |
start = self.starting_from + self.page_size |
|
140 |
if start >= self.total: |
|
141 |
return self.no_next_page_link |
|
142 |
stop = start + self.page_size - 1 |
|
143 |
url = self.build_url(**merge_dicts(params, {self.start_param : start, |
|
144 |
self.stop_param : stop,})) |
|
145 |
url = html_escape(url) |
|
146 |
return self.next_page_link_templ % (url, title, content) |
|
147 |
||
148 |
||
149 |
class RelatedObjectsVComponent(EntityVComponent): |
|
150 |
"""a section to display some related entities""" |
|
151 |
vid = 'list' |
|
152 |
||
153 |
def rql(self): |
|
655 | 154 |
"""override this method if you want to use a custom rql query""" |
0 | 155 |
return None |
156 |
||
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
157 |
def cell_call(self, row, col, view=None): |
0 | 158 |
rql = self.rql() |
159 |
if rql is None: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
160 |
entity = self.rset.get_entity(row, col) |
0 | 161 |
if self.target == 'object': |
162 |
role = 'subject' |
|
163 |
else: |
|
164 |
role = 'object' |
|
165 |
rset = entity.related(self.rtype, role) |
|
166 |
else: |
|
529
6e84e93fa7ec
EntityVComponent should now implements cell_call
sylvain.thenault@logilab.fr
parents:
330
diff
changeset
|
167 |
eid = self.rset[row][col] |
0 | 168 |
rset = self.req.execute(self.rql(), {'x': eid}, 'x') |
169 |
if not rset.rowcount: |
|
170 |
return |
|
171 |
self.w(u'<div class="%s">' % self.div_class()) |
|
172 |
self.wview(self.vid, rset, title=self.req._(self.title).capitalize()) |
|
173 |
self.w(u'</div>') |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
174 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
175 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
176 |
VComponent = class_moved('VComponent', VComponent, |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
177 |
'VComponent is deprecated, use Component') |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
178 |
SingletonVComponent = class_moved('SingletonVComponent', VComponent, |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
659
diff
changeset
|
179 |
'SingletonVComponent is deprecated, use Component and explicit registration control') |