author | Sandrine Ribeau <sandrine.ribeau@logilab.fr> |
Thu, 23 Apr 2009 11:58:11 -0700 | |
changeset 1461 | 023b123ab76b |
parent 1247 | 3332c92d950c |
child 1263 | 01152fffd593 |
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 |
|
612 | 13 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 14 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
15 |
""" |
|
16 |
__docformat__ = "restructuredtext en" |
|
17 |
||
18 |
from logilab.mtconverter import html_escape |
|
613 | 19 |
from cubicweb.common.selectors import any_rset, appobject_selectable |
0 | 20 |
from cubicweb.web.htmlwidgets import BoxWidget, BoxMenu, BoxHtml, RawBoxItem |
21 |
from cubicweb.web.box import BoxTemplate, ExtResourcesBoxTemplate |
|
22 |
||
23 |
_ = unicode |
|
1214
25480a710422
removing any_rset selector to enable 'download schema owl action' to layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
1113
diff
changeset
|
24 |
|
0 | 25 |
class EditBox(BoxTemplate): |
26 |
""" |
|
27 |
box with all actions impacting the entity displayed: edit, copy, delete |
|
28 |
change state, add related entities |
|
29 |
""" |
|
30 |
id = 'edit_box' |
|
31 |
title = _('actions') |
|
32 |
order = 2 |
|
33 |
||
1247
3332c92d950c
give view when selecting actions
sylvain.thenault@logilab.fr
parents:
1242
diff
changeset
|
34 |
def call(self, view=None, **kwargs): |
0 | 35 |
_ = self.req._ |
36 |
title = _(self.title) |
|
37 |
if self.rset: |
|
38 |
etypes = self.rset.column_types(0) |
|
39 |
if len(etypes) == 1: |
|
40 |
plural = self.rset.rowcount > 1 and 'plural' or '' |
|
41 |
etypelabel = display_name(self.req, iter(etypes).next(), plural) |
|
42 |
title = u'%s - %s' % (title, etypelabel.lower()) |
|
43 |
box = BoxWidget(title, self.id, _class="greyBoxFrame") |
|
44 |
# build list of actions |
|
1247
3332c92d950c
give view when selecting actions
sylvain.thenault@logilab.fr
parents:
1242
diff
changeset
|
45 |
actions = self.vreg.possible_actions(self.req, self.rset, view=view) |
0 | 46 |
add_menu = BoxMenu(_('add')) # 'addrelated' category |
47 |
other_menu = BoxMenu(_('more actions')) # 'moreactions' category |
|
48 |
searchstate = self.req.search_state[0] |
|
49 |
for category, menu in (('mainactions', box), |
|
50 |
('addrelated', add_menu), |
|
51 |
('moreactions', other_menu)): |
|
52 |
for action in actions.get(category, ()): |
|
53 |
menu.append(self.box_action(action)) |
|
54 |
if self.rset and self.rset.rowcount == 1 and \ |
|
55 |
not self.schema[self.rset.description[0][0]].is_final() and \ |
|
56 |
searchstate == 'normal': |
|
57 |
entity = self.rset.get_entity(0, 0) |
|
58 |
#entity.complete() |
|
59 |
if add_menu.items: |
|
60 |
self.info('explicit actions defined, ignoring potential rtags for %s', |
|
61 |
entity.e_schema) |
|
62 |
else: |
|
63 |
# some addrelated actions may be specified but no one is selectable |
|
64 |
# in which case we should not fallback to schema_actions. The proper |
|
65 |
# way to avoid this is to override add_related_schemas() on the |
|
66 |
# entity class to return an empty list |
|
67 |
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
|
68 |
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
|
69 |
self.workflow_actions(entity, box) |
0 | 70 |
if box.is_empty() and not other_menu.is_empty(): |
71 |
box.items = other_menu.items |
|
72 |
other_menu.items = [] |
|
73 |
self.add_submenu(box, add_menu, _('add')) |
|
74 |
self.add_submenu(box, other_menu) |
|
75 |
if not box.is_empty(): |
|
76 |
box.render(self.w) |
|
1214
25480a710422
removing any_rset selector to enable 'download schema owl action' to layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
1113
diff
changeset
|
77 |
|
0 | 78 |
def add_submenu(self, box, submenu, label_prefix=None): |
79 |
if len(submenu.items) == 1: |
|
80 |
boxlink = submenu.items[0] |
|
81 |
if label_prefix: |
|
82 |
boxlink.label = u'%s %s' % (label_prefix, boxlink.label) |
|
83 |
box.append(boxlink) |
|
84 |
elif submenu.items: |
|
85 |
box.append(submenu) |
|
86 |
||
87 |
def schema_actions(self, entity): |
|
88 |
user = self.req.user |
|
89 |
actions = [] |
|
90 |
_ = self.req._ |
|
91 |
eschema = entity.e_schema |
|
92 |
for rschema, teschema, x in entity.add_related_schemas(): |
|
93 |
if x == 'subject': |
|
94 |
label = 'add %s %s %s %s' % (eschema, rschema, teschema, x) |
|
95 |
url = self.linkto_url(entity, rschema, teschema, 'object') |
|
96 |
else: |
|
97 |
label = 'add %s %s %s %s' % (teschema, rschema, eschema, x) |
|
98 |
url = self.linkto_url(entity, rschema, teschema, 'subject') |
|
99 |
actions.append(self.mk_action(_(label), url)) |
|
100 |
return actions |
|
101 |
||
102 |
||
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
103 |
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
|
104 |
if 'in_state' in entity.e_schema.subject_relations() and entity.in_state: |
399 | 105 |
_ = self.req._ |
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
if transitions: |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
109 |
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
|
110 |
menu_items = [] |
1113
5608c79c789b
[boxes] that looked like a redundant call (needs review)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
859
diff
changeset
|
111 |
for tr in transitions: |
185
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
# 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
|
116 |
# 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
|
117 |
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
|
118 |
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
|
119 |
_('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
|
120 |
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
|
121 |
return None |
9fc69c46e5e7
EditBox: extract workflow actions construction in a separate method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
143
diff
changeset
|
122 |
|
0 | 123 |
def linkto_url(self, entity, rtype, etype, target): |
124 |
return self.build_url(vid='creation', etype=etype, |
|
125 |
__linkto='%s:%s:%s' % (rtype, entity.eid, target), |
|
126 |
__redirectpath=entity.rest_path(), # should not be url quoted! |
|
127 |
__redirectvid=self.req.form.get('vid', '')) |
|
128 |
||
129 |
||
130 |
class SearchBox(BoxTemplate): |
|
131 |
"""display a box with a simple search form""" |
|
132 |
id = 'search_box' |
|
133 |
visible = True # enabled by default |
|
134 |
title = _('search') |
|
135 |
order = 0 |
|
136 |
need_resources = 'SEARCH_GO' |
|
137 |
formdef = u"""<form action="%s"> |
|
138 |
<table id="tsearch"><tr><td> |
|
139 |
<input id="norql" type="text" accesskey="q" tabindex="%s" title="search text" value="%s" name="rql" /> |
|
140 |
<input type="hidden" name="__fromsearchbox" value="1" /> |
|
141 |
<input type="hidden" name="subvid" value="tsearch" /> |
|
142 |
</td><td> |
|
852
105893288777
simplify css style
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
536
diff
changeset
|
143 |
<input tabindex="%s" type="submit" id="rqlboxsubmit" class="rqlsubmit" value="" /> |
0 | 144 |
</td></tr></table> |
145 |
</form>""" |
|
146 |
||
147 |
||
148 |
def call(self, view=None, **kwargs): |
|
149 |
req = self.req |
|
150 |
if req.form.pop('__fromsearchbox', None): |
|
151 |
rql = req.form.get('rql', '') |
|
152 |
else: |
|
153 |
rql = '' |
|
154 |
form = self.formdef % (req.build_url('view'), req.next_tabindex(), |
|
155 |
html_escape(rql), req.next_tabindex()) |
|
156 |
title = u"""<span onclick="javascript: toggleVisibility('rqlinput')">%s</span>""" % req._(self.title) |
|
157 |
box = BoxWidget(title, self.id, _class="searchBoxFrame", islist=False, escape=False) |
|
158 |
box.append(BoxHtml(form)) |
|
159 |
box.render(self.w) |
|
160 |
||
161 |
||
162 |
# boxes disabled by default ################################################### |
|
163 |
||
164 |
class PossibleViewsBox(BoxTemplate): |
|
165 |
"""display a box containing links to all possible views""" |
|
166 |
id = 'possible_views_box' |
|
167 |
||
168 |
title = _('possible views') |
|
169 |
order = 10 |
|
170 |
require_groups = ('users', 'managers') |
|
171 |
visible = False |
|
172 |
||
173 |
def call(self, **kwargs): |
|
174 |
box = BoxWidget(self.req._(self.title), self.id) |
|
613 | 175 |
views = [v for v in self.vreg.possible_views(self.req, self.rset) |
176 |
if v.category != 'startupview'] |
|
177 |
for category, views in self.sort_actions(views): |
|
0 | 178 |
menu = BoxMenu(category) |
613 | 179 |
for view in views: |
180 |
menu.append(self.box_action(view)) |
|
0 | 181 |
box.append(menu) |
182 |
if not box.is_empty(): |
|
183 |
box.render(self.w) |
|
184 |
||
143
c4f11f70b75e
adding two different rss feed component
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
142
diff
changeset
|
185 |
|
0 | 186 |
class RSSIconBox(ExtResourcesBoxTemplate): |
187 |
"""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
|
188 |
__selectors__ = ExtResourcesBoxTemplate.__selectors__ + (appobject_selectable('components', 'rss_feed_url'),) |
0 | 189 |
|
190 |
id = 'rss' |
|
191 |
order = 999 |
|
192 |
need_resources = 'RSS_LOGO', |
|
193 |
visible = False |
|
194 |
||
195 |
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
|
196 |
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
|
197 |
url = urlgetter.feed_url() |
0 | 198 |
rss = self.req.external_resource('RSS_LOGO') |
536 | 199 |
self.w(u'<a href="%s"><img src="%s" alt="rss"/></a>\n' % (html_escape(url), rss)) |
0 | 200 |
|
125
979dbe0cade3
views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
107
diff
changeset
|
201 |
|
0 | 202 |
class StartupViewsBox(BoxTemplate): |
203 |
"""display a box containing links to all startup views""" |
|
204 |
id = 'startup_views_box' |
|
1214
25480a710422
removing any_rset selector to enable 'download schema owl action' to layout
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
1113
diff
changeset
|
205 |
visible = False# disabled by default |
0 | 206 |
title = _('startup views') |
207 |
order = 70 |
|
208 |
||
209 |
def call(self, **kwargs): |
|
210 |
box = BoxWidget(self.req._(self.title), self.id) |
|
211 |
for view in self.vreg.possible_views(self.req, None): |
|
212 |
if view.category == 'startupview': |
|
213 |
box.append(self.box_action(view)) |
|
214 |
||
215 |
if not box.is_empty(): |
|
216 |
box.render(self.w) |
|
217 |