author | Laure Bourgois <Laure.Bourgois@logilab.fr> |
Wed, 17 Dec 2008 09:42:48 +0100 | |
changeset 213 | 6842c3dee34b |
parent 185 | 9fc69c46e5e7 |
child 399 | 8df3b190275b |
permissions | -rw-r--r-- |
0 | 1 |
""" |
2 |
generic boxes for CubicWeb web client: |
|
3 |
||
4 |
* actions box |
|
5 |
* possible views box |
|
6 |
* rss icon |
|
7 |
||
8 |
additional (disabled by default) boxes |
|
9 |
* schema box |
|
10 |
* startup views box |
|
11 |
||
12 |
:organization: Logilab |
|
13 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
14 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
15 |
""" |
|
16 |
__docformat__ = "restructuredtext en" |
|
17 |
||
18 |
from logilab.mtconverter import html_escape |
|
19 |
||
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
20 |
from cubicweb.common.selectors import (rset_selector, appobject_selectable) |
0 | 21 |
from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, BoxHtml, RawBoxItem |
22 |
from cubicweb.web.box import BoxTemplate, ExtResourcesBoxTemplate |
|
23 |
||
24 |
_ = unicode |
|
25 |
||
26 |
||
27 |
class EditBox(BoxTemplate): |
|
28 |
""" |
|
29 |
box with all actions impacting the entity displayed: edit, copy, delete |
|
30 |
change state, add related entities |
|
31 |
""" |
|
32 |
__selectors__ = (rset_selector,) + BoxTemplate.__selectors__ |
|
33 |
id = 'edit_box' |
|
34 |
title = _('actions') |
|
35 |
order = 2 |
|
36 |
||
37 |
def call(self, **kwargs): |
|
38 |
_ = self.req._ |
|
39 |
title = _(self.title) |
|
40 |
if self.rset: |
|
41 |
etypes = self.rset.column_types(0) |
|
42 |
if len(etypes) == 1: |
|
43 |
plural = self.rset.rowcount > 1 and 'plural' or '' |
|
44 |
etypelabel = display_name(self.req, iter(etypes).next(), plural) |
|
45 |
title = u'%s - %s' % (title, etypelabel.lower()) |
|
46 |
box = BoxWidget(title, self.id, _class="greyBoxFrame") |
|
47 |
# build list of actions |
|
48 |
actions = self.vreg.possible_actions(self.req, self.rset) |
|
49 |
add_menu = BoxMenu(_('add')) # 'addrelated' category |
|
50 |
other_menu = BoxMenu(_('more actions')) # 'moreactions' category |
|
51 |
searchstate = self.req.search_state[0] |
|
52 |
for category, menu in (('mainactions', box), |
|
53 |
('addrelated', add_menu), |
|
54 |
('moreactions', other_menu)): |
|
55 |
for action in actions.get(category, ()): |
|
56 |
menu.append(self.box_action(action)) |
|
57 |
if self.rset and self.rset.rowcount == 1 and \ |
|
58 |
not self.schema[self.rset.description[0][0]].is_final() and \ |
|
59 |
searchstate == 'normal': |
|
60 |
entity = self.rset.get_entity(0, 0) |
|
61 |
#entity.complete() |
|
62 |
if add_menu.items: |
|
63 |
self.info('explicit actions defined, ignoring potential rtags for %s', |
|
64 |
entity.e_schema) |
|
65 |
else: |
|
66 |
# some addrelated actions may be specified but no one is selectable |
|
67 |
# in which case we should not fallback to schema_actions. The proper |
|
68 |
# way to avoid this is to override add_related_schemas() on the |
|
69 |
# entity class to return an empty list |
|
70 |
for action in self.schema_actions(entity): |
|
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
71 |
add_menu.append(action) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
72 |
self.workflow_actions(entity, box) |
0 | 73 |
if box.is_empty() and not other_menu.is_empty(): |
74 |
box.items = other_menu.items |
|
75 |
other_menu.items = [] |
|
76 |
self.add_submenu(box, add_menu, _('add')) |
|
77 |
self.add_submenu(box, other_menu) |
|
78 |
if not box.is_empty(): |
|
79 |
box.render(self.w) |
|
80 |
||
81 |
def add_submenu(self, box, submenu, label_prefix=None): |
|
82 |
if len(submenu.items) == 1: |
|
83 |
boxlink = submenu.items[0] |
|
84 |
if label_prefix: |
|
85 |
boxlink.label = u'%s %s' % (label_prefix, boxlink.label) |
|
86 |
box.append(boxlink) |
|
87 |
elif submenu.items: |
|
88 |
box.append(submenu) |
|
89 |
||
90 |
def schema_actions(self, entity): |
|
91 |
user = self.req.user |
|
92 |
actions = [] |
|
93 |
_ = self.req._ |
|
94 |
eschema = entity.e_schema |
|
95 |
for rschema, teschema, x in entity.add_related_schemas(): |
|
96 |
if x == 'subject': |
|
97 |
label = 'add %s %s %s %s' % (eschema, rschema, teschema, x) |
|
98 |
url = self.linkto_url(entity, rschema, teschema, 'object') |
|
99 |
else: |
|
100 |
label = 'add %s %s %s %s' % (teschema, rschema, eschema, x) |
|
101 |
url = self.linkto_url(entity, rschema, teschema, 'subject') |
|
102 |
actions.append(self.mk_action(_(label), url)) |
|
103 |
return actions |
|
104 |
||
105 |
||
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
106 |
def workflow_actions(self, entity, box): |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
107 |
if 'in_state' in entity.e_schema.subject_relations() and entity.in_state: |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
108 |
state = entity.in_state[0] |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
109 |
transitions = list(state.transitions(entity)) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
110 |
if transitions: |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
111 |
menu_title = u'%s: %s' % (_('state'), state.view('text')) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
112 |
menu_items = [] |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
113 |
for tr in state.transitions(entity): |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
114 |
url = entity.absolute_url(vid='statuschange', treid=tr.eid) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
115 |
menu_items.append(self.mk_action(_(tr.name), url)) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
116 |
box.append(BoxMenu(menu_title, menu_items)) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
117 |
# when there are no possible transition, put state if the menu if |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
118 |
# there are some other actions |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
119 |
elif not box.is_empty(): |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
120 |
menu_title = u'<a title="%s">%s: <i>%s</i></a>' % ( |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
121 |
_('no possible transition'), _('state'), state.view('text')) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
122 |
box.append(RawBoxItem(menu_title, 'boxMainactions')) |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
123 |
return None |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
124 |
|
0 | 125 |
def linkto_url(self, entity, rtype, etype, target): |
126 |
return self.build_url(vid='creation', etype=etype, |
|
127 |
__linkto='%s:%s:%s' % (rtype, entity.eid, target), |
|
128 |
__redirectpath=entity.rest_path(), # should not be url quoted! |
|
129 |
__redirectvid=self.req.form.get('vid', '')) |
|
130 |
||
131 |
||
132 |
class SearchBox(BoxTemplate): |
|
133 |
"""display a box with a simple search form""" |
|
134 |
id = 'search_box' |
|
135 |
visible = True # enabled by default |
|
136 |
title = _('search') |
|
137 |
order = 0 |
|
138 |
need_resources = 'SEARCH_GO' |
|
139 |
formdef = u"""<form action="%s"> |
|
140 |
<table id="tsearch"><tr><td> |
|
141 |
<input id="norql" type="text" accesskey="q" tabindex="%s" title="search text" value="%s" name="rql" /> |
|
142 |
<input type="hidden" name="__fromsearchbox" value="1" /> |
|
143 |
<input type="hidden" name="subvid" value="tsearch" /> |
|
144 |
</td><td> |
|
145 |
<input tabindex="%s" type="submit" id="rqlboxsubmit" value="" /> |
|
146 |
</td></tr></table> |
|
147 |
</form>""" |
|
148 |
||
149 |
||
150 |
def call(self, view=None, **kwargs): |
|
151 |
req = self.req |
|
152 |
if req.form.pop('__fromsearchbox', None): |
|
153 |
rql = req.form.get('rql', '') |
|
154 |
else: |
|
155 |
rql = '' |
|
156 |
form = self.formdef % (req.build_url('view'), req.next_tabindex(), |
|
157 |
html_escape(rql), req.next_tabindex()) |
|
158 |
title = u"""<span onclick="javascript: toggleVisibility('rqlinput')">%s</span>""" % req._(self.title) |
|
159 |
box = BoxWidget(title, self.id, _class="searchBoxFrame", islist=False, escape=False) |
|
160 |
box.append(BoxHtml(form)) |
|
161 |
box.render(self.w) |
|
162 |
||
163 |
||
164 |
# boxes disabled by default ################################################### |
|
165 |
||
166 |
class PossibleViewsBox(BoxTemplate): |
|
167 |
"""display a box containing links to all possible views""" |
|
168 |
id = 'possible_views_box' |
|
169 |
||
170 |
title = _('possible views') |
|
171 |
order = 10 |
|
172 |
require_groups = ('users', 'managers') |
|
173 |
visible = False |
|
174 |
||
175 |
def call(self, **kwargs): |
|
176 |
box = BoxWidget(self.req._(self.title), self.id) |
|
177 |
actions = [v for v in self.vreg.possible_views(self.req, self.rset) |
|
178 |
if v.category != 'startupview'] |
|
179 |
for category, actions in self.sort_actions(actions): |
|
180 |
menu = BoxMenu(category) |
|
181 |
for action in actions: |
|
182 |
menu.append(self.box_action(action)) |
|
183 |
box.append(menu) |
|
184 |
if not box.is_empty(): |
|
185 |
box.render(self.w) |
|
186 |
||
187 |
||
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
188 |
|
0 | 189 |
class RSSIconBox(ExtResourcesBoxTemplate): |
190 |
"""just display the RSS icon on uniform result set""" |
|
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
191 |
__selectors__ = ExtResourcesBoxTemplate.__selectors__ + (appobject_selectable('components', 'rss_feed_url'),) |
0 | 192 |
|
193 |
id = 'rss' |
|
194 |
order = 999 |
|
195 |
need_resources = 'RSS_LOGO', |
|
196 |
visible = False |
|
197 |
||
198 |
def call(self, **kwargs): |
|
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
199 |
urlgetter = self.vreg.select_component('rss_feed_url', self.req, self.rset) |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
200 |
url = urlgetter.feed_url() |
0 | 201 |
rss = self.req.external_resource('RSS_LOGO') |
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
125
diff
changeset
|
202 |
self.w(u'<a href="%s"><img src="%s" border="0" /></a>\n' % (html_escape(url), rss)) |
0 | 203 |
|
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
107
diff
changeset
|
204 |
|
0 | 205 |
## warning("schemabox ne marche plus pour le moment") |
206 |
## class SchemaBox(BoxTemplate): |
|
207 |
## """display a box containing link to list of entities by type""" |
|
208 |
## id = 'schema_box' |
|
209 |
## visible = False # disabled by default |
|
210 |
## title = _('entity list') |
|
211 |
## order = 60 |
|
212 |
||
213 |
## def call(self, **kwargs): |
|
214 |
## box = BoxWidget(self.req._(title), self.id) |
|
215 |
## for etype in self.config.etypes(self.req.user, 'read'): |
|
216 |
## view = self.vreg.select_view('list', self.req, self.etype_rset(etype)) |
|
217 |
## box.append(self.mk_action(display_name(self.req, etype, 'plural'), |
|
218 |
## view.url(), etype=etype)) |
|
219 |
## if not box.is_empty(): |
|
220 |
## box.render(self.w) |
|
221 |
||
222 |
class StartupViewsBox(BoxTemplate): |
|
223 |
"""display a box containing links to all startup views""" |
|
224 |
id = 'startup_views_box' |
|
225 |
visible = False # disabled by default |
|
226 |
title = _('startup views') |
|
227 |
order = 70 |
|
228 |
||
229 |
def call(self, **kwargs): |
|
230 |
box = BoxWidget(self.req._(self.title), self.id) |
|
231 |
for view in self.vreg.possible_views(self.req, None): |
|
232 |
if view.category == 'startupview': |
|
233 |
box.append(self.box_action(view)) |
|
234 |
||
235 |
if not box.is_empty(): |
|
236 |
box.render(self.w) |
|
237 |